Skip to content

Commit

Permalink
intro tutorial: Update batch run model with agent reporter
Browse files Browse the repository at this point in the history
Add a steps_not_given agent reporter to the model that's used in the batch_run() function. This way we agent plots can be discussed.
  • Loading branch information
EwoutH committed Jan 11, 2024
1 parent 4e39e86 commit 59818a0
Showing 1 changed file with 48 additions and 7 deletions.
55 changes: 48 additions & 7 deletions docs/tutorials/intro_tutorial.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -1003,14 +1003,19 @@
"source": [
"### Batch Run\n",
"\n",
"Like we mentioned above, you usually won't run a model only once, but multiple times, with fixed parameters to find the overall distributions the model generates, and with varying parameters to analyze how they drive the model's outputs and behaviors. Instead of needing to write nested for-loops for each model, Mesa provides a [`batch_run`](https://github.com/projectmesa/mesa/blob/main/mesa/batchrunner.py) function which automates it for you."
"Like we mentioned above, you usually won't run a model only once, but multiple times, with fixed parameters to find the overall distributions the model generates, and with varying parameters to analyze how they drive the model's outputs and behaviors. Instead of needing to write nested for-loops for each model, Mesa provides a [`batch_run`](https://github.com/projectmesa/mesa/blob/main/mesa/batchrunner.py) function which automates it for you.\n",
"\n",
"The batch runner also requires an additional variable `self.running` for the MoneyModel class. This variable enables conditional shut off of the model once a condition is met. In this example it will be set as True indefinitely."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The batch runner also requires an additional variable `self.running` for the MoneyModel class. This variable enables conditional shut off of the model once a condition is met. In this example it will be set as True indefinitely."
"#### Additional agent reporter\n",
"To make the results a little bit more interesting, we will also calculate the number of consecutive time steps an agent hasn't given any wealth as an agent reporter.\n",
"\n",
"This way we can see how data is handled when multiple reporters are used."
]
},
{
Expand Down Expand Up @@ -1047,18 +1052,54 @@
" self.grid.place_agent(a, (x, y))\n",
"\n",
" self.datacollector = mesa.DataCollector(\n",
" model_reporters={\"Gini\": compute_gini}, agent_reporters={\"Wealth\": \"wealth\"}\n",
" model_reporters={\"Gini\": compute_gini},\n",
" agent_reporters={\"Wealth\": \"wealth\", \"Steps_not_given\": \"steps_not_given\"},\n",
" )\n",
"\n",
" def step(self):\n",
" self.datacollector.collect(self)\n",
" self.schedule.step()"
" self.schedule.step()\n",
"\n",
"\n",
"class MoneyAgent(mesa.Agent):\n",
" \"\"\"An agent with fixed initial wealth.\"\"\"\n",
"\n",
" def __init__(self, unique_id, model):\n",
" super().__init__(unique_id, model)\n",
" self.wealth = 1\n",
" self.steps_not_given = 0\n",
"\n",
" def move(self):\n",
" possible_steps = self.model.grid.get_neighborhood(\n",
" self.pos, moore=True, include_center=False\n",
" )\n",
" new_position = self.random.choice(possible_steps)\n",
" self.model.grid.move_agent(self, new_position)\n",
"\n",
" def give_money(self):\n",
" cellmates = self.model.grid.get_cell_list_contents([self.pos])\n",
" if len(cellmates) > 1:\n",
" other = self.random.choice(cellmates)\n",
" other.wealth += 1\n",
" self.wealth -= 1\n",
" self.steps_not_given = 0\n",
" else:\n",
" self.steps_not_given += 1\n",
"\n",
" def step(self):\n",
" self.move()\n",
" if self.wealth > 0:\n",
" self.give_money()\n",
" else:\n",
" self.steps_not_given += 1"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Batch run\n",
"\n",
"We call `batch_run` with the following arguments:\n",
"\n",
"* `model_cls`\n",
Expand Down Expand Up @@ -1229,7 +1270,7 @@
"# Then, print the columns of interest of the filtered data frame\n",
"print(\n",
" one_episode_wealth.to_string(\n",
" index=False, columns=[\"Step\", \"AgentID\", \"Wealth\"], max_rows=25\n",
" index=False, columns=[\"Step\", \"AgentID\", \"Wealth\"], max_rows=10\n",
" )\n",
")\n",
"# For a prettier display we can also convert the data frame to html, uncomment to test in a Jupyter Notebook\n",
Expand All @@ -1253,7 +1294,7 @@
"results_one_episode = results_df[\n",
" (results_df.N == 10) & (results_df.iteration == 1) & (results_df.AgentID == 0)\n",
"]\n",
"print(results_one_episode.to_string(index=False, columns=[\"Step\", \"Gini\"], max_rows=25))"
"print(results_one_episode.to_string(index=False, columns=[\"Step\", \"Gini\"], max_rows=10))"
]
},
{
Expand Down Expand Up @@ -1372,7 +1413,7 @@
")\n",
"g.figure.set_size_inches(8, 4)\n",
"g.set(\n",
" title=\"Average number of consecutive rounds without a transaction for different population sizes\",\n",
" title=\"Average number of consecutive rounds without a transaction for different population sizes\\n(mean with 95% confidence interval)\",\n",
" ylabel=\"Consecutive rounds without a transaction\",\n",
");"
],
Expand Down

0 comments on commit 59818a0

Please sign in to comment.