forked from projectmesa/mesa
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_time.py
188 lines (153 loc) · 5.35 KB
/
test_time.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
"""
Test the advanced schedulers.
"""
import unittest
from unittest import TestCase, mock
from mesa import Model, Agent
from mesa.time import (
BaseScheduler,
StagedActivation,
RandomActivation,
SimultaneousActivation,
)
RANDOM = "random"
STAGED = "staged"
SIMULTANEOUS = "simultaneous"
class MockAgent(Agent):
"""
Minimalistic agent for testing purposes.
"""
def __init__(self, unique_id, model):
super().__init__(unique_id, model)
self.steps = 0
self.advances = 0
def stage_one(self):
self.model.log.append(self.unique_id + "_1")
def stage_two(self):
self.model.log.append(self.unique_id + "_2")
def advance(self):
self.advances += 1
def step(self):
self.steps += 1
class MockModel(Model):
def __init__(self, shuffle=False, activation=STAGED):
"""
Creates a Model instance with a schedule
Args:
shuffle (Bool): whether or not to instantiate a scheduler
with shuffling.
This option is only used for
StagedActivation schedulers.
activation (str): which kind of scheduler to use.
'random' creates a RandomActivation scheduler.
'staged' creates a StagedActivation scheduler.
The default scheduler is a BaseScheduler.
"""
self.log = []
# Make scheduler
if activation == STAGED:
model_stages = ["stage_one", "stage_two"]
self.schedule = StagedActivation(self, model_stages, shuffle=shuffle)
elif activation == RANDOM:
self.schedule = RandomActivation(self)
elif activation == SIMULTANEOUS:
self.schedule = SimultaneousActivation(self)
else:
self.schedule = BaseScheduler(self)
# Make agents
for name in ["A", "B"]:
agent = MockAgent(name, self)
self.schedule.add(agent)
def step(self):
self.schedule.step()
class TestStagedActivation(TestCase):
"""
Test the staged activation.
"""
expected_output = ["A_1", "B_1", "A_2", "B_2"]
def test_no_shuffle(self):
"""
Testing staged activation without shuffling.
"""
model = MockModel(shuffle=False)
model.step()
model.step()
assert all([i == j for i, j in zip(model.log[:4], model.log[4:])])
def test_shuffle(self):
"""
Test staged activation with shuffling
"""
model = MockModel(shuffle=True)
model.step()
for output in self.expected_output[:2]:
assert output in model.log[:2]
for output in self.expected_output[2:]:
assert output in model.log[2:]
def test_shuffle_shuffles_agents(self):
model = MockModel(shuffle=True)
model.random = mock.Mock()
assert model.random.shuffle.call_count == 0
model.step()
assert model.random.shuffle.call_count == 1
def test_remove(self):
"""
Test staged activation can remove an agent
"""
model = MockModel(shuffle=True)
agent_keys = list(model.schedule._agents.keys())
agent = model.schedule._agents[agent_keys[0]]
model.schedule.remove(agent)
assert agent not in model.schedule.agents
def test_add_existing_agent(self):
model = MockModel()
agent = model.schedule.agents[0]
with self.assertRaises(Exception):
model.schedule.add(agent)
class TestRandomActivation(TestCase):
"""
Test the random activation.
"""
def test_random_activation_step_shuffles(self):
"""
Test the random activation step
"""
model = MockModel(activation=RANDOM)
model.random = mock.Mock()
model.schedule.step()
assert model.random.shuffle.call_count == 1
def test_random_activation_step_increments_step_and_time_counts(self):
"""
Test the random activation step increments step and time counts
"""
model = MockModel(activation=RANDOM)
assert model.schedule.steps == 0
assert model.schedule.time == 0
model.schedule.step()
assert model.schedule.steps == 1
assert model.schedule.time == 1
def test_random_activation_step_steps_each_agent(self):
"""
Test the random activation step causes each agent to step
"""
model = MockModel(activation=RANDOM)
model.step()
agent_steps = [i.steps for i in model.schedule.agents]
# one step for each of 2 agents
assert all(map(lambda x: x == 1, agent_steps))
class TestSimultaneousActivation(TestCase):
"""
Test the simultaneous activation.
"""
def test_simultaneous_activation_step_steps_and_advances_each_agent(self):
"""
Test the simultaneous activation step causes each agent to step
"""
model = MockModel(activation=SIMULTANEOUS)
model.step()
# one step for each of 2 agents
agent_steps = [i.steps for i in model.schedule.agents]
agent_advances = [i.advances for i in model.schedule.agents]
assert all(map(lambda x: x == 1, agent_steps))
assert all(map(lambda x: x == 1, agent_advances))
if __name__ == "__main__":
unittest.main()