Skip to content

Commit 9f3ceb9

Browse files
authored
Fix bug in tutorial to calculate MRS with potential trade (#33)
- 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 9f3ceb9

File tree

3 files changed

+33
-13
lines changed

3 files changed

+33
-13
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: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,24 @@ def step(self):
187187

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

191209
def run_model(self, step_count=1000):
192210
for i in range(step_count):

examples/sugarscape_g1mt/sugarscape_g1mt/trader_agents.py

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

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

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

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

154154
def calculate_sell_spice_amount(self, price):
155155
"""
@@ -205,8 +205,10 @@ def maybe_sell_spice(self, other, price, welfare_self, welfare_other):
205205
welfare_self < self.calculate_welfare(self_sugar, self_spice)
206206
) and (welfare_other < other.calculate_welfare(other_sugar, other_spice))
207207

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

211213
if not (both_agents_better_off and mrs_not_crossing):
212214
return False
@@ -229,9 +231,9 @@ def trade(self, other):
229231
assert other.sugar > 0
230232
assert other.spice > 0
231233

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

236238
# calculate each agents welfare
237239
welfare_self = self.calculate_welfare(self.sugar, self.spice)

0 commit comments

Comments
 (0)