From 84542d4c8dac6c7c9d9ebf0dcd5c9748caac4529 Mon Sep 17 00:00:00 2001 From: Ewout ter Hoeven Date: Wed, 25 Sep 2024 21:16:19 +0200 Subject: [PATCH] gis/GeoSir: Replace BaseScheduler with AgentSet functionality (#210) Replace BaseScheduler in the GeoSir gis model with AgentSet functionality, using `agents_by_type`, `shuffle_do()` and `do()`. - Remove BaseScheduler initialization and usage - Use automatic agent registration for PersonAgents - Explicitly register NeighbourhoodAgents with the model - Update step() method to use AgentSet methods for agent activation - Maintain original activation order: PersonAgents (shuffled) then NeighbourhoodAgents - Remove unnecessary scheduler.add() calls --- gis/geo_sir/model.py | 17 ++++++----------- 1 file changed, 6 insertions(+), 11 deletions(-) diff --git a/gis/geo_sir/model.py b/gis/geo_sir/model.py index de3c1cc0..b7baa2fa 100644 --- a/gis/geo_sir/model.py +++ b/gis/geo_sir/model.py @@ -29,7 +29,6 @@ def __init__( if it has been exposed to another infected """ super().__init__() - self.schedule = mesa.time.BaseScheduler(self) self.space = mg.GeoSpace(warn_crs_conversion=False) self.steps = 0 self.counts = None @@ -52,7 +51,6 @@ def __init__( ) # Set up the Neighbourhood patches for every region in file - # (add to schedule later) ac = mg.AgentCreator(NeighbourhoodAgent, model=self) neighbourhood_agents = ac.from_file(self.geojson_regions) self.space.add_agents(neighbourhood_agents) @@ -64,7 +62,7 @@ def __init__( crs=self.space.crs, agent_kwargs={"init_infected": init_infected}, ) - # Generate random location, add agent to grid and scheduler + # Generate random location and add agent to grid for i in range(pop_size): this_neighbourhood = self.random.randint( 0, len(neighbourhood_agents) - 1 @@ -81,12 +79,6 @@ def __init__( this_y = center_y[0] + self.random.randint(0, spread_y) - spread_y / 2 this_person = ac_population.create_agent(Point(this_x, this_y)) self.space.add_agents(this_person) - self.schedule.add(this_person) - - # Add the neighbourhood agents to schedule AFTER person agents, - # to allow them to update their color by using BaseScheduler - for agent in neighbourhood_agents: - self.schedule.add(agent) self.datacollector.collect(self) @@ -102,9 +94,12 @@ def reset_counts(self): def step(self): """Run one step of the model.""" - self.steps += 1 self.reset_counts() - self.schedule.step() + + # Activate PersonAgents in random order + self.agents_by_type[PersonAgent].shuffle_do("step") + # For NeighbourhoodAgents the order doesn't matter, since they update independently from each other + self.agents_by_type[NeighbourhoodAgent].do("step") self.datacollector.collect(self)