Last active
July 7, 2020 20:16
-
-
Save ericmjl/28205e7673e6231c8f036bd9f95b1300 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"# Troubleshooting batchrunner" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 1, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"from mesa import Agent, Model\n", | |
"from mesa.time import RandomActivation\n", | |
"from mesa.space import MultiGrid\n", | |
"from mesa.datacollection import DataCollector\n", | |
"\n", | |
"class MyAgent(Agent):\n", | |
" def __init__(self, name, model):\n", | |
" super().__init__(name, model)\n", | |
" self.name = name\n", | |
"\n", | |
" def step(self):\n", | |
" pass\n", | |
"\n", | |
"class MyModel(Model):\n", | |
" def __init__(self, n_agents):\n", | |
" super().__init__()\n", | |
" self.schedule = RandomActivation(self)\n", | |
" self.grid = MultiGrid(10, 10, torus=True)\n", | |
" for i in range(n_agents):\n", | |
" a = MyAgent(i, self)\n", | |
" self.schedule.add(a)\n", | |
" coords = (self.random.randrange(0, 10), self.random.randrange(0, 10))\n", | |
" self.grid.place_agent(a, coords)\n", | |
" self.dc = DataCollector(\n", | |
" model_reporters={\n", | |
" \"agent_count\": lambda m: m.schedule.get_agent_count()\n", | |
" },\n", | |
" agent_reporters={\"name\": lambda a: a.name}\n", | |
" )\n", | |
"\n", | |
" def step(self):\n", | |
" self.schedule.step()\n", | |
" self.dc.collect(self)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 2, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stderr", | |
"output_type": "stream", | |
"text": [ | |
"4it [00:00, 1078.92it/s]\n" | |
] | |
} | |
], | |
"source": [ | |
"from mesa.batchrunner import BatchRunner\n", | |
"\n", | |
"parameters = {\"n_agents\": range(1, 5)}\n", | |
"batch_run = BatchRunner(\n", | |
" MyModel, \n", | |
" parameters, \n", | |
" max_steps=10,\n", | |
" model_reporters={\n", | |
" \"n_agents\": lambda m: m.schedule.get_agent_count()\n", | |
" }\n", | |
")\n", | |
"batch_run.run_all()\n" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 3, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/html": [ | |
"<div>\n", | |
"<style scoped>\n", | |
" .dataframe tbody tr th:only-of-type {\n", | |
" vertical-align: middle;\n", | |
" }\n", | |
"\n", | |
" .dataframe tbody tr th {\n", | |
" vertical-align: top;\n", | |
" }\n", | |
"\n", | |
" .dataframe thead th {\n", | |
" text-align: right;\n", | |
" }\n", | |
"</style>\n", | |
"<table border=\"1\" class=\"dataframe\">\n", | |
" <thead>\n", | |
" <tr style=\"text-align: right;\">\n", | |
" <th></th>\n", | |
" <th>n_agents</th>\n", | |
" <th>Run</th>\n", | |
" </tr>\n", | |
" </thead>\n", | |
" <tbody>\n", | |
" <tr>\n", | |
" <th>0</th>\n", | |
" <td>1</td>\n", | |
" <td>0</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>1</th>\n", | |
" <td>2</td>\n", | |
" <td>1</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>2</th>\n", | |
" <td>3</td>\n", | |
" <td>2</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>3</th>\n", | |
" <td>4</td>\n", | |
" <td>3</td>\n", | |
" </tr>\n", | |
" </tbody>\n", | |
"</table>\n", | |
"</div>" | |
], | |
"text/plain": [ | |
" n_agents Run\n", | |
"0 1 0\n", | |
"1 2 1\n", | |
"2 3 2\n", | |
"3 4 3" | |
] | |
}, | |
"execution_count": 3, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"batch_run.get_model_vars_dataframe()" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"_This is incorrect, there should be an index for time step; each model should be run for 10 steps, so I should have 40 rows._\n", | |
"\n", | |
"Additionally, re-declaring the model reporters seems to be repetitive, especially when it's already declared in the Model class definition." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 5, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stderr", | |
"output_type": "stream", | |
"text": [ | |
"4it [00:00, 1453.96it/s]\n" | |
] | |
}, | |
{ | |
"ename": "AttributeError", | |
"evalue": "'BatchRunner' object has no attribute 'model_vars'", | |
"output_type": "error", | |
"traceback": [ | |
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", | |
"\u001b[0;31mAttributeError\u001b[0m Traceback (most recent call last)", | |
"\u001b[0;32m<ipython-input-5-0be3ef67a8a1>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[1;32m 8\u001b[0m )\n\u001b[1;32m 9\u001b[0m \u001b[0mbatch_run\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mrun_all\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 10\u001b[0;31m \u001b[0mbatch_run\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mget_model_vars_dataframe\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", | |
"\u001b[0;32m~/anaconda/envs/prog_forecast/lib/python3.8/site-packages/mesa/batchrunner.py\u001b[0m in \u001b[0;36mget_model_vars_dataframe\u001b[0;34m(self)\u001b[0m\n\u001b[1;32m 211\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 212\u001b[0m \"\"\"\n\u001b[0;32m--> 213\u001b[0;31m \u001b[0;32mreturn\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_prepare_report_table\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mmodel_vars\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 214\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 215\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0mget_agent_vars_dataframe\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", | |
"\u001b[0;31mAttributeError\u001b[0m: 'BatchRunner' object has no attribute 'model_vars'" | |
] | |
} | |
], | |
"source": [ | |
"from mesa.batchrunner import BatchRunner\n", | |
"\n", | |
"parameters = {\"n_agents\": range(1, 5)}\n", | |
"batch_run = BatchRunner(\n", | |
" MyModel, \n", | |
" parameters, \n", | |
" max_steps=10,\n", | |
")\n", | |
"batch_run.run_all()\n", | |
"batch_run.get_model_vars_dataframe()" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"display_name": "prog_forecast", | |
"language": "python", | |
"name": "prog_forecast" | |
}, | |
"language_info": { | |
"codemirror_mode": { | |
"name": "ipython", | |
"version": 3 | |
}, | |
"file_extension": ".py", | |
"mimetype": "text/x-python", | |
"name": "python", | |
"nbconvert_exporter": "python", | |
"pygments_lexer": "ipython3", | |
"version": "3.8.1" | |
}, | |
"widgets": { | |
"application/vnd.jupyter.widget-state+json": { | |
"state": {}, | |
"version_major": 2, | |
"version_minor": 0 | |
} | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 4 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment