Skip to content

Commit d2c8013

Browse files
committed
sugarscape
1 parent bd75e82 commit d2c8013

File tree

6 files changed

+101
-105
lines changed

6 files changed

+101
-105
lines changed

examples/sugarscape_cg/sugarscape_cg/agents.py

Lines changed: 25 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -2,79 +2,76 @@
22

33
import mesa
44

5+
from mesa.experimental.cell_space import FixedAgent, CellAgent
56

6-
def get_distance(pos_1, pos_2):
7+
def get_distance(cell_1, cell_2):
78
"""Get the distance between two point
89
910
Args:
1011
pos_1, pos_2: Coordinate tuples for both points.
1112
"""
12-
x1, y1 = pos_1
13-
x2, y2 = pos_2
13+
x1, y1 = cell_1.coordinate
14+
x2, y2 = cell_2.coordinate
1415
dx = x1 - x2
1516
dy = y1 - y2
1617
return math.sqrt(dx**2 + dy**2)
1718

1819

19-
class SsAgent(mesa.Agent):
20-
def __init__(self, model, moore=False, sugar=0, metabolism=0, vision=0):
20+
class SsAgent(CellAgent):
21+
def __init__(self, model, cell, sugar=0, metabolism=0, vision=0):
2122
super().__init__(model)
22-
self.moore = moore
23+
self.cell = cell
2324
self.sugar = sugar
2425
self.metabolism = metabolism
2526
self.vision = vision
2627

27-
def get_sugar(self, pos):
28-
this_cell = self.model.grid.get_cell_list_contents([pos])
29-
for agent in this_cell:
30-
if type(agent) is Sugar:
28+
def get_sugar(self, cell):
29+
for agent in cell.agents:
30+
if isinstance(agent, Sugar):
3131
return agent
3232

33-
def is_occupied(self, pos):
34-
this_cell = self.model.grid.get_cell_list_contents([pos])
35-
return any(isinstance(agent, SsAgent) for agent in this_cell)
33+
def is_occupied(self, cell):
34+
return any(isinstance(agent, SsAgent) for agent in cell.agents)
3635

3736
def move(self):
3837
# Get neighborhood within vision
3938
neighbors = [
40-
i
41-
for i in self.model.grid.get_neighborhood(
42-
self.pos, self.moore, False, radius=self.vision
43-
)
44-
if not self.is_occupied(i)
39+
cell
40+
for cell in self.cell.get_neighborhood(radius=self.vision)
41+
if not self.is_occupied(cell)
4542
]
46-
neighbors.append(self.pos)
43+
neighbors.append(self.cell)
4744
# Look for location with the most sugar
48-
max_sugar = max(self.get_sugar(pos).amount for pos in neighbors)
45+
max_sugar = max(self.get_sugar(cell).amount for cell in neighbors)
4946
candidates = [
50-
pos for pos in neighbors if self.get_sugar(pos).amount == max_sugar
47+
cell for cell in neighbors if self.get_sugar(cell).amount == max_sugar
5148
]
5249
# Narrow down to the nearest ones
53-
min_dist = min(get_distance(self.pos, pos) for pos in candidates)
50+
min_dist = min(get_distance(self.cell, cell) for cell in candidates)
5451
final_candidates = [
55-
pos for pos in candidates if get_distance(self.pos, pos) == min_dist
52+
cell for cell in candidates if get_distance(self.cell, cell) == min_dist
5653
]
5754
self.random.shuffle(final_candidates)
58-
self.model.grid.move_agent(self, final_candidates[0])
55+
self.cell = final_candidates[0]
5956

6057
def eat(self):
61-
sugar_patch = self.get_sugar(self.pos)
58+
sugar_patch = self.get_sugar(self.cell)
6259
self.sugar = self.sugar - self.metabolism + sugar_patch.amount
6360
sugar_patch.amount = 0
6461

6562
def step(self):
6663
self.move()
6764
self.eat()
6865
if self.sugar <= 0:
69-
self.model.grid.remove_agent(self)
7066
self.remove()
7167

7268

73-
class Sugar(mesa.Agent):
74-
def __init__(self, model, max_sugar):
69+
class Sugar(FixedAgent):
70+
def __init__(self, model, max_sugar, cell):
7571
super().__init__(model)
7672
self.amount = max_sugar
7773
self.max_sugar = max_sugar
74+
self.cell = cell
7875

7976
def step(self):
8077
self.amount = min([self.max_sugar, self.amount + 1])

examples/sugarscape_cg/sugarscape_cg/model.py

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
from pathlib import Path
1313

1414
import mesa
15+
from mesa.experimental.cell_space import OrthogonalVonNeumannGrid
1516

1617
from .agents import SsAgent, Sugar
1718

@@ -23,21 +24,26 @@ class SugarscapeCg(mesa.Model):
2324

2425
verbose = True # Print-monitoring
2526

26-
def __init__(self, width=50, height=50, initial_population=100):
27+
def __init__(self, width=50, height=50, initial_population=100, seed=None):
2728
"""
28-
Create a new Constant Growback model with the given parameters.
29+
Create a new constant grow back model with the given parameters.
2930
3031
Args:
32+
width (int): Width of the Sugarscape 2 Constant Growback model.
33+
height (int): Height of the Sugarscape 2 Constant Growback model.
3134
initial_population: Number of population to start with
35+
seed (int): Seed for the random number generator
36+
3237
"""
33-
super().__init__()
38+
super().__init__(seed=seed)
3439

3540
# Set parameters
3641
self.width = width
3742
self.height = height
3843
self.initial_population = initial_population
3944

40-
self.grid = mesa.space.MultiGrid(self.width, self.height, torus=False)
45+
# self.grid = mesa.space.MultiGrid(self.width, self.height, torus=False)
46+
self.grid = OrthogonalVonNeumannGrid((self.width, self.height), torus=True)
4147
self.datacollector = mesa.DataCollector(
4248
{"SsAgent": lambda m: len(m.agents_by_type[SsAgent])}
4349
)
@@ -46,10 +52,9 @@ def __init__(self, width=50, height=50, initial_population=100):
4652
import numpy as np
4753

4854
sugar_distribution = np.genfromtxt(Path(__file__).parent / "sugar-map.txt")
49-
for _, (x, y) in self.grid.coord_iter():
50-
max_sugar = sugar_distribution[x, y]
51-
sugar = Sugar(self, max_sugar)
52-
self.grid.place_agent(sugar, (x, y))
55+
for cell in self.grid.all_cells:
56+
max_sugar = sugar_distribution[cell.coordinate]
57+
Sugar(self, max_sugar, cell)
5358

5459
# Create agent:
5560
for i in range(self.initial_population):
@@ -58,8 +63,8 @@ def __init__(self, width=50, height=50, initial_population=100):
5863
sugar = self.random.randrange(6, 25)
5964
metabolism = self.random.randrange(2, 4)
6065
vision = self.random.randrange(1, 6)
61-
ssa = SsAgent(self, False, sugar, metabolism, vision)
62-
self.grid.place_agent(ssa, (x, y))
66+
cell = self.grid[(x,y)]
67+
SsAgent(self, cell, sugar, metabolism, vision)
6368

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

examples/sugarscape_g1mt/sugarscape_g1mt/model.py

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
from pathlib import Path
22

33
import mesa
4+
from mesa.experimental.cell_space import OrthogonalVonNeumannGrid
5+
46
import numpy as np
57

68
from .resource_agents import Resource
@@ -69,7 +71,7 @@ def __init__(
6971
self.running = True
7072

7173
# initiate mesa grid class
72-
self.grid = mesa.space.MultiGrid(self.width, self.height, torus=False)
74+
self.grid =OrthogonalVonNeumannGrid((self.width, self.height), torus=False)
7375
# initiate datacollector
7476
self.datacollector = mesa.DataCollector(
7577
model_reporters={
@@ -88,11 +90,11 @@ def __init__(
8890
sugar_distribution = np.genfromtxt(Path(__file__).parent / "sugar-map.txt")
8991
spice_distribution = np.flip(sugar_distribution, 1)
9092

91-
for _, (x, y) in self.grid.coord_iter():
92-
max_sugar = sugar_distribution[x, y]
93-
max_spice = spice_distribution[x, y]
94-
resource = Resource(self, max_sugar, max_spice)
95-
self.grid.place_agent(resource, (x, y))
93+
for cell in self.grid.all_cells:
94+
max_sugar = sugar_distribution[cell.coordinate]
95+
max_spice = spice_distribution[cell.coordinate]
96+
Resource(self, max_sugar, max_spice, cell)
97+
9698

9799
for _ in range(self.initial_population):
98100
# get agent position
@@ -111,18 +113,18 @@ def __init__(
111113
)
112114
# give agents vision
113115
vision = int(self.random.uniform(self.vision_min, self.vision_max + 1))
116+
117+
cell = self.grid[(x, y)]
114118
# create Trader object
115119
trader = Trader(
116120
self,
117-
moore=False,
121+
cell,
118122
sugar=sugar,
119123
spice=spice,
120124
metabolism_sugar=metabolism_sugar,
121125
metabolism_spice=metabolism_spice,
122126
vision=vision,
123127
)
124-
# place agent
125-
self.grid.place_agent(trader, (x, y))
126128

127129
def step(self):
128130
"""

examples/sugarscape_g1mt/sugarscape_g1mt/resource_agents.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,22 @@
11
import mesa
22

3+
from mesa.experimental.cell_space import FixedAgent
34

4-
class Resource(mesa.Agent):
5+
class Resource(FixedAgent):
56
"""
67
Resource:
78
- contains an amount of sugar and spice
89
- grows 1 amount of sugar at each turn
910
- grows 1 amount of spice at each turn
1011
"""
1112

12-
def __init__(self, model, max_sugar, max_spice):
13+
def __init__(self, model, max_sugar, max_spice, cell):
1314
super().__init__(model)
1415
self.sugar_amount = max_sugar
1516
self.max_sugar = max_sugar
1617
self.spice_amount = max_spice
1718
self.max_spice = max_spice
19+
self.cell = cell
1820

1921
def step(self):
2022
"""

0 commit comments

Comments
 (0)