-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathSimulationStatistics.py
65 lines (49 loc) · 1.79 KB
/
SimulationStatistics.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import abc
import time
import matplotlib.pyplot as plt
from Game import Strategy
class SimulationStatistics(abc.ABC):
@abc.abstractmethod
def record_stats(self, world, iteration):
pass
# override if desired
def end_simulation(self, world, iteration):
return False
@abc.abstractmethod
def print_results(self):
pass
class StrategyFractionsTimeSeries(SimulationStatistics):
def __init__(self):
self.cooperator_fraction_ts = []
self.defector_fraction_ts = []
def record_stats(self, world, iteration):
cooperator_fraction = world.get_num_players_with_strategy(
Strategy.cooperate)
defector_fraction = world.get_num_players_with_strategy(
Strategy.defect)
self.cooperator_fraction_ts.append(
cooperator_fraction / world.num_players)
self.defector_fraction_ts.append(defector_fraction / world.num_players)
def end_simulation(self, world, iteration):
if iteration > 500:
return True
else:
return False
def print_results(self):
self.figure = plt.figure("Strategy Fractions Time Series")
cooperator_fraction_plot = plt.plot(
self.cooperator_fraction_ts, label="Cooperator fraction"
)
defector_fraction_plot = plt.plot(
self.defector_fraction_ts, label="Defector fraction"
)
plt.xlabel("Iteration")
plt.ylabel("Fraction of players with strategy")
plt.legend()
self.figure.canvas.flush_events()
plt.show()
# sleep to prevent results disappearing immediately
time.sleep(20)
class StrategyFractionsTimeSeries4Network(StrategyFractionsTimeSeries):
def print_results(self):
return self.cooperator_fraction_ts