Skip to content

Commit

Permalink
Fix bug in tutorial to calculate MRS with potential trade
Browse files Browse the repository at this point in the history
- 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)
  • Loading branch information
tpike3 committed May 1, 2023
1 parent 16bfd95 commit 4d72cba
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 12 deletions.
7 changes: 7 additions & 0 deletions examples/sugarscape_g1mt/sugarscape_g1mt/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,13 @@ def step(self):

# collect model level data
self.datacollector.collect(self)
# Need to remove excess data
# Create local variable to store trade data
agent_trades = self.datacollector._agent_records[self.schedule.steps]
# Get rid of all None to reduce data storage needs
agent_trades = [agent for agent in agent_trades if agent[2] is not None]
# Reassign the dictionary value with lean trade data
self.datacollector._agent_records[self.schedule.steps] = agent_trades

def run_model(self, step_count=1000):
for i in range(step_count):
Expand Down
29 changes: 17 additions & 12 deletions examples/sugarscape_g1mt/sugarscape_g1mt/trader_agents.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,16 +140,21 @@ def is_starved(self):

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

def calculate_MRS(self):
"""
Helper function for self.trade()
Determines what trader agent is needs and can give up
"""

return (self.spice / self.metabolism_spice) / (
self.sugar / self.metabolism_sugar
)
def calculate_MRS(self, sugar= None, spice = None):
'''
Helper function for
- self.trade()
- self.maybe_self_spice()
Determines what trader agent needs and can give up
'''
if sugar == None:
sugar = self.sugar
if spice == None:
spice = self.spice

return (spice/self.metabolism_spice) / (
sugar/self.metabolism_sugar)

def calculate_sell_spice_amount(self, price):
"""
Expand Down Expand Up @@ -205,8 +210,8 @@ def maybe_sell_spice(self, other, price, welfare_self, welfare_other):
welfare_self < self.calculate_welfare(self_sugar, self_spice)
) and (welfare_other < other.calculate_welfare(other_sugar, other_spice))

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

if not (both_agents_better_off and mrs_not_crossing):
return False
Expand Down

0 comments on commit 4d72cba

Please sign in to comment.