From c9ac90da8385336857e1b27d843b858fda8d248f Mon Sep 17 00:00:00 2001 From: Yavoryna Date: Sat, 12 Oct 2024 12:52:43 +0300 Subject: [PATCH 1/2] =?UTF-8?q?=D0=B8=D0=BF=D1=80=D0=B0=D0=B2=D0=B8=D0=BB?= =?UTF-8?q?=D0=B0=20=D0=B1=D0=B0=D0=B3=D0=B8=20=D0=B2=20SugarscapeCg=20?= =?UTF-8?q?=D0=BC=D0=BE=D0=B4=D0=B5=D0=BB=D1=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../sugarscape_cg/sugarscape_cg/agents.py | 8 ++-- examples/sugarscape_cg/sugarscape_cg/model.py | 38 ++++++++++++------- 2 files changed, 29 insertions(+), 17 deletions(-) diff --git a/examples/sugarscape_cg/sugarscape_cg/agents.py b/examples/sugarscape_cg/sugarscape_cg/agents.py index a75f07c2..df47a619 100644 --- a/examples/sugarscape_cg/sugarscape_cg/agents.py +++ b/examples/sugarscape_cg/sugarscape_cg/agents.py @@ -17,8 +17,8 @@ def get_distance(pos_1, pos_2): class SsAgent(mesa.Agent): - def __init__(self, model, moore=False, sugar=0, metabolism=0, vision=0): - super().__init__(model) + def __init__(self, agent_id, model, moore=False, sugar=0, metabolism=0, vision=0): + super().__init__(agent_id, model) self.moore = moore self.sugar = sugar self.metabolism = metabolism @@ -71,8 +71,8 @@ def step(self): class Sugar(mesa.Agent): - def __init__(self, model, max_sugar): - super().__init__(model) + def __init__(self, agent_id, model, max_sugar): + super().__init__(agent_id, model) self.amount = max_sugar self.max_sugar = max_sugar diff --git a/examples/sugarscape_cg/sugarscape_cg/model.py b/examples/sugarscape_cg/sugarscape_cg/model.py index 667e9b37..4d4abc58 100644 --- a/examples/sugarscape_cg/sugarscape_cg/model.py +++ b/examples/sugarscape_cg/sugarscape_cg/model.py @@ -10,11 +10,9 @@ """ from pathlib import Path - import mesa - from .agents import SsAgent, Sugar - +import numpy as np class SugarscapeCg(mesa.Model): """ @@ -38,40 +36,54 @@ def __init__(self, width=50, height=50, initial_population=100): self.initial_population = initial_population self.grid = mesa.space.MultiGrid(self.width, self.height, torus=False) + + # Initialize agents by type + self.agents_by_type = {SsAgent: [], Sugar: []} + + # Set up data collector self.datacollector = mesa.DataCollector( {"SsAgent": lambda m: len(m.agents_by_type[SsAgent])} ) + self.steps = 0 # Create sugar - import numpy as np - sugar_distribution = np.genfromtxt(Path(__file__).parent / "sugar-map.txt") - for _, (x, y) in self.grid.coord_iter(): + sugar_id = 1 + for (contents, x, y) in self.grid.coord_iter(): max_sugar = sugar_distribution[x, y] - sugar = Sugar(self, max_sugar) + sugar = Sugar(sugar_id, self, max_sugar) self.grid.place_agent(sugar, (x, y)) + self.agents_by_type[Sugar].append(sugar) # Track sugar agents + sugar_id += 1 - # Create agent: + # Create agents + agent_id = 1 for i in range(self.initial_population): x = self.random.randrange(self.width) y = self.random.randrange(self.height) sugar = self.random.randrange(6, 25) metabolism = self.random.randrange(2, 4) vision = self.random.randrange(1, 6) - ssa = SsAgent(self, False, sugar, metabolism, vision) + ssa = SsAgent(agent_id, self, False, sugar, metabolism, vision) self.grid.place_agent(ssa, (x, y)) + self.agents_by_type[SsAgent].append(ssa) # Track SsAgent agents + agent_id += 1 self.running = True self.datacollector.collect(self) def step(self): - # Step suger and agents - self.agents_by_type[Sugar].do("step") - self.agents_by_type[SsAgent].shuffle_do("step") - # collect data + # Step sugar and agents + for sugar in self.agents_by_type[Sugar]: + sugar.step() + for agent in self.agents_by_type[SsAgent]: + agent.step() + + # Collect data self.datacollector.collect(self) if self.verbose: print(f"Step: {self.steps}, SsAgents: {len(self.agents_by_type[SsAgent])}") + self.steps += 1 def run_model(self, step_count=200): if self.verbose: From 616a4a3a28e2e28748dd08c70446180092762158 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sat, 12 Oct 2024 09:56:17 +0000 Subject: [PATCH 2/2] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- examples/sugarscape_cg/sugarscape_cg/model.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/examples/sugarscape_cg/sugarscape_cg/model.py b/examples/sugarscape_cg/sugarscape_cg/model.py index 4d4abc58..3eba713a 100644 --- a/examples/sugarscape_cg/sugarscape_cg/model.py +++ b/examples/sugarscape_cg/sugarscape_cg/model.py @@ -14,6 +14,7 @@ from .agents import SsAgent, Sugar import numpy as np + class SugarscapeCg(mesa.Model): """ Sugarscape 2 Constant Growback @@ -49,7 +50,7 @@ def __init__(self, width=50, height=50, initial_population=100): # Create sugar sugar_distribution = np.genfromtxt(Path(__file__).parent / "sugar-map.txt") sugar_id = 1 - for (contents, x, y) in self.grid.coord_iter(): + for contents, x, y in self.grid.coord_iter(): max_sugar = sugar_distribution[x, y] sugar = Sugar(sugar_id, self, max_sugar) self.grid.place_agent(sugar, (x, y))