Skip to content

Commit

Permalink
replace mesa scheduler with agentset in intro tutorial
Browse files Browse the repository at this point in the history
  • Loading branch information
wang-boyu committed Nov 24, 2024
1 parent f819dd5 commit fd0af55
Showing 1 changed file with 17 additions and 23 deletions.
40 changes: 17 additions & 23 deletions docs/tutorials/intro_tutorial.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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"
]
},
{
Expand Down Expand Up @@ -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"
]
},
{
Expand Down Expand Up @@ -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",
Expand All @@ -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"
]
},
{
Expand Down Expand Up @@ -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",
Expand All @@ -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",
Expand Down Expand Up @@ -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",
Expand All @@ -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\")"
]
},
{
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down

0 comments on commit fd0af55

Please sign in to comment.