Skip to content

Commit 041f699

Browse files
committed
Boltzmann wealth model: Refactor code
1 parent 10ace2e commit 041f699

File tree

1 file changed

+9
-9
lines changed
  • examples/boltzmann_wealth_model_experimental

1 file changed

+9
-9
lines changed

examples/boltzmann_wealth_model_experimental/model.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ 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)
2425
self.datacollector = mesa.DataCollector(
@@ -32,12 +33,10 @@ def __init__(self, N=100, width=10, height=10):
3233
y = self.random.randrange(self.grid.height)
3334
self.grid.place_agent(a, (x, y))
3435

35-
self.running = True
3636
self.datacollector.collect(self)
3737

3838
def step(self):
3939
self.agents.shuffle().do("step")
40-
# collect data
4140
self.datacollector.collect(self)
4241

4342
def run_model(self, n):
@@ -53,17 +52,18 @@ def __init__(self, unique_id, model):
5352
self.wealth = 1
5453

5554
def move(self):
56-
possible_steps = self.model.grid.get_neighborhood(
55+
possible_positions = self.model.grid.get_neighborhood(
5756
self.pos, moore=True, include_center=False
5857
)
59-
new_position = self.random.choice(possible_steps)
60-
self.model.grid.move_agent(self, new_position)
58+
self.model.grid.move_agent_to_one_of(self, possible_positions)
6159

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

0 commit comments

Comments
 (0)