Repository navigation
Expand file tree
/
Copy pathreporter_funcs.py
More file actions
128 lines (89 loc) · 3.07 KB
/
Copy pathreporter_funcs.py
File metadata and controls
128 lines (89 loc) · 3.07 KB
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
def total_n_agents(model):
return len(model.schedule.agents)
def avg_agent_age(model):
if not model.schedule.agents:
return 0
return (
sum([agent.age for agent in model.schedule.agents])
/ len(model.schedule.agents)
)
def avg_delta_energy(model):
if not model.schedule.agents:
return 0
return (
sum([agent.delta_energy for agent in model.schedule.agents])
/ len(model.schedule.agents)
)
def n_friendlier(model):
return len([agent for agent in model.schedule.agents
if sum(agent.strategy)/4 >= 0.5])
def n_aggressive(model):
return len([agent for agent in model.schedule.agents
if sum(agent.strategy)/4 < 0.5])
def perc_cooperative_actions(model):
active_agents = [
a for a in model.schedule.agents if a.rece_interaction is not None
]
if not active_agents:
return 0
coop_actions = [
a.NCactions for a in active_agents
]
tot_actions = [
a.Nactions for a in active_agents
]
return sum(coop_actions) / sum(tot_actions)
def get_strategies(model):
return [agent.strategy for agent in model.schedule.agents]
def strategy_counter_factory(strategy, tol):
def strategy_counter(model):
return len([
a for a in model.schedule.agents
if all(
strategy[i] - tol < a.strategy[i] < strategy[i] + tol
for i in range(4))
])
return strategy_counter
def n_neighbor_measure(model):
# Calculate the avg number of neighbors
if not model.schedule.agents:
return 0
list_n_neighbors = [agent.n_neighbors for agent in model.schedule.agents]
return sum(list_n_neighbors)/len(list_n_neighbors)
def perc_CC_interactions(model):
# Calculate the percentage of the total number of cooperative actions
number_coop_actions = sum(
[a.NCactions for a in model.schedule.agents]
)
number_tot_actions = sum(
[a.Nactions for a in model.schedule.agents]
)
if not number_tot_actions:
return 0
return number_coop_actions / number_tot_actions
def coop_per_neig(model):
import scipy.optimize as optimize
number_coop_actions = [
a.NCactions for a in model.schedule.agents if a.n_neighbors != 0
]
number_neighbors = [
a.n_neighbors for a in model.schedule.agents if a.n_neighbors != 0
]
if not number_neighbors:
return 0
def lin(x, a, b):
return a*x + b
return optimize.curve_fit(lin, number_neighbors, number_coop_actions)[0][0]
def coop_per_neig_intc(model):
import scipy.optimize as optimize
number_coop_actions = [
a.NCactions for a in model.schedule.agents if a.n_neighbors != 0
]
number_neighbors = [
a.n_neighbors for a in model.schedule.agents if a.n_neighbors != 0
]
if not number_neighbors:
return 0
def lin(x, a, b):
return a*x + b
return optimize.curve_fit(lin, number_neighbors, number_coop_actions)[0][1]