Skip to content

Commit

Permalink
Revert PR projectmesa#161: Replace schedulers with AgentSet functiona…
Browse files Browse the repository at this point in the history
…lity (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.
  • Loading branch information
EwoutH authored Aug 22, 2024
1 parent 4eb166d commit 9f27caf
Show file tree
Hide file tree
Showing 8 changed files with 30 additions and 20 deletions.
2 changes: 1 addition & 1 deletion examples/basic/boid_flockers/Flocker Test.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
"def draw_boids(model):\n",
" x_vals = []\n",
" y_vals = []\n",
" for boid in model.agents:\n",
" for boid in model.schedule.agents:\n",
" x, y = boid.pos\n",
" x_vals.append(x)\n",
" y_vals.append(y)\n",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def __init__(self, portrayal_method=None, canvas_height=500, canvas_width=500):

def render(self, model):
space_state = []
for obj in model.agents:
for obj in model.schedule.agents:
portrayal = self.portrayal_method(obj)
x, y = obj.pos
x = (x - model.space.x_min) / (model.space.x_max - model.space.x_min)
Expand Down
5 changes: 3 additions & 2 deletions examples/basic/boid_flockers/boid_flockers/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ def __init__(
self.vision = vision
self.speed = speed
self.separation = separation

self.schedule = mesa.time.RandomActivation(self)
self.space = mesa.space.ContinuousSpace(width, height, True)
self.factors = {"cohere": cohere, "separate": separate, "match": match}
self.make_agents()
Expand All @@ -144,6 +144,7 @@ def make_agents(self):
**self.factors,
)
self.space.place_agent(boid, pos)
self.schedule.add(boid)

def step(self):
self.agents.shuffle().do("step")
self.schedule.step()
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@


def compute_gini(model):
agent_wealths = [agent.wealth for agent in model.agents]
agent_wealths = [agent.wealth for agent in model.schedule.agents]
x = sorted(agent_wealths)
N = model.num_agents
B = sum(xi * (N - i) for i, xi in enumerate(x)) / (N * sum(x))
Expand All @@ -21,14 +21,14 @@ def __init__(self, N=100, width=10, height=10):
super().__init__()
self.num_agents = N
self.grid = mesa.space.MultiGrid(width, height, True)

self.schedule = mesa.time.RandomActivation(self)
self.datacollector = mesa.DataCollector(
model_reporters={"Gini": compute_gini}, agent_reporters={"Wealth": "wealth"}
)
# Create agents
for i in range(self.num_agents):
a = MoneyAgent(i, self)

self.schedule.add(a)
# Add the agent to a random grid cell
x = self.random.randrange(self.grid.width)
y = self.random.randrange(self.grid.height)
Expand All @@ -38,7 +38,7 @@ def __init__(self, N=100, width=10, height=10):
self.datacollector.collect(self)

def step(self):
self.agents.shuffle().do("step")
self.schedule.step()
# collect data
self.datacollector.collect(self)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def isAlive(self):
def neighbors(self):
return self.model.grid.iter_neighbors((self.x, self.y), True)

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

def assume_state(self):
def advance(self):
"""
Set the state to the new computed state -- computed in step().
"""
Expand Down
17 changes: 12 additions & 5 deletions examples/basic/conways_game_of_life/conways_game_of_life/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,15 @@ def __init__(self, width=50, height=50):
Create a new playing area of (width, height) cells.
"""
super().__init__()

# Set up the grid and schedule.

# Use SimultaneousActivation which simulates all the cells
# computing their next state simultaneously. This needs to
# be done because each cell's next state depends on the current
# state of all its neighbors -- before they've changed.
self.schedule = mesa.time.SimultaneousActivation(self)

# Use a simple grid, where edges wrap around.
self.grid = mesa.space.SingleGrid(width, height, torus=True)

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

self.running = True

def step(self):
"""
Perform the model step in two stages:
- First, all cells assume their next state (whether they will be dead or alive)
- Then, all cells change state to their next state
Have the scheduler advance each cell by one step
"""
self.agents.do("determine_state")
self.agents.do("assume_state")
self.schedule.step()
6 changes: 4 additions & 2 deletions examples/basic/schelling/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ def __init__(
self.homophily = homophily
self.radius = radius

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

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

self.datacollector.collect(self)

Expand All @@ -92,9 +94,9 @@ def step(self):
Run one step of the model.
"""
self.happy = 0 # Reset counter of happy agents
self.agents.shuffle().do("step")
self.schedule.step()

self.datacollector.collect(self)

if self.happy == len(self.agents):
if self.happy == self.schedule.get_agent_count():
self.running = False
6 changes: 3 additions & 3 deletions examples/basic/virus_on_network/virus_on_network/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def __init__(
prob = avg_node_degree / self.num_nodes
self.G = nx.erdos_renyi_graph(n=self.num_nodes, p=prob)
self.grid = mesa.space.NetworkGrid(self.G)

self.schedule = mesa.time.RandomActivation(self)
self.initial_outbreak_size = (
initial_outbreak_size if initial_outbreak_size <= num_nodes else num_nodes
)
Expand Down Expand Up @@ -75,7 +75,7 @@ def __init__(
self.recovery_chance,
self.gain_resistance_chance,
)

self.schedule.add(a)
# Add the agent to the node
self.grid.place_agent(a, node)

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

def step(self):
self.agents.shuffle().do("step")
self.schedule.step()
# collect data
self.datacollector.collect(self)

Expand Down

0 comments on commit 9f27caf

Please sign in to comment.