Skip to content

Commit 08ec008

Browse files
committed
moved all boltman models over
1 parent 1b341ad commit 08ec008

File tree

7 files changed

+29
-59
lines changed

7 files changed

+29
-59
lines changed

examples/aco_tsp/aco_tsp/model.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33
import mesa
44
import networkx as nx
55
import numpy as np
6-
7-
86
from mesa.spaces import CellAgent, Network
97

108

examples/bank_reserves/bank_reserves/model.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@
1212

1313
import mesa
1414
import numpy as np
15+
from mesa.spaces import OrthogonalMooreGrid
1516

1617
from .agents import Bank, Person
17-
from mesa.spaces import OrthogonalMooreGrid
1818

1919
"""
2020
If you want to perform a parameter sweep, call batch_run.py instead of run.py.

examples/bank_reserves/bank_reserves/server.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ def person_portrayal(agent):
8383

8484
# create instance of Mesa ModularServer
8585
server = mesa.visualization.ModularServer(
86-
BankReserves,
86+
BankReservesModel,
8787
[canvas_element, chart_element],
8888
"Bank Reserves Model",
8989
model_params=model_params,

examples/bank_reserves/batch_run.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,9 @@
2424
every step of every run.
2525
"""
2626

27-
import itertools
2827

2928
import mesa
30-
import numpy as np
3129
import pandas as pd
32-
3330
from bank_reserves.model import BankReservesModel
3431

3532

examples/boltzmann_wealth_model/boltzmann_wealth_model/model.py

Lines changed: 8 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -20,19 +20,19 @@ class BoltzmannWealthModel(mesa.Model):
2020
def __init__(self, N=100, width=10, height=10):
2121
super().__init__()
2222
self.num_agents = N
23-
self.grid = mesa.space.MultiGrid(width, height, True)
23+
self.grid = mesa.spaces.OrthogonalMooreGrid((width, height), torus=True, random=self.random)
2424

2525
self.datacollector = mesa.DataCollector(
2626
model_reporters={"Gini": compute_gini}, agent_reporters={"Wealth": "wealth"}
2727
)
2828
# Create agents
2929
for _ in range(self.num_agents):
30-
a = MoneyAgent(self)
30+
agent = MoneyAgent(self)
3131

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,21 @@ 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)
63-
6457
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
58+
cellmates = [agent for agent in self.cell.agents if agent is not self] # Ensure agent is not giving money to itself
6959
if len(cellmates) > 0:
7060
other = self.random.choice(cellmates)
7161
other.wealth += 1
7262
self.wealth -= 1
7363

7464
def step(self):
75-
self.move()
65+
self.move_to(self.cell.neighborhood().select_random_cell())
7666
if self.wealth > 0:
7767
self.give_money()

examples/boltzmann_wealth_model_experimental/model.py

Lines changed: 8 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -20,19 +20,19 @@ class BoltzmannWealthModel(mesa.Model):
2020
def __init__(self, N=100, width=10, height=10):
2121
super().__init__()
2222
self.num_agents = N
23-
self.grid = mesa.space.MultiGrid(width, height, True)
23+
self.grid = mesa.spaces.OrthogonalMooreGrid((width, height), torus=True, random=self.random)
2424

2525
self.datacollector = mesa.DataCollector(
2626
model_reporters={"Gini": compute_gini}, agent_reporters={"Wealth": "wealth"}
2727
)
2828
# Create agents
2929
for _ in range(self.num_agents):
30-
a = MoneyAgent(self)
30+
agent = MoneyAgent(self)
3131

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,21 @@ 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)
63-
6457
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
58+
cellmates = [agent for agent in self.cell.agents if agent is not self]
6959
if len(cellmates) > 0:
7060
other = self.random.choice(cellmates)
7161
other.wealth += 1
7262
self.wealth -= 1
7363

7464
def step(self):
75-
self.move()
65+
self.move_to(self.cell.neighborhood().select_random_cell())
7666
if self.wealth > 0:
7767
self.give_money()

examples/boltzmann_wealth_model_network/boltzmann_wealth_model_network/model.py

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ def __init__(self, num_agents=7, num_nodes=10):
1818
self.num_agents = num_agents
1919
self.num_nodes = num_nodes if num_nodes >= self.num_agents else self.num_agents
2020
self.G = nx.erdos_renyi_graph(n=self.num_nodes, p=0.5)
21-
self.grid = mesa.space.NetworkGrid(self.G)
21+
self.grid = mesa.spaces.Network(self.G, random=self.random, capacity=1)
22+
2223

2324
self.datacollector = mesa.DataCollector(
2425
model_reporters={"Gini": compute_gini},
@@ -28,11 +29,11 @@ def __init__(self, num_agents=7, num_nodes=10):
2829
list_of_random_nodes = self.random.sample(list(self.G), self.num_agents)
2930

3031
# Create agents
31-
for i in range(self.num_agents):
32-
a = MoneyAgent(self)
32+
for position in list_of_random_nodes:
33+
agent = MoneyAgent(self)
3334

3435
# Add the agent to a random node
35-
self.grid.place_agent(a, list_of_random_nodes[i])
36+
agent.move_to(self.grid[position])
3637

3738
self.running = True
3839
self.datacollector.collect(self)
@@ -47,31 +48,25 @@ def run_model(self, n):
4748
self.step()
4849

4950

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

5354
def __init__(self, model):
5455
super().__init__(model)
5556
self.wealth = 1
5657

57-
def move(self):
58-
possible_steps = [
59-
node
60-
for node in self.model.grid.get_neighborhood(self.pos, include_center=False)
61-
if self.model.grid.is_cell_empty(node)
62-
]
63-
if len(possible_steps) > 0:
64-
new_position = self.random.choice(possible_steps)
65-
self.model.grid.move_agent(self, new_position)
6658

6759
def give_money(self):
68-
neighbors = self.model.grid.get_neighbors(self.pos, include_center=False)
60+
neighbors = [agent for agent in self.cell.neighborhood().agents if not self]
6961
if len(neighbors) > 0:
7062
other = self.random.choice(neighbors)
7163
other.wealth += 1
7264
self.wealth -= 1
7365

7466
def step(self):
75-
self.move()
67+
empty_neighbors = [cell for cell in self.cell.neighborhood() if cell.is_empty]
68+
if empty_neighbors:
69+
self.move_to(self.random.choice(empty_neighbors))
70+
7671
if self.wealth > 0:
7772
self.give_money()

0 commit comments

Comments
 (0)