Skip to content

Commit 1096760

Browse files
committed
move boltzman benchmark wealth model to new spaces
1 parent 55ce2de commit 1096760

File tree

3 files changed

+28
-21
lines changed

3 files changed

+28
-21
lines changed

benchmarks/BoltzmannWealth/boltzmann_wealth.py

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# https://github.com/projectmesa/mesa-examples/blob/main/examples/boltzmann_wealth_model_experimental/model.py
22
import mesa
3-
3+
import mesa.spaces as spaces
44

55
def compute_gini(model):
66
agent_wealths = [agent.wealth for agent in model.agents]
@@ -21,18 +21,18 @@ class BoltzmannWealth(mesa.Model):
2121
def __init__(self, seed=None, n=100, width=10, height=10):
2222
super().__init__()
2323
self.num_agents = n
24-
self.grid = mesa.space.MultiGrid(width, height, True)
24+
self.grid = spaces.OrthogonalMooreGrid((width, height))
2525
self.schedule = mesa.time.RandomActivation(self)
2626
self.datacollector = mesa.DataCollector(
2727
model_reporters={"Gini": compute_gini}, agent_reporters={"Wealth": "wealth"}
2828
)
2929
# Create agents
3030
for _ in range(self.num_agents):
31-
a = MoneyAgent(self)
31+
agent = MoneyAgent(self)
3232
# Add the agent to a random grid cell
33-
x = self.random.randrange(self.grid.width)
34-
y = self.random.randrange(self.grid.height)
35-
self.grid.place_agent(a, (x, y))
33+
x = self.random.randrange(width)
34+
y = self.random.randrange(height)
35+
agent.move_to(self.grid[(x, y)])
3636

3737
self.running = True
3838
self.datacollector.collect(self)
@@ -47,31 +47,34 @@ def run_model(self, n):
4747
self.step()
4848

4949

50-
class MoneyAgent(mesa.Agent):
50+
class MoneyAgent(mesa.spaces.CellAgent):
5151
"""An agent with fixed initial wealth."""
5252

5353
def __init__(self, model):
5454
super().__init__(model)
5555
self.wealth = 1
5656

57-
def move(self):
58-
possible_steps = self.model.grid.get_neighborhood(
59-
self.pos, moore=True, include_center=False
60-
)
61-
new_position = self.random.choice(possible_steps)
62-
self.model.grid.move_agent(self, new_position)
57+
# def move(self):
58+
# possible_steps = self.model.grid.get_neighborhood(
59+
# self.pos, moore=True, include_center=False
60+
# )
61+
# new_position = self.random.choice(possible_steps)
62+
# self.model.grid.move_agent(self, new_position)
6363

6464
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
65+
cellmates = [agent for agent in self.cell.agents if not agent is self]
6966
if len(cellmates) > 0:
7067
other = self.random.choice(cellmates)
7168
other.wealth += 1
7269
self.wealth -= 1
7370

7471
def step(self):
75-
self.move()
72+
self.move_to(self.cell.neighborhood().select_random_cell())
7673
if self.wealth > 0:
7774
self.give_money()
75+
76+
77+
if __name__ == '__main__':
78+
model = BoltzmannWealth()
79+
for _ in range(10):
80+
model.step()

mesa/spaces/cell.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,9 @@ def __repr__(self):
121121

122122
# FIXME: Revisit caching strategy on methods
123123
@cache # noqa: B019
124-
def neighborhood(self, radius=1, include_center=False):
124+
def neighborhood(self, radius: int = 1, include_center: bool = False) -> CellCollection:
125+
# FIXME? if this is a method rather than a property, should it not be get_neighborhood?
126+
# FIXME:: operating on this has side effects!
125127
return CellCollection(
126128
self._neighborhood(radius=radius, include_center=include_center),
127129
random=self.random,

mesa/spaces/cell_agent.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,16 @@ def __init__(self, model: Model) -> None:
2424
Create a new agent.
2525
2626
Args:
27-
unique_id (int): A unique identifier for this agent.
2827
model (Model): The model instance in which the agent exists.
2928
"""
3029
super().__init__(model)
3130
self.cell: Cell | None = None
3231

3332
def move_to(self, cell) -> None:
3433
if self.cell is not None:
35-
self.cell.remove_agent(self)
34+
try:
35+
self.cell.remove_agent(self)
36+
except ValueError:
37+
raise ValueError("Cannot remove agent from cell")
3638
self.cell = cell
3739
cell.add_agent(self)

0 commit comments

Comments
 (0)