Skip to content

Commit d55d144

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

File tree

1 file changed

+12
-12
lines changed
  • examples/boltzmann_wealth_model_experimental

1 file changed

+12
-12
lines changed

examples/boltzmann_wealth_model_experimental/model.py

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,10 @@ def __init__(self, N=100, width=10, height=10):
3232
y = self.random.randrange(self.grid.height)
3333
self.grid.place_agent(a, (x, y))
3434

35-
self.running = True
3635
self.datacollector.collect(self)
3736

3837
def step(self):
3938
self.agents.shuffle().do("step")
40-
# collect data
4139
self.datacollector.collect(self)
4240

4341
def run_model(self, n):
@@ -53,23 +51,25 @@ def __init__(self, unique_id, model):
5351
self.wealth = 1
5452

5553
def move(self):
56-
possible_steps = self.model.grid.get_neighborhood(
54+
possible_positions = self.model.grid.get_neighborhood(
5755
self.pos, moore=True, include_center=False
5856
)
59-
new_position = self.random.choice(possible_steps)
60-
self.model.grid.move_agent(self, new_position)
57+
self.model.grid.move_agent_to_one_of(self, possible_positions)
6158

62-
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
59+
def maybe_give_money(self):
60+
if self.wealth <= 0:
61+
return
62+
cellmates = [
63+
c
64+
for c in self.model.grid.get_cell_list_contents([self.pos])
65+
# Ensure agent is not giving money to itself
66+
if c is not self
67+
]
6768
if len(cellmates) > 0:
6869
other = self.random.choice(cellmates)
6970
other.wealth += 1
7071
self.wealth -= 1
7172

7273
def step(self):
7374
self.move()
74-
if self.wealth > 0:
75-
self.give_money()
75+
self.maybe_give_money()

0 commit comments

Comments
 (0)