forked from projectmesa/mesa
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_time.py
167 lines (135 loc) · 4.97 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
'''
Test the advanced schedulers.
'''
from unittest import TestCase
from unittest.mock import patch
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 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):
pass
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()
assert model.log == self.expected_output
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)
with patch('mesa.time.random.shuffle') as mock_shuffle:
assert mock_shuffle.call_count == 0
model.step()
assert mock_shuffle.call_count == 1
def test_remove(self):
'''
Test staged activation can remove an agent
'''
model = MockModel(shuffle=True)
agent = model.schedule.agents[0]
model.schedule.remove(model.schedule.agents[0])
assert agent not in model.schedule.agents
class TestRandomActivation(TestCase):
'''
Test the random activation.
'''
def test_random_activation_step_shuffles(self):
'''
Test the random activation step
'''
model = MockModel(activation=RANDOM)
with patch('mesa.time.random.shuffle') as mock_shuffle:
model.schedule.step()
assert mock_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
'''
with patch('test_time.MockAgent.step') as mock_agent_step:
model = MockModel(activation=RANDOM)
model.step()
# one step for each of 2 agents
assert mock_agent_step.call_count == 2
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
'''
with patch('test_time.MockAgent.step') as mock_agent_step,\
patch('test_time.MockAgent.advance') as mock_agent_advance:
model = MockModel(activation=SIMULTANEOUS)
model.step()
# one step for each of 2 agents
assert mock_agent_step.call_count == 2
assert mock_agent_advance.call_count == 2