Skip to content

Commit e596c17

Browse files
committed
Fix bug in tutorial to calculate MRS with potential trade
- update maybe_sell_spice() so calculate_MRS() takes potential sugar and spice based on trade - update calculate_MRS() with kwargs so it can take potential trade but defaults to agents sugar and spice Update increases agent_reporter due to number of trades creating memory issues - Update data collection so non-relevant data rmeoved on each step (i.e. sugar and spice agents don't have trade partners)
1 parent 16bfd95 commit e596c17

File tree

3 files changed

+31
-15
lines changed

3 files changed

+31
-15
lines changed

examples/sugarscape_g1mt/run.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,8 @@ def assess_results(results, single_agent):
8888
params = {
8989
"width": 50,
9090
"height": 50,
91-
"vision_min": range(1, 3),
92-
"metabolism_max": [3, 5],
91+
"vision_min": range(1,4),
92+
"metabolism_max": [2,3,4,5],
9393
}
9494

9595
results_batch = mesa.batch_run(

examples/sugarscape_g1mt/sugarscape_g1mt/model.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,21 @@ def step(self):
187187

188188
# collect model level data
189189
self.datacollector.collect(self)
190+
'''
191+
Mesa is working on updating datacollector agent reporter so it can collect information on specific agents
192+
from mesa.time.RandomActivationByType.
193+
194+
Please see issue #1419 at https://github.com/projectmesa/mesa/issues/1419 (contributions welcome)
195+
196+
Below is one way to update agent_records to get specific Trader agent data
197+
'''
198+
# Need to remove excess data
199+
# Create local variable to store trade data
200+
agent_trades = self.datacollector._agent_records[self.schedule.steps]
201+
# Get rid of all None to reduce data storage needs
202+
agent_trades = [agent for agent in agent_trades if agent[2] is not None]
203+
# Reassign the dictionary value with lean trade data
204+
self.datacollector._agent_records[self.schedule.steps] = agent_trades
190205

191206
def run_model(self, step_count=1000):
192207
for i in range(step_count):

examples/sugarscape_g1mt/sugarscape_g1mt/trader_agents.py

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -140,16 +140,17 @@ def is_starved(self):
140140

141141
return (self.sugar <= 0) or (self.spice <= 0)
142142

143-
def calculate_MRS(self):
144-
"""
145-
Helper function for self.trade()
143+
def calculate_MRS(self, sugar, spice):
144+
'''
145+
Helper function for
146+
- self.trade()
147+
- self.maybe_self_spice()
146148
147-
Determines what trader agent is needs and can give up
148-
"""
149+
Determines what trader agent needs and can give up
150+
'''
149151

150-
return (self.spice / self.metabolism_spice) / (
151-
self.sugar / self.metabolism_sugar
152-
)
152+
return (spice/self.metabolism_spice) / (
153+
sugar/self.metabolism_sugar)
153154

154155
def calculate_sell_spice_amount(self, price):
155156
"""
@@ -205,8 +206,8 @@ def maybe_sell_spice(self, other, price, welfare_self, welfare_other):
205206
welfare_self < self.calculate_welfare(self_sugar, self_spice)
206207
) and (welfare_other < other.calculate_welfare(other_sugar, other_spice))
207208

208-
# trade criteria #2 is their mrs crossing
209-
mrs_not_crossing = self.calculate_MRS() > other.calculate_MRS()
209+
#trade criteria #2 is their mrs crossing with potential trade
210+
mrs_not_crossing = self.calculate_MRS(self_sugar, self_spice) > other.calculate_MRS(other_sugar, other_spice)
210211

211212
if not (both_agents_better_off and mrs_not_crossing):
212213
return False
@@ -229,9 +230,9 @@ def trade(self, other):
229230
assert other.sugar > 0
230231
assert other.spice > 0
231232

232-
# calculate marginal rate of subsitution in Growing Artificial Socieites p. 101
233-
mrs_self = self.calculate_MRS()
234-
mrs_other = other.calculate_MRS()
233+
# calculate marginal rate of substitution in Growing Artificial Societies p. 101
234+
mrs_self = self.calculate_MRS(self.sugar, self.spice)
235+
mrs_other = other.calculate_MRS(other.sugar, other.spice)
235236

236237
# calculate each agents welfare
237238
welfare_self = self.calculate_welfare(self.sugar, self.spice)

0 commit comments

Comments
 (0)