forked from projectmesa/mesa
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_batch_run.py
200 lines (177 loc) · 4.96 KB
/
test_batch_run.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
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
import mesa
from mesa.agent import Agent
from mesa.batchrunner import _make_model_kwargs
from mesa.datacollection import DataCollector
from mesa.model import Model
from mesa.time import BaseScheduler
def test_make_model_kwargs():
assert _make_model_kwargs({"a": 3, "b": 5}) == [{"a": 3, "b": 5}]
assert _make_model_kwargs({"a": 3, "b": range(3)}) == [
{"a": 3, "b": 0},
{"a": 3, "b": 1},
{"a": 3, "b": 2},
]
assert _make_model_kwargs({"a": range(2), "b": range(2)}) == [
{"a": 0, "b": 0},
{"a": 0, "b": 1},
{"a": 1, "b": 0},
{"a": 1, "b": 1},
]
# If the value is a single string, do not iterate over it.
assert _make_model_kwargs({"a": "value"}) == [{"a": "value"}]
class MockAgent(Agent):
"""
Minimalistic agent implementation for testing purposes
"""
def __init__(self, unique_id, model, val):
super().__init__(unique_id, model)
self.unique_id = unique_id
self.val = val
self.local = 0
def step(self):
self.val += 1
self.local += 0.25
class MockModel(Model):
"""
Minimalistic model for testing purposes
"""
def __init__(
self,
variable_model_param=None,
variable_agent_param=None,
fixed_model_param=None,
schedule=None,
enable_agent_reporters=True,
n_agents=3,
**kwargs,
):
super().__init__()
self.schedule = BaseScheduler(self) if schedule is None else schedule
self.variable_model_param = variable_model_param
self.variable_agent_param = variable_agent_param
self.fixed_model_param = fixed_model_param
self.n_agents = n_agents
if enable_agent_reporters:
agent_reporters = {"agent_id": "unique_id", "agent_local": "local"}
else:
agent_reporters = None
self.datacollector = DataCollector(
model_reporters={"reported_model_param": self.get_local_model_param},
agent_reporters=agent_reporters,
)
self.running = True
self.init_agents()
def init_agents(self):
if self.variable_agent_param is None:
agent_val = 1
else:
agent_val = self.variable_agent_param
for i in range(self.n_agents):
self.schedule.add(MockAgent(i, self, agent_val))
def get_local_model_param(self):
return 42
def step(self):
self.datacollector.collect(self)
self.schedule.step()
def test_batch_run():
result = mesa.batch_run(MockModel, {}, number_processes=2)
assert result == [
{
"RunId": 0,
"iteration": 0,
"Step": 1000,
"reported_model_param": 42,
"AgentID": 0,
"agent_id": 0,
"agent_local": 250.0,
},
{
"RunId": 0,
"iteration": 0,
"Step": 1000,
"reported_model_param": 42,
"AgentID": 1,
"agent_id": 1,
"agent_local": 250.0,
},
{
"RunId": 0,
"iteration": 0,
"Step": 1000,
"reported_model_param": 42,
"AgentID": 2,
"agent_id": 2,
"agent_local": 250.0,
},
]
def test_batch_run_with_params():
mesa.batch_run(
MockModel,
{
"variable_model_params": range(3),
"variable_agent_params": ["H", "E", "Y"],
},
number_processes=2,
)
def test_batch_run_no_agent_reporters():
result = mesa.batch_run(
MockModel, {"enable_agent_reporters": False}, number_processes=2
)
print(result)
assert result == [
{
"RunId": 0,
"iteration": 0,
"Step": 1000,
"enable_agent_reporters": False,
"reported_model_param": 42,
}
]
def test_batch_run_single_core():
mesa.batch_run(MockModel, {}, number_processes=1, iterations=6)
def test_batch_run_unhashable_param():
result = mesa.batch_run(
MockModel,
{
"n_agents": 2,
"variable_model_params": [{"key": "value"}],
},
iterations=2,
)
template = {
"Step": 1000,
"reported_model_param": 42,
"agent_local": 250.0,
"n_agents": 2,
"variable_model_params": {"key": "value"},
}
assert result == [
{
"RunId": 0,
"iteration": 0,
"AgentID": 0,
"agent_id": 0,
**template,
},
{
"RunId": 0,
"iteration": 0,
"AgentID": 1,
"agent_id": 1,
**template,
},
{
"RunId": 1,
"iteration": 1,
"AgentID": 0,
"agent_id": 0,
**template,
},
{
"RunId": 1,
"iteration": 1,
"AgentID": 1,
"agent_id": 1,
**template,
},
]