Skip to content

Commit

Permalink
gis: Replace schedulers with AgentSet functionality (#207)
Browse files Browse the repository at this point in the history
  • Loading branch information
EwoutH authored Sep 25, 2024
1 parent 2c05149 commit de5cda9
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 29 deletions.
13 changes: 6 additions & 7 deletions gis/agents_and_networks/src/model/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,21 @@ def get_time(model) -> pd.Timedelta:

def get_num_commuters_by_status(model, status: str) -> int:
commuters = [
commuter for commuter in model.schedule.agents if commuter.status == status
commuter
for commuter in model.agents_by_type[Commuter]
if commuter.status == status
]
return len(commuters)


def get_total_friendships_by_type(model, friendship_type: str) -> int:
if friendship_type == "home":
num_friendships = [
commuter.num_home_friends for commuter in model.schedule.agents
commuter.num_home_friends for commuter in model.agents_by_type[Commuter]
]
elif friendship_type == "work":
num_friendships = [
commuter.num_work_friends for commuter in model.schedule.agents
commuter.num_work_friends for commuter in model.agents_by_type[Commuter]
]
else:
raise ValueError(
Expand All @@ -45,7 +47,6 @@ def get_total_friendships_by_type(model, friendship_type: str) -> int:

class AgentsAndNetworks(mesa.Model):
running: bool
schedule: mesa.time.RandomActivation
show_walkway: bool
show_lakes_and_rivers: bool
current_id: int
Expand Down Expand Up @@ -82,7 +83,6 @@ def __init__(
show_driveway=False,
) -> None:
super().__init__()
self.schedule = mesa.time.RandomActivation(self)
self.show_walkway = show_walkway
self.show_lakes_and_rivers = show_lakes_and_rivers
self.data_crs = data_crs
Expand Down Expand Up @@ -144,7 +144,6 @@ def _create_commuters(self) -> None:
commuter.set_work(random_work)
commuter.status = "home"
self.space.add_commuter(commuter)
self.schedule.add(commuter)

def _load_buildings_from_file(
self, buildings_file: str, crs: str, campus: str
Expand Down Expand Up @@ -215,7 +214,7 @@ def _set_building_entrance(self) -> None:

def step(self) -> None:
self.__update_clock()
self.schedule.step()
self.agents.shuffle_do("step")
self.datacollector.collect(self)

def __update_clock(self) -> None:
Expand Down
11 changes: 4 additions & 7 deletions gis/geo_schelling/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,11 @@ def step(self):
if similar < different:
# Select an empty region
empties = [a for a in self.model.space.agents if a.atype is None]
# Switch atypes and add/remove from scheduler
# Switch atypes
new_region = random.choice(empties)
new_region.atype = self.atype
self.model.schedule.add(new_region)
self.atype = None
self.model.schedule.remove(self)
self.remove()
else:
self.model.happy += 1

Expand All @@ -77,7 +76,6 @@ def __init__(self, density=0.6, minority_pc=0.2, export_data=False):
self.minority_pc = minority_pc
self.export_data = export_data

self.schedule = mesa.time.RandomActivation(self)
self.space = mg.GeoSpace(warn_crs_conversion=False)

self.happy = 0
Expand All @@ -100,7 +98,6 @@ def __init__(self, density=0.6, minority_pc=0.2, export_data=False):
agent.atype = 1
else:
agent.atype = 0
self.schedule.add(agent)

def export_agents_to_file(self) -> None:
self.space.get_agents_as_GeoDataFrame(agent_cls=SchellingAgent).to_crs(
Expand All @@ -113,10 +110,10 @@ def step(self):
If All agents are happy, halt the model.
"""
self.happy = 0 # Reset counter of happy agents
self.schedule.step()
self.agents.shuffle_do("step")
self.datacollector.collect(self)

if self.happy == self.schedule.get_agent_count():
if self.happy == len(self.agents):
self.running = False

if not self.running and self.export_data:
Expand Down
4 changes: 1 addition & 3 deletions gis/geo_schelling_points/geo_schelling_points/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ def __init__(self, red_percentage=0.5, similarity_threshold=0.5):
self.red_percentage = red_percentage
PersonAgent.SIMILARITY_THRESHOLD = similarity_threshold

self.schedule = mesa.time.RandomActivation(self)
self.space = Nuts2Eu()

self.datacollector = mesa.DataCollector(
Expand All @@ -40,7 +39,6 @@ def __init__(self, red_percentage=0.5, similarity_threshold=0.5):
region_id=region.unique_id,
)
self.space.add_person_to_region(person, region_id=region.unique_id)
self.schedule.add(person)

self.datacollector.collect(self)

Expand All @@ -57,7 +55,7 @@ def happy(self):
return self.space.num_people - self.unhappy

def step(self):
self.schedule.step()
self.agents.shuffle_do("step")
self.datacollector.collect(self)

if not self.unhappy:
Expand Down
4 changes: 1 addition & 3 deletions gis/population/population/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,6 @@ def __init__(
Person.MOBILITY_RANGE_X = pixel_size_x / 2.0
Person.MOBILITY_RANGE_Y = pixel_size_y / 2.0

self.schedule = mesa.time.RandomActivation(self)
self._create_agents()

def _create_agents(self):
Expand All @@ -90,7 +89,6 @@ def _create_agents(self):
)
person.set_random_world_coord()
self.space.add_agents(person)
self.schedule.add(person)

def step(self):
self.schedule.step()
self.agents.shuffle_do("step")
9 changes: 3 additions & 6 deletions gis/rainfall/rainfall/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ def pos(self, pos):

def step(self):
if self.is_at_boundary:
self.model.schedule.remove(self)
self.remove()
else:
lowest_pos = min(
self.model.space.raster_layer.get_neighboring_cells(
Expand All @@ -65,7 +65,6 @@ def __init__(self, rain_rate=500, water_height=5, export_data=False, num_steps=2
self.num_steps = num_steps

self.space = CraterLake(crs="epsg:4326", water_height=water_height, model=self)
self.schedule = mesa.time.RandomActivation(self)
self.datacollector = mesa.DataCollector(
{
"Total Amount of Water": "water_amount",
Expand Down Expand Up @@ -101,10 +100,9 @@ def step(self):
pos=(random_x, random_y),
)
self.space.add_raindrop(raindrop)
self.schedule.add(raindrop)
self.water_amount += 1

self.schedule.step()
self.agents.shuffle_do("step")
self.datacollector.collect(self)

current_water_level = self.space.raster_layer.get_raster("water_level")
Expand All @@ -113,8 +111,7 @@ def step(self):
"water_level_normalized",
)

self.num_steps -= 1
if self.num_steps == 0:
if self.steps >= self.num_steps:
self.running = False
if not self.running and self.export_data:
self.export_water_level_to_file()
4 changes: 1 addition & 3 deletions gis/urban_growth/urban_growth/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ def __init__(
self.slope_coefficient = slope_coefficient
self.critical_slope = critical_slope
self.road_influence = road_influence
self.schedule = mesa.time.RandomActivation(self)

self.dispersion_value = (dispersion_coefficient * 0.005) * (
world_width**2 + world_height**2
Expand All @@ -52,7 +51,6 @@ def __init__(
cell.road_found = False
cell.road_pixel = None
cell.model = self
self.schedule.add(cell)

self.initialize_data_collector(
model_reporters={"Percentage Urbanized": "pct_urbanized"}
Expand Down Expand Up @@ -110,7 +108,7 @@ def _check_suitability(self) -> None:

def step(self):
self._spontaneous_growth()
self.schedule.step()
self.agents.shuffle_do("step")
if self.road_influence:
self._road_influenced_growth()
self.datacollector.collect(self)
Expand Down

0 comments on commit de5cda9

Please sign in to comment.