Skip to content

Commit

Permalink
docs/tutorial: Replace scheduler in MoneyModel (projectmesa#2475)
Browse files Browse the repository at this point in the history
Replace scheduler in MoneyModel with AgentSet functionality. This model is used in the Viz tutorial.
  • Loading branch information
EwoutH authored Nov 9, 2024
1 parent 7e9e6f5 commit 22784df
Showing 1 changed file with 3 additions and 4 deletions.
7 changes: 3 additions & 4 deletions docs/tutorials/MoneyModel.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@


def compute_gini(model):
agent_wealths = [agent.wealth for agent in model.schedule.agents]
agent_wealths = [agent.wealth for agent in model.agents]
x = sorted(agent_wealths)
N = model.num_agents
B = sum(xi * (N - i) for i, xi in enumerate(x)) / (N * sum(x))
Expand Down Expand Up @@ -60,12 +60,10 @@ def __init__(self, n=10, width=10, height=10, seed=None):
super().__init__(seed=seed)
self.num_agents = n
self.grid = mesa.space.MultiGrid(width, height, True)
self.schedule = mesa.time.RandomActivation(self)

# Create agents
for _ in range(self.num_agents):
a = MoneyAgent(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 @@ -74,8 +72,9 @@ def __init__(self, n=10, width=10, height=10, seed=None):
self.datacollector = mesa.DataCollector(
model_reporters={"Gini": compute_gini}, agent_reporters={"Wealth": "wealth"}
)
self.datacollector.collect(self)

def step(self):
"""do one step of the model"""
self.agents.shuffle_do("step")
self.datacollector.collect(self)
self.schedule.step()

0 comments on commit 22784df

Please sign in to comment.