From fd0af559775bcf93b8fe965f5299c96660e69fb5 Mon Sep 17 00:00:00 2001 From: Wang Boyu Date: Sat, 23 Nov 2024 20:55:36 -0500 Subject: [PATCH] replace mesa scheduler with agentset in intro tutorial --- docs/tutorials/intro_tutorial.ipynb | 40 ++++++++++++----------------- 1 file changed, 17 insertions(+), 23 deletions(-) diff --git a/docs/tutorials/intro_tutorial.ipynb b/docs/tutorials/intro_tutorial.ipynb index 40ee8218..128adb50 100644 --- a/docs/tutorials/intro_tutorial.ipynb +++ b/docs/tutorials/intro_tutorial.ipynb @@ -122,7 +122,6 @@ "has_explanation": false }, "source": [ - "\n", "The first thing we are going to do is create the person agent class. This class has several attributes necessary to make a more holistic model. \n", "\n", "First, there are the required attributes for any GeoAgent in Mesa-Geo\n", @@ -143,7 +142,7 @@ "\n", "The **`__repr__`** function is a Python primitive that will print out information as directed by the code. In this case we will print out the agent ID\n", "\n", - "The **step** function is a Mesa primitive that the scheduler looks for and describes what action the agent takes each step" + "The **step** function is a Mesa primitive that describes what action the agent takes each step" ] }, { @@ -225,7 +224,7 @@ "\n", "We will also use the **`__repr__`** function to print out the agent ID\n", "\n", - "Then the **step** function, which is a primitive that the Mesa scheduler looks for and describes what action the agent takes each step" + "Then the **step** function, which is a primitive that describes what action the agent takes each step" ] }, { @@ -287,7 +286,7 @@ "\n", "**Creating the NeighbourhoodAgents**\n", "\n", - "In this case we will use the `torontoneighbourhoods.geojson` file located in the data folder to to create the NeighbourhoodAgents. Next, we will add them to the environment with the space.add_agents function. Then we will iterate through each of the NeighbourhoodAgents to add them to the schedule. \n", + "In this case we will use the `torontoneighbourhoods.geojson` file located in the data folder to to create the NeighbourhoodAgents. Next, we will add them to the environment with the space.add_agents function.\n", "\n", "\n", "**Creating the PersonAgents**\n", @@ -308,11 +307,11 @@ "\n", "**Step Function**\n", "\n", - "The final piece is to initialize a step function. This function a Mesa primitive calls the RandomActiviationByType scheduler we set up and then iterates through each agent calling their step function. \n", + "The final piece is to initialize a step function. This function is a Mesa primitive that iterates through each agent calling their step function.\n", "\n", "**The Model**\n", "\n", - "We know have the pieces of our Model. A GIS layer of polygons that creates NeighbourhoodAgents from our GeoJSON file. A diverse population of GIS Point objects, with different infection, recovery and death risks. A model class that initializes these agents, a scheduler to call these agents, a GIS space and step function to execute the simulation\n" + "We know have the pieces of our Model. A GIS layer of polygons that creates NeighbourhoodAgents from our GeoJSON file. A diverse population of GIS Point objects, with different infection, recovery and death risks. A model class that initializes these agents, a GIS space and step function to execute the simulation" ] }, { @@ -340,7 +339,6 @@ " max_recovery_time=5,\n", " ):\n", " super().__init__()\n", - " self.schedule = mesa.time.RandomActivationByType(self)\n", " self.space = mg.GeoSpace(warn_crs_conversion=False)\n", "\n", " # SIR model parameters\n", @@ -358,11 +356,7 @@ " # Add neighbourhood agents to space\n", " self.space.add_agents(neighbourhood_agents)\n", "\n", - " # Add neighbourhood agents to scheduler\n", - " for agent in neighbourhood_agents:\n", - " self.schedule.add(agent)\n", - "\n", - " # Generate random location, add agent to grid and scheduler\n", + " # Generate random location, add agent to grid\n", " for i in range(pop_size):\n", " # assess if they are infected\n", " if self.random.random() < self.initial_infection:\n", @@ -396,7 +390,6 @@ "\n", " this_person = unique_person.create_agent(Point(x_home, y_home))\n", " self.space.add_agents(this_person)\n", - " self.schedule.add(this_person)\n", "\n", " def find_home(self, neighbourhood_agents):\n", " \"\"\"Find start location of agent\"\"\"\n", @@ -420,7 +413,10 @@ "\n", " def step(self):\n", " \"\"\"Run one step of the model.\"\"\"\n", - " self.schedule.step()" + " # Activate PersonAgents in random order\n", + " self.agents_by_type[PersonAgent].shuffle_do(\"step\")\n", + " # For NeighbourhoodAgents the order doesn't matter, since they update independently from each other\n", + " self.agents_by_type[NeighbourhoodAgent].do(\"step\")" ] }, { @@ -766,8 +762,6 @@ " max_recovery_time=5,\n", " ):\n", " super().__init__()\n", - " # Scheduler\n", - " self.schedule = mesa.time.RandomActivationByType(self)\n", " # Space\n", " self.space = mg.GeoSpace(warn_crs_conversion=False)\n", " # Data Collection\n", @@ -801,11 +795,7 @@ " # Add neighbourhood agents to space\n", " self.space.add_agents(neighbourhood_agents)\n", "\n", - " # Add neighbourhood agents to scheduler\n", - " for agent in neighbourhood_agents:\n", - " self.schedule.add(agent)\n", - "\n", - " # Generate random location, add agent to grid and scheduler\n", + " # Generate random location, add agent to grid\n", " for i in range(pop_size):\n", " # assess if they are infected\n", " if self.random.random() < self.initial_infection:\n", @@ -839,7 +829,6 @@ "\n", " this_person = unique_person.create_agent(Point(x_home, y_home))\n", " self.space.add_agents(this_person)\n", - " self.schedule.add(this_person)\n", "\n", " def find_home(self, neighbourhood_agents):\n", " \"\"\"Find start location of agent\"\"\"\n", @@ -876,7 +865,12 @@ " \"\"\"Run one step of the model.\"\"\"\n", "\n", " self.reset_counts() # added\n", - " self.schedule.step()\n", + " \n", + " # Activate PersonAgents in random order\n", + " self.agents_by_type[PersonAgent].shuffle_do(\"step\")\n", + " # For NeighbourhoodAgents the order doesn't matter, since they update independently from each other\n", + " self.agents_by_type[NeighbourhoodAgent].do(\"step\")\n", + " \n", " self.datacollector.collect(self) # added\n", "\n", " # Run until no one is infected\n",