Skip to content

Commit 9f27caf

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

File tree

8 files changed

+30
-20
lines changed

8 files changed

+30
-20
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.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/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.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/basic/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/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.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/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 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/basic/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()

examples/basic/schelling/model.py

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

71+
self.schedule = mesa.time.RandomActivation(self)
7172
self.grid = mesa.space.SingleGrid(width, height, torus=True)
7273

7374
self.happy = 0
@@ -84,6 +85,7 @@ def __init__(
8485
agent_type = 1 if self.random.random() < self.minority_pc else 0
8586
agent = SchellingAgent(self.next_id(), self, agent_type)
8687
self.grid.place_agent(agent, pos)
88+
self.schedule.add(agent)
8789

8890
self.datacollector.collect(self)
8991

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

9799
self.datacollector.collect(self)
98100

99-
if self.happy == len(self.agents):
101+
if self.happy == self.schedule.get_agent_count():
100102
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-
50+
self.schedule = mesa.time.RandomActivation(self)
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-
78+
self.schedule.add(a)
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.agents.shuffle().do("step")
99+
self.schedule.step()
100100
# collect data
101101
self.datacollector.collect(self)
102102

0 commit comments

Comments
 (0)