Skip to content

Commit

Permalink
gis/GeoSir: Replace BaseScheduler with AgentSet functionality (#210)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
EwoutH authored Sep 25, 2024
1 parent d4b18cd commit 84542d4
Showing 1 changed file with 6 additions and 11 deletions.
17 changes: 6 additions & 11 deletions gis/geo_sir/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand All @@ -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
Expand All @@ -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)

Expand All @@ -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)

Expand Down

0 comments on commit 84542d4

Please sign in to comment.