Skip to content

Instantly share code, notes, and snippets.

@tpike3
Created November 11, 2016 19:45
Show Gist options
  • Save tpike3/bcb34adc9de2f4247d7354bf902e7872 to your computer and use it in GitHub Desktop.
Save tpike3/bcb34adc9de2f4247d7354bf902e7872 to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"from mesa import Agent, Model\n",
"from mesa.time import RandomActivation\n",
"import random\n",
"%matplotlib inline\n",
"import matplotlib.pyplot as plt\n",
"from mesa.space import MultiGrid\n",
"import numpy as np\n",
"from mesa.datacollection import DataCollector\n",
"from mesa.batchrunner import BatchRunner\n",
"from mesa.visualization.modules import CanvasGrid\n",
"from mesa.visualization.ModularVisualization import ModularServer\n",
"from mesa.visualization.modules import ChartModule\n"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"def compute_gini(model):\n",
" agent_wealths = [agent.wealth for agent in model.schedule.agents]\n",
" x = sorted(agent_wealths)\n",
" N = model.num_agents\n",
" B = sum( xi * (N-i) for i,xi in enumerate(x) ) / (N*sum(x))\n",
" return (1 + (1/N) - 2*B)\n"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"class MoneyAgent(Agent):\n",
" \"\"\"An agent with fixed initial wealth.\"\"\"\n",
" def __init__(self, unique_id, model):\n",
" super().__init__(unique_id, model)\n",
" self.wealth = 1\n",
" \n",
" # agent move to cell based on moore neighborhood\n",
" def move(self):\n",
" possible_steps = self.model.grid.get_neighborhood(\n",
" self.pos,\n",
" moore=True,\n",
" include_center=False)\n",
" new_position = random.choice(possible_steps)\n",
" self.model.grid.move_agent(self, new_position) \n",
" \n",
" # interact with agents in grid\n",
" def give_money(self):\n",
" cellmates = self.model.grid.get_cell_list_contents([self.pos])\n",
" if len(cellmates) > 1:\n",
" other = random.choice(cellmates)\n",
" other.wealth += 1\n",
" self.wealth -= 1\n",
" \n",
" # Agent steps \n",
" def step(self):\n",
" self.move()\n",
" if self.wealth > 0:\n",
" self.give_money()\n",
" \n",
" \n"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"class MoneyModel(Model):\n",
" \"\"\"A model with some number of agents.\"\"\"\n",
" def __init__(self, N, width, height):\n",
" self.num_agents = N\n",
" self.grid = MultiGrid(width, height, True)\n",
" self.schedule = RandomActivation(self)\n",
" self.running = True\n",
" \n",
" # Create agents\n",
" for i in range(self.num_agents):\n",
" a = MoneyAgent(i, self)\n",
" self.schedule.add(a)\n",
" \n",
" # Add the agent to a random grid cell\n",
" x = random.randrange(self.grid.width)\n",
" y = random.randrange(self.grid.height)\n",
" self.grid.place_agent(a, (x, y))\n",
" \n",
" self.datacollector = DataCollector(\n",
" model_reporters={\"Gini\": compute_gini},\n",
" agent_reporters={\"Wealth\": lambda a: a.wealth})\n",
" \n",
" def step(self):\n",
" self.datacollector.collect(self)\n",
" '''Advance the model by one step.'''\n",
" self.schedule.step()\n",
" \n",
" \n"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"model = MoneyModel(50, 10, 10)\n",
"for i in range(20):\n",
" model.step()"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"<matplotlib.colorbar.Colorbar at 0x220a548d828>"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
},
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAZoAAAFkCAYAAAAZnv0KAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAAPYQAAD2EBqD+naQAAIABJREFUeJzt3X2UZVV55/HvrwsGgi+YDMqbuByiEJkmGFDUEDQOKopL\njRlHacyIjagojg5m4stSFygromYA32CEaARMJKKTrEjigINvGUUlgBBbQFcURHnTFm0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",
"text/plain": [
"<matplotlib.figure.Figure at 0x220a539e550>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"\n",
"agent_counts = np.zeros((model.grid.width, model.grid.height))\n",
"for cell in model.grid.coord_iter():\n",
" cell_content, x, y = cell\n",
" agent_count = len(cell_content)\n",
" agent_counts[x][y] = agent_count\n",
"plt.imshow(agent_counts, interpolation='nearest')\n",
"plt.colorbar()\n"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"model = MoneyModel(50, 10, 10)\n",
"for i in range(100):\n",
" model.step()"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"def agent_portrayal(agent):\n",
" portrayal = {\"Shape\": \"circle\",\n",
" \"Color\": \"red\",\n",
" \"Filled\": \"true\",\n",
" \"Layer\": 0,\n",
" \"r\": 0.5}\n",
" \n",
" \n",
" if agent.wealth > 0:\n",
" portrayal[\"Color\"] = \"red\"\n",
" portrayal[\"Layer\"] = 0\n",
" else:\n",
" portrayal[\"Color\"] = \"grey\"\n",
" portrayal[\"Layer\"] = 1\n",
" portrayal[\"r\"] = 0.2\n",
" return portrayal\n",
" \n",
" \n",
" return portrayal"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"grid = CanvasGrid(agent_portrayal, 10, 10, 500, 500)"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Interface starting at http://127.0.0.1:8889\n"
]
},
{
"ename": "RuntimeError",
"evalue": "IOLoop is already running",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mRuntimeError\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m<ipython-input-10-b57517a62713>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[1;32m 9\u001b[0m 100, 10, 10)\n\u001b[1;32m 10\u001b[0m \u001b[0mserver\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mport\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;36m8889\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m---> 11\u001b[0;31m \u001b[0mserver\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mlaunch\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[0;32mC:\\Users\\Tom\\Anaconda3\\lib\\site-packages\\mesa\\visualization\\ModularVisualization.py\u001b[0m in \u001b[0;36mlaunch\u001b[0;34m(self, port)\u001b[0m\n\u001b[1;32m 282\u001b[0m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m'Interface starting at http://127.0.0.1:{PORT}'\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mformat\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mPORT\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mport\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m 283\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mlisten\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mport\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m--> 284\u001b[0;31m \u001b[0mtornado\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mioloop\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mIOLoop\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0minstance\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mstart\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[0;32mC:\\Users\\Tom\\Anaconda3\\lib\\site-packages\\zmq\\eventloop\\ioloop.py\u001b[0m in \u001b[0;36mstart\u001b[0;34m(self)\u001b[0m\n\u001b[1;32m 160\u001b[0m \u001b[1;32mdef\u001b[0m \u001b[0mstart\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mself\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m 161\u001b[0m \u001b[1;32mtry\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m--> 162\u001b[0;31m \u001b[0msuper\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mZMQIOLoop\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mstart\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 163\u001b[0m \u001b[1;32mexcept\u001b[0m \u001b[0mZMQError\u001b[0m \u001b[1;32mas\u001b[0m \u001b[0me\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m 164\u001b[0m \u001b[1;32mif\u001b[0m \u001b[0me\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0merrno\u001b[0m \u001b[1;33m==\u001b[0m \u001b[0mETERM\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
"\u001b[0;32mC:\\Users\\Tom\\Anaconda3\\lib\\site-packages\\tornado\\ioloop.py\u001b[0m in \u001b[0;36mstart\u001b[0;34m(self)\u001b[0m\n\u001b[1;32m 750\u001b[0m \u001b[1;32mdef\u001b[0m \u001b[0mstart\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mself\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m 751\u001b[0m \u001b[1;32mif\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_running\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m--> 752\u001b[0;31m \u001b[1;32mraise\u001b[0m \u001b[0mRuntimeError\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m\"IOLoop is already running\"\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 753\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_setup_logging\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m 754\u001b[0m \u001b[1;32mif\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_stopped\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
"\u001b[0;31mRuntimeError\u001b[0m: IOLoop is already running"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Socket opened!\n",
"{\"type\":\"reset\"}\n",
"{\"type\":\"get_step\",\"step\":1}\n",
"{\"type\":\"get_step\",\"step\":2}\n",
"{\"type\":\"get_step\",\"step\":3}\n",
"{\"type\":\"get_step\",\"step\":4}\n",
"{\"type\":\"get_step\",\"step\":5}\n",
"{\"type\":\"get_step\",\"step\":6}\n",
"{\"type\":\"get_step\",\"step\":7}\n",
"{\"type\":\"get_step\",\"step\":8}\n",
"{\"type\":\"get_step\",\"step\":9}\n",
"{\"type\":\"get_step\",\"step\":10}\n",
"{\"type\":\"get_step\",\"step\":11}\n",
"{\"type\":\"get_step\",\"step\":12}\n",
"{\"type\":\"get_step\",\"step\":13}\n",
"{\"type\":\"get_step\",\"step\":14}\n",
"{\"type\":\"get_step\",\"step\":15}\n",
"{\"type\":\"get_step\",\"step\":16}\n",
"{\"type\":\"get_step\",\"step\":17}\n",
"{\"type\":\"get_step\",\"step\":18}\n",
"{\"type\":\"get_step\",\"step\":19}\n",
"{\"type\":\"get_step\",\"step\":20}\n",
"{\"type\":\"get_step\",\"step\":21}\n",
"{\"type\":\"get_step\",\"step\":22}\n",
"{\"type\":\"get_step\",\"step\":23}\n",
"{\"type\":\"get_step\",\"step\":24}\n",
"{\"type\":\"get_step\",\"step\":25}\n"
]
}
],
"source": [
"\n",
"chart = ChartModule([{\"Label\": \"Gini\",\n",
" \"Color\": \"Black\"}],\n",
" data_collector_name='datacollector')\n",
"\n",
"server = ModularServer(MoneyModel,\n",
" [grid],\n",
" \"Money Model\",\n",
" 100, 10, 10)\n",
"server.port = 8889\n",
"server.launch()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
}
],
"metadata": {
"anaconda-cloud": {},
"kernelspec": {
"display_name": "Python [conda root]",
"language": "python",
"name": "conda-root-py"
},
"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.5.2"
}
},
"nbformat": 4,
"nbformat_minor": 1
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment