Skip to content

Commit

Permalink
fix: pass model.random to schedulers (#2359)
Browse files Browse the repository at this point in the history
As pionted out in by @EwoutH, #2350 broke the tutorial. This is due to not updating the schedulers to pass model.random. This fixes that bug (and does a few minor updates to the tutorial).

The tutorial needs a complete update to remove schedulers and use (in my view) the new grid spaces. I am not going to do that in this PR.
  • Loading branch information
quaquel authored Oct 15, 2024
1 parent 3f75fc4 commit 6fcd5b0
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 5 deletions.
4 changes: 2 additions & 2 deletions docs/tutorials/MoneyModel.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,15 +49,15 @@ def step(self):
class MoneyModel(mesa.Model):
"""A model with some number of agents."""

def __init__(self, N, width, height):
def __init__(self, N, width, height, seed=None):
"""Initialize a MoneyModel instance.
Args:
N: The number of agents.
width: width of the grid.
height: Height of the grid.
"""
super().__init__()
super().__init__(seed=seed)
self.num_agents = N
self.grid = mesa.space.MultiGrid(width, height, True)
self.schedule = mesa.time.RandomActivation(self)
Expand Down
8 changes: 5 additions & 3 deletions mesa/time.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ def __init__(self, model: Model, agents: Iterable[Agent] | None = None) -> None:
if agents is None:
agents = []

self._agents: AgentSet = AgentSet(agents, model)
self._agents: AgentSet = AgentSet(agents, model.random)

self._remove_warning_given = False
self._agents_key_warning_given = False
Expand Down Expand Up @@ -312,7 +312,9 @@ def __init__(self, model: Model, agents: Iterable[Agent] | None = None) -> None:
try:
self._agents_by_type[type(agent)].add(agent)
except KeyError:
self._agents_by_type[type(agent)] = AgentSet([agent], self.model)
self._agents_by_type[type(agent)] = AgentSet(
[agent], self.model.random
)

def add(self, agent: Agent) -> None:
"""Add an Agent object to the schedule.
Expand All @@ -325,7 +327,7 @@ def add(self, agent: Agent) -> None:
try:
self._agents_by_type[type(agent)].add(agent)
except KeyError:
self._agents_by_type[type(agent)] = AgentSet([agent], self.model)
self._agents_by_type[type(agent)] = AgentSet([agent], self.model.random)

def remove(self, agent: Agent) -> None:
"""Remove all instances of a given agent from the schedule.
Expand Down

0 comments on commit 6fcd5b0

Please sign in to comment.