Skip to content

Commit edb733b

Browse files
committed
Boltzmann wealth model: Use the new model.agents API
1 parent 6e68e4e commit edb733b

File tree

1 file changed

+13
-13
lines changed
  • examples/boltzmann_wealth_model_experimental

1 file changed

+13
-13
lines changed

examples/boltzmann_wealth_model_experimental/model.py

Lines changed: 13 additions & 13 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 = model.agents.get("wealth")
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))
@@ -19,27 +19,26 @@ class BoltzmannWealthModel(mesa.Model):
1919

2020
def __init__(self, N=100, width=10, height=10):
2121
super().__init__()
22+
self.running = True # TODO remove this line when at Mesa 3.0
2223
self.num_agents = N
2324
self.grid = mesa.space.MultiGrid(width, height, True)
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-
self.schedule.add(a)
3231
# Add the agent to a random grid cell
3332
x = self.random.randrange(self.grid.width)
3433
y = self.random.randrange(self.grid.height)
3534
self.grid.place_agent(a, (x, y))
3635

37-
self.running = True
3836
self.datacollector.collect(self)
3937

4038
def step(self):
41-
self.schedule.step()
42-
# collect data
39+
self.agents.shuffle().do("step")
40+
# Must be before data collection.
41+
self._advance_time() # Temporary API; will be finalized by Mesa 3.0 release
4342
self.datacollector.collect(self)
4443

4544
def run_model(self, n):
@@ -55,17 +54,18 @@ def __init__(self, unique_id, model):
5554
self.wealth = 1
5655

5756
def move(self):
58-
possible_steps = self.model.grid.get_neighborhood(
57+
possible_positions = self.model.grid.get_neighborhood(
5958
self.pos, moore=True, include_center=False
6059
)
61-
new_position = self.random.choice(possible_steps)
62-
self.model.grid.move_agent(self, new_position)
60+
self.model.grid.move_agent_to_one_of(self, possible_positions)
6361

6462
def give_money(self):
65-
cellmates = self.model.grid.get_cell_list_contents([self.pos])
66-
cellmates.pop(
67-
cellmates.index(self)
68-
) # Ensure agent is not giving money to itself
63+
cellmates = [
64+
c
65+
for c in self.model.grid.get_cell_list_contents([self.pos])
66+
# Ensure agent is not giving money to itself
67+
if c is not self
68+
]
6969
if len(cellmates) > 0:
7070
other = self.random.choice(cellmates)
7171
other.wealth += 1

0 commit comments

Comments
 (0)