From b24c6b10d4229b0e4bfd9e5c6215a319acbd31ef Mon Sep 17 00:00:00 2001 From: rht Date: Wed, 24 Jan 2024 13:56:09 -0500 Subject: [PATCH 1/4] fix: Use resource agent for Sugarscape G1MT server.py --- .../sugarscape_g1mt/sugarscape_g1mt/server.py | 29 +++++++++---------- 1 file changed, 13 insertions(+), 16 deletions(-) diff --git a/examples/sugarscape_g1mt/sugarscape_g1mt/server.py b/examples/sugarscape_g1mt/sugarscape_g1mt/server.py index fbc10ce6..3ef00668 100644 --- a/examples/sugarscape_g1mt/sugarscape_g1mt/server.py +++ b/examples/sugarscape_g1mt/sugarscape_g1mt/server.py @@ -1,7 +1,7 @@ import mesa from .model import SugarscapeG1mt -from .resource_agents import Spice, Sugar +from .resource_agents import Resource from .trader_agents import Trader sugar_dic = {4: "#005C00", 3: "#008300", 2: "#00AA00", 1: "#00F800"} @@ -21,9 +21,18 @@ def Agent_portrayal(agent): "Color": "#FF0A01", } - elif isinstance(agent, Sugar): - color = sugar_dic[agent.amount] if agent.amount != 0 else "#D6F5D6" - layer = 1 if agent.amount > 2 else 0 + elif isinstance(agent, Resource): + resource_type = "sugar" if agent.max_sugar > agent.max_spice else "spice" + if resource_type == "sugar": + color = ( + sugar_dic[agent.sugar_amount] if agent.sugar_amount != 0 else "#D6F5D6" + ) + layer = 1 if agent.sugar_amount > 2 else 0 + else: + color = ( + spice_dic[agent.spice_amount] if agent.spice_amount != 0 else "#D6F5D6" + ) + layer = 1 if agent.spice_amount > 2 else 0 return { "Color": color, "Shape": "rect", @@ -33,18 +42,6 @@ def Agent_portrayal(agent): "h": 1, } - elif isinstance(agent, Spice): - color = spice_dic[agent.amount] if agent.amount != 0 else "#D6F5D6" - layer = 1 if agent.amount > 2 else 0 - return { - "Color": color, - "Shape": "rect", - "Filled": "true", - "Layer": 0, - "w": 1, - "h": 1, - } - return {} From 0675295339da2e54dc3adc7617557f5d9b03a8ca Mon Sep 17 00:00:00 2001 From: rht Date: Mon, 29 Jan 2024 18:58:05 -0500 Subject: [PATCH 2/4] refactor: Remove randomize_traders --- .../sugarscape_g1mt/sugarscape_g1mt/model.py | 19 ++----------------- 1 file changed, 2 insertions(+), 17 deletions(-) diff --git a/examples/sugarscape_g1mt/sugarscape_g1mt/model.py b/examples/sugarscape_g1mt/sugarscape_g1mt/model.py index f88f1c2c..f1b620e3 100644 --- a/examples/sugarscape_g1mt/sugarscape_g1mt/model.py +++ b/examples/sugarscape_g1mt/sugarscape_g1mt/model.py @@ -136,18 +136,6 @@ def __init__( self.schedule.add(trader) agent_id += 1 - def randomize_traders(self): - """ - helper function for self.step() - - puts traders in randomized list for step function - """ - - traders_shuffle = list(self.schedule.agents_by_type[Trader].values()) - self.random.shuffle(traders_shuffle) - - return traders_shuffle - def step(self): """ Unique step function that does staged activation of sugar and spice @@ -160,7 +148,7 @@ def step(self): # step trader agents # to account for agent death and removal we need a seperate data strcuture to # iterate - trader_shuffle = self.randomize_traders() + trader_shuffle = self.get_agents_of_type(Trader).shuffle(inplace=True) for agent in trader_shuffle: agent.prices = [] @@ -175,10 +163,7 @@ def step(self): self.datacollector.collect(self) return - trader_shuffle = self.randomize_traders() - - for agent in trader_shuffle: - agent.trade_with_neighbors() + self.get_agents_of_type(Trader).shuffle(inplace=True).do("trade_with_neighbors") self._steps += 1 # collect model level data From 77d8d79d7340ce371a65cd9d735a4bf467ea0805 Mon Sep 17 00:00:00 2001 From: rht Date: Mon, 29 Jan 2024 19:04:03 -0500 Subject: [PATCH 3/4] refactor: Remove usage of agents_by_type --- examples/sugarscape_g1mt/sugarscape_g1mt/model.py | 9 +++------ examples/sugarscape_g1mt/tests.py | 4 ++-- 2 files changed, 5 insertions(+), 8 deletions(-) diff --git a/examples/sugarscape_g1mt/sugarscape_g1mt/model.py b/examples/sugarscape_g1mt/sugarscape_g1mt/model.py index f1b620e3..080682c5 100644 --- a/examples/sugarscape_g1mt/sugarscape_g1mt/model.py +++ b/examples/sugarscape_g1mt/sugarscape_g1mt/model.py @@ -77,13 +77,10 @@ def __init__( model_reporters={ "Trader": lambda m: m.schedule.get_type_count(Trader), "Trade Volume": lambda m: sum( - len(a.trade_partners) - for a in m.schedule.agents_by_type[Trader].values() + len(a.trade_partners) for a in m.get_agents_of_type(Trader) ), "Price": lambda m: geometric_mean( - flatten( - [a.prices for a in m.schedule.agents_by_type[Trader].values()] - ) + flatten([a.prices for a in m.get_agents_of_type(Trader)]) ), }, agent_reporters={"Trade Network": lambda a: get_trade(a)}, @@ -142,7 +139,7 @@ def step(self): and then randomly activates traders """ # step Resource agents - for resource in self.schedule.agents_by_type[Resource].values(): + for resource in self.get_agents_of_type(Resource): resource.step() # step trader agents diff --git a/examples/sugarscape_g1mt/tests.py b/examples/sugarscape_g1mt/tests.py index bcfcf739..4a308974 100644 --- a/examples/sugarscape_g1mt/tests.py +++ b/examples/sugarscape_g1mt/tests.py @@ -23,7 +23,7 @@ def test_decreasing_price_variance(): model.datacollector._new_model_reporter( "price_variance", lambda m: np.var( - flatten([a.prices for a in m.schedule.agents_by_type[Trader].values()]) + flatten([a.prices for a in m.get_agents_of_type(Trader)]) ), ) model.run_model(step_count=50) @@ -40,7 +40,7 @@ def calculate_carrying_capacities(enable_trade): for vision_max in visions: model = SugarscapeG1mt(vision_max=vision_max, enable_trade=enable_trade) model.run_model(step_count=50) - carrying_capacities.append(len(model.schedule.agents_by_type[Trader])) + carrying_capacities.append(len(model.get_agents_of_type(Trader))) return carrying_capacities # Carrying capacity should increase over mean vision (figure IV-6). From 44cdd1e214c360f47eb72a3ec9616b8df381ef79 Mon Sep 17 00:00:00 2001 From: rht Date: Mon, 29 Jan 2024 19:08:16 -0500 Subject: [PATCH 4/4] refactor: Remove scheduler --- examples/sugarscape_g1mt/sugarscape_g1mt/model.py | 6 +----- examples/sugarscape_g1mt/sugarscape_g1mt/trader_agents.py | 2 +- 2 files changed, 2 insertions(+), 6 deletions(-) diff --git a/examples/sugarscape_g1mt/sugarscape_g1mt/model.py b/examples/sugarscape_g1mt/sugarscape_g1mt/model.py index 080682c5..d96f51d7 100644 --- a/examples/sugarscape_g1mt/sugarscape_g1mt/model.py +++ b/examples/sugarscape_g1mt/sugarscape_g1mt/model.py @@ -68,14 +68,12 @@ def __init__( self.enable_trade = enable_trade self.running = True - # initiate activation schedule - self.schedule = mesa.time.RandomActivationByType(self) # initiate mesa grid class self.grid = mesa.space.MultiGrid(self.width, self.height, torus=False) # initiate datacollector self.datacollector = mesa.DataCollector( model_reporters={ - "Trader": lambda m: m.schedule.get_type_count(Trader), + "Trader": lambda m: len(m.get_agents_of_type(Trader)), "Trade Volume": lambda m: sum( len(a.trade_partners) for a in m.get_agents_of_type(Trader) ), @@ -95,7 +93,6 @@ def __init__( max_sugar = sugar_distribution[x, y] max_spice = spice_distribution[x, y] resource = Resource(agent_id, self, (x, y), max_sugar, max_spice) - self.schedule.add(resource) self.grid.place_agent(resource, (x, y)) agent_id += 1 @@ -130,7 +127,6 @@ def __init__( ) # place agent self.grid.place_agent(trader, (x, y)) - self.schedule.add(trader) agent_id += 1 def step(self): diff --git a/examples/sugarscape_g1mt/sugarscape_g1mt/trader_agents.py b/examples/sugarscape_g1mt/sugarscape_g1mt/trader_agents.py index 96bc8c5b..f5f299d7 100644 --- a/examples/sugarscape_g1mt/sugarscape_g1mt/trader_agents.py +++ b/examples/sugarscape_g1mt/sugarscape_g1mt/trader_agents.py @@ -306,7 +306,7 @@ def maybe_die(self): if self.is_starved(): self.model.grid.remove_agent(self) - self.model.schedule.remove(self) + self.remove() def trade_with_neighbors(self): """