Skip to content

Commit 7275b17

Browse files
rhttpike3
authored andcommitted
refactor: Simplify Sugarscape resource agents
This combines the Sugar and Spice resources into 1 resource class.
1 parent 654f832 commit 7275b17

File tree

3 files changed

+31
-99
lines changed

3 files changed

+31
-99
lines changed

examples/sugarscape_g1mt/sugarscape_g1mt/model.py

Lines changed: 8 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import mesa
22
import numpy as np
33

4-
from .resource_agents import Spice, Sugar
4+
from .resource_agents import Resource
55
from .trader_agents import Trader
66

77

@@ -93,18 +93,11 @@ def __init__(
9393
agent_id = 0
9494
for _, (x, y) in self.grid.coord_iter():
9595
max_sugar = sugar_distribution[x, y]
96-
if max_sugar > 0:
97-
sugar = Sugar(agent_id, self, (x, y), max_sugar)
98-
self.schedule.add(sugar)
99-
self.grid.place_agent(sugar, (x, y))
100-
agent_id += 1
101-
10296
max_spice = spice_distribution[x, y]
103-
if max_spice > 0:
104-
spice = Spice(agent_id, self, (x, y), max_spice)
105-
self.schedule.add(spice)
106-
self.grid.place_agent(spice, (x, y))
107-
agent_id += 1
97+
resource = Resource(agent_id, self, (x, y), max_sugar, max_spice)
98+
self.schedule.add(resource)
99+
self.grid.place_agent(resource, (x, y))
100+
agent_id += 1
108101

109102
for i in range(self.initial_population):
110103
# get agent position
@@ -157,13 +150,9 @@ def step(self):
157150
Unique step function that does staged activation of sugar and spice
158151
and then randomly activates traders
159152
"""
160-
# step Sugar agents
161-
for sugar in self.schedule.agents_by_type[Sugar].values():
162-
sugar.step()
163-
164-
# step Spice agents
165-
for spice in self.schedule.agents_by_type[Spice].values():
166-
spice.step()
153+
# step Resource agents
154+
for resource in self.schedule.agents_by_type[Resource].values():
155+
resource.step()
167156

168157
# step trader agents
169158
# to account for agent death and removal we need a seperate data strcuture to
Lines changed: 10 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,26 @@
11
import mesa
22

33

4-
class Sugar(mesa.Agent):
4+
class Resource(mesa.Agent):
55
"""
6-
Sugar:
7-
- contains an amount of sugar
6+
Resource:
7+
- contains an amount of sugar and spice
88
- grows 1 amount of sugar at each turn
9-
"""
10-
11-
def __init__(self, unique_id, model, pos, max_sugar):
12-
super().__init__(unique_id, model)
13-
self.pos = pos
14-
self.amount = max_sugar
15-
self.max_sugar = max_sugar
16-
17-
def step(self):
18-
"""
19-
Sugar growth function, adds one unit of sugar each step until
20-
max amount
21-
"""
22-
self.amount = min([self.max_sugar, self.amount + 1])
23-
24-
25-
class Spice(mesa.Agent):
26-
"""
27-
Spice:
28-
- contains an amount of spice
299
- grows 1 amount of spice at each turn
3010
"""
3111

32-
def __init__(self, unique_id, model, pos, max_spice):
12+
def __init__(self, unique_id, model, pos, max_sugar, max_spice):
3313
super().__init__(unique_id, model)
3414
self.pos = pos
35-
self.amount = max_spice
15+
self.sugar_amount = max_sugar
16+
self.max_sugar = max_sugar
17+
self.spice_amount = max_spice
3618
self.max_spice = max_spice
3719

3820
def step(self):
3921
"""
40-
Spice growth function, adds one unit of spice each step until
22+
Growth function, adds one unit of sugar and spice each step up to
4123
max amount
4224
"""
43-
self.amount = min([self.max_spice, self.amount + 1])
25+
self.sugar_amount = min([self.max_sugar, self.sugar_amount + 1])
26+
self.spice_amount = min([self.max_spice, self.spice_amount + 1])

examples/sugarscape_g1mt/sugarscape_g1mt/trader_agents.py

Lines changed: 13 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import mesa
44

5-
from .resource_agents import Spice, Sugar
5+
from .resource_agents import Resource
66

77

88
# Helper function
@@ -50,47 +50,12 @@ def __init__(
5050
self.prices = []
5151
self.trade_partners = []
5252

53-
def get_sugar(self, pos):
54-
"""
55-
used in self.get_sugar_amount()
56-
"""
57-
58-
this_cell = self.model.grid.get_cell_list_contents(pos)
59-
for agent in this_cell:
60-
if type(agent) is Sugar:
61-
return agent
62-
return None
63-
64-
def get_sugar_amount(self, pos):
65-
"""
66-
used in self.move() as part of self.calculate_welfare()
67-
"""
68-
69-
sugar_patch = self.get_sugar(pos)
70-
if sugar_patch:
71-
return sugar_patch.amount
72-
return 0
73-
74-
def get_spice(self, pos):
75-
"""
76-
used in self.get_spice_amount()
77-
"""
78-
53+
def get_resource(self, pos):
7954
this_cell = self.model.grid.get_cell_list_contents(pos)
8055
for agent in this_cell:
81-
if type(agent) is Spice:
56+
if type(agent) is Resource:
8257
return agent
83-
return None
84-
85-
def get_spice_amount(self, pos):
86-
"""
87-
used in self.move() as part of self.calculate_welfare()
88-
"""
89-
90-
spice_patch = self.get_spice(pos)
91-
if spice_patch:
92-
return spice_patch.amount
93-
return 0
58+
raise Exception(f"Resource agent not found in the position {pos}")
9459

9560
def get_trader(self, pos):
9661
"""
@@ -292,8 +257,8 @@ def move(self):
292257

293258
welfares = [
294259
self.calculate_welfare(
295-
self.sugar + self.get_sugar_amount(pos),
296-
self.spice + self.get_spice_amount(pos),
260+
self.sugar + self.get_resource(pos).sugar_amount,
261+
self.spice + self.get_resource(pos).spice_amount,
297262
)
298263
for pos in neighbors
299264
]
@@ -323,20 +288,15 @@ def move(self):
323288
self.model.grid.move_agent(self, final_candidate)
324289

325290
def eat(self):
326-
# get sugar
327-
sugar_patch = self.get_sugar(self.pos)
328-
329-
if sugar_patch:
330-
self.sugar += sugar_patch.amount
331-
sugar_patch.amount = 0
291+
patch = self.get_resource(self.pos)
292+
if patch.sugar_amount > 0:
293+
self.sugar += patch.sugar_amount
294+
patch.sugar_amount = 0
332295
self.sugar -= self.metabolism_sugar
333296

334-
# get_spice
335-
spice_patch = self.get_spice(self.pos)
336-
337-
if spice_patch:
338-
self.spice += spice_patch.amount
339-
spice_patch.amount = 0
297+
if patch.spice_amount > 0:
298+
self.spice += patch.spice_amount
299+
patch.spice_amount = 0
340300
self.spice -= self.metabolism_spice
341301

342302
def maybe_die(self):

0 commit comments

Comments
 (0)