Skip to content

Commit 90d668c

Browse files
committed
Reinstate PR #161: Replace schedulers with AgentSet functionality
This reinstates PR #161 after the previous revert.
1 parent 9f27caf commit 90d668c

File tree

8 files changed

+20
-30
lines changed

8 files changed

+20
-30
lines changed

examples/basic/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.schedule.agents:\n",
28+
" for boid in model.agents:\n",
2929
" x, y = boid.pos\n",
3030
" x_vals.append(x)\n",
3131
" y_vals.append(y)\n",

examples/basic/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.schedule.agents:
21+
for obj in model.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/basic/boid_flockers/boid_flockers/model.py

Lines changed: 2 additions & 3 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-
self.schedule = mesa.time.RandomActivation(self)
123+
124124
self.space = mesa.space.ContinuousSpace(width, height, True)
125125
self.factors = {"cohere": cohere, "separate": separate, "match": match}
126126
self.make_agents()
@@ -144,7 +144,6 @@ def make_agents(self):
144144
**self.factors,
145145
)
146146
self.space.place_agent(boid, pos)
147-
self.schedule.add(boid)
148147

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

examples/basic/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.schedule.agents]
5+
agent_wealths = [agent.wealth for agent in model.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-
self.schedule = mesa.time.RandomActivation(self)
24+
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-
self.schedule.add(a)
31+
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.schedule.step()
41+
self.agents.shuffle().do("step")
4242
# collect data
4343
self.datacollector.collect(self)
4444

examples/basic/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 step(self):
27+
def determine_state(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 step(self):
4646
if live_neighbors == 3:
4747
self._nextState = self.ALIVE
4848

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

examples/basic/conways_game_of_life/conways_game_of_life/model.py

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,6 @@ 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-
2617
# Use a simple grid, where edges wrap around.
2718
self.grid = mesa.space.SingleGrid(width, height, torus=True)
2819

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

3828
self.running = True
3929

4030
def step(self):
4131
"""
42-
Have the scheduler advance each cell by one step
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
4335
"""
44-
self.schedule.step()
36+
self.agents.do("determine_state")
37+
self.agents.do("assume_state")

examples/basic/schelling/model.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,6 @@ def __init__(
6868
self.homophily = homophily
6969
self.radius = radius
7070

71-
self.schedule = mesa.time.RandomActivation(self)
7271
self.grid = mesa.space.SingleGrid(width, height, torus=True)
7372

7473
self.happy = 0
@@ -85,7 +84,6 @@ def __init__(
8584
agent_type = 1 if self.random.random() < self.minority_pc else 0
8685
agent = SchellingAgent(self.next_id(), self, agent_type)
8786
self.grid.place_agent(agent, pos)
88-
self.schedule.add(agent)
8987

9088
self.datacollector.collect(self)
9189

@@ -94,9 +92,9 @@ def step(self):
9492
Run one step of the model.
9593
"""
9694
self.happy = 0 # Reset counter of happy agents
97-
self.schedule.step()
95+
self.agents.shuffle().do("step")
9896

9997
self.datacollector.collect(self)
10098

101-
if self.happy == self.schedule.get_agent_count():
99+
if self.happy == len(self.agents):
102100
self.running = False

examples/basic/virus_on_network/virus_on_network/model.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ def __init__(
4747
prob = avg_node_degree / self.num_nodes
4848
self.G = nx.erdos_renyi_graph(n=self.num_nodes, p=prob)
4949
self.grid = mesa.space.NetworkGrid(self.G)
50-
self.schedule = mesa.time.RandomActivation(self)
50+
5151
self.initial_outbreak_size = (
5252
initial_outbreak_size if initial_outbreak_size <= num_nodes else num_nodes
5353
)
@@ -75,7 +75,7 @@ def __init__(
7575
self.recovery_chance,
7676
self.gain_resistance_chance,
7777
)
78-
self.schedule.add(a)
78+
7979
# Add the agent to the node
8080
self.grid.place_agent(a, node)
8181

@@ -96,7 +96,7 @@ def resistant_susceptible_ratio(self):
9696
return math.inf
9797

9898
def step(self):
99-
self.schedule.step()
99+
self.agents.shuffle().do("step")
100100
# collect data
101101
self.datacollector.collect(self)
102102

0 commit comments

Comments
 (0)