Skip to content

Commit

Permalink
Boltzmann wealth model: Refactor code
Browse files Browse the repository at this point in the history
  • Loading branch information
rht committed Feb 22, 2024
1 parent 10ace2e commit 041f699
Showing 1 changed file with 9 additions and 9 deletions.
18 changes: 9 additions & 9 deletions examples/boltzmann_wealth_model_experimental/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ class BoltzmannWealthModel(mesa.Model):

def __init__(self, N=100, width=10, height=10):
super().__init__()
self.running = True # TODO remove this line when at Mesa 3.0
self.num_agents = N
self.grid = mesa.space.MultiGrid(width, height, True)
self.datacollector = mesa.DataCollector(
Expand All @@ -32,12 +33,10 @@ def __init__(self, N=100, width=10, height=10):
y = self.random.randrange(self.grid.height)
self.grid.place_agent(a, (x, y))

self.running = True
self.datacollector.collect(self)

def step(self):
self.agents.shuffle().do("step")
# collect data
self.datacollector.collect(self)

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

def move(self):
possible_steps = self.model.grid.get_neighborhood(
possible_positions = self.model.grid.get_neighborhood(
self.pos, moore=True, include_center=False
)
new_position = self.random.choice(possible_steps)
self.model.grid.move_agent(self, new_position)
self.model.grid.move_agent_to_one_of(self, possible_positions)

def give_money(self):
cellmates = self.model.grid.get_cell_list_contents([self.pos])
cellmates.pop(
cellmates.index(self)
) # Ensure agent is not giving money to itself
cellmates = [
c
for c in self.model.grid.get_cell_list_contents([self.pos])
# Ensure agent is not giving money to itself
if c is not self
]
if len(cellmates) > 0:
other = self.random.choice(cellmates)
other.wealth += 1
Expand Down

0 comments on commit 041f699

Please sign in to comment.