Skip to content

Commit 36b6a9d

Browse files
authored
Revert PR #161: Replace schedulers with AgentSet functionality (#170)
This commit reverts PR #161 projectmesa/mesa-examples#161 That PR assumed that time advancement would be done automatically, like proposed in #2223 We encountered some underlying issues with time, which we couldn't resolve in time.
1 parent 49e392b commit 36b6a9d

File tree

6 files changed

+23
-15
lines changed

6 files changed

+23
-15
lines changed

examples/boid_flockers/Flocker Test.ipynb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
"def draw_boids(model):\n",
2626
" x_vals = []\n",
2727
" y_vals = []\n",
28-
" for boid in model.agents:\n",
28+
" for boid in model.schedule.agents:\n",
2929
" x, y = boid.pos\n",
3030
" x_vals.append(x)\n",
3131
" y_vals.append(y)\n",

examples/boid_flockers/boid_flockers/SimpleContinuousModule.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ def __init__(self, portrayal_method=None, canvas_height=500, canvas_width=500):
1818

1919
def render(self, model):
2020
space_state = []
21-
for obj in model.agents:
21+
for obj in model.schedule.agents:
2222
portrayal = self.portrayal_method(obj)
2323
x, y = obj.pos
2424
x = (x - model.space.x_min) / (model.space.x_max - model.space.x_min)

examples/boid_flockers/boid_flockers/model.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ def __init__(
120120
self.vision = vision
121121
self.speed = speed
122122
self.separation = separation
123-
123+
self.schedule = mesa.time.RandomActivation(self)
124124
self.space = mesa.space.ContinuousSpace(width, height, True)
125125
self.factors = {"cohere": cohere, "separate": separate, "match": match}
126126
self.make_agents()
@@ -144,6 +144,7 @@ def make_agents(self):
144144
**self.factors,
145145
)
146146
self.space.place_agent(boid, pos)
147+
self.schedule.add(boid)
147148

148149
def step(self):
149-
self.agents.shuffle().do("step")
150+
self.schedule.step()

examples/boltzmann_wealth_model/boltzmann_wealth_model/model.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33

44
def compute_gini(model):
5-
agent_wealths = [agent.wealth for agent in model.agents]
5+
agent_wealths = [agent.wealth for agent in model.schedule.agents]
66
x = sorted(agent_wealths)
77
N = model.num_agents
88
B = sum(xi * (N - i) for i, xi in enumerate(x)) / (N * sum(x))
@@ -21,14 +21,14 @@ def __init__(self, N=100, width=10, height=10):
2121
super().__init__()
2222
self.num_agents = N
2323
self.grid = mesa.space.MultiGrid(width, height, True)
24-
24+
self.schedule = mesa.time.RandomActivation(self)
2525
self.datacollector = mesa.DataCollector(
2626
model_reporters={"Gini": compute_gini}, agent_reporters={"Wealth": "wealth"}
2727
)
2828
# Create agents
2929
for i in range(self.num_agents):
3030
a = MoneyAgent(i, self)
31-
31+
self.schedule.add(a)
3232
# Add the agent to a random grid cell
3333
x = self.random.randrange(self.grid.width)
3434
y = self.random.randrange(self.grid.height)
@@ -38,7 +38,7 @@ def __init__(self, N=100, width=10, height=10):
3838
self.datacollector.collect(self)
3939

4040
def step(self):
41-
self.agents.shuffle().do("step")
41+
self.schedule.step()
4242
# collect data
4343
self.datacollector.collect(self)
4444

examples/conways_game_of_life/conways_game_of_life/cell.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ def isAlive(self):
2424
def neighbors(self):
2525
return self.model.grid.iter_neighbors((self.x, self.y), True)
2626

27-
def determine_state(self):
27+
def step(self):
2828
"""
2929
Compute if the cell will be dead or alive at the next tick. This is
3030
based on the number of alive or dead neighbors. The state is not
@@ -46,7 +46,7 @@ def determine_state(self):
4646
if live_neighbors == 3:
4747
self._nextState = self.ALIVE
4848

49-
def assume_state(self):
49+
def advance(self):
5050
"""
5151
Set the state to the new computed state -- computed in step().
5252
"""

examples/conways_game_of_life/conways_game_of_life/model.py

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,15 @@ def __init__(self, width=50, height=50):
1414
Create a new playing area of (width, height) cells.
1515
"""
1616
super().__init__()
17+
18+
# Set up the grid and schedule.
19+
20+
# Use SimultaneousActivation which simulates all the cells
21+
# computing their next state simultaneously. This needs to
22+
# be done because each cell's next state depends on the current
23+
# state of all its neighbors -- before they've changed.
24+
self.schedule = mesa.time.SimultaneousActivation(self)
25+
1726
# Use a simple grid, where edges wrap around.
1827
self.grid = mesa.space.SingleGrid(width, height, torus=True)
1928

@@ -24,14 +33,12 @@ def __init__(self, width=50, height=50):
2433
if self.random.random() < 0.1:
2534
cell.state = cell.ALIVE
2635
self.grid.place_agent(cell, (x, y))
36+
self.schedule.add(cell)
2737

2838
self.running = True
2939

3040
def step(self):
3141
"""
32-
Perform the model step in two stages:
33-
- First, all cells assume their next state (whether they will be dead or alive)
34-
- Then, all cells change state to their next state
42+
Have the scheduler advance each cell by one step
3543
"""
36-
self.agents.do("determine_state")
37-
self.agents.do("assume_state")
44+
self.schedule.step()

0 commit comments

Comments
 (0)