{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Demographic Prisoner's Dilemma\n",
    "\n",
    "The Demographic Prisoner's Dilemma is a family of variants on the classic two-player [Prisoner's Dilemma](https://en.wikipedia.org/wiki/Prisoner's_dilemma), first developed by [Joshua Epstein](http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.8.8629&rep=rep1&type=pdf). The model consists of agents, each with a strategy of either Cooperate or Defect. Each agent's payoff is based on its strategy and the strategies of its spatial neighbors. After each step of the model, the agents adopt the strategy of their neighbor with the highest total score. \n",
    "\n",
    "The specific variant presented here is adapted from the [Evolutionary Prisoner's Dilemma](http://ccl.northwestern.edu/netlogo/models/PDBasicEvolutionary) model included with NetLogo. Its payoff table is a slight variant of the traditional PD payoff table:\n",
    "\n",
    "<table>\n",
    "    <tr><td></td><td>**Cooperate**</td><td>**Defect**</td></tr>\n",
    "    <tr><td>**Cooperate**</td><td>1, 1</td><td>0, *D*</td></tr>\n",
    "    <tr><td>**Defect**</td><td>*D*, 0</td><td>0, 0</td></tr>\n",
    "</table>\n",
    "\n",
    "Where *D* is the defection bonus, generally set higher than 1. In these runs, the defection bonus is set to $D=1.6$.\n",
    "\n",
    "The Demographic Prisoner's Dilemma demonstrates how simple rules can lead to the emergence of widespread cooperation, despite the Defection strategy dominiating each individual interaction game. However, it is also interesting for another reason: it is known to be sensitive to the activation regime employed in it.\n",
    "\n",
    "Below, we demonstrate this by instantiating the same model (with the same random seed) three times, with three different activation regimes: \n",
    "\n",
    "* Sequential activation, where agents are activated in the order they were added to the model;\n",
    "* Random activation, where they are activated in random order every step;\n",
    "* Simultaneous activation, simulating them all being activated simultaneously.\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "from pd_grid.model import PdGrid\n",
    "\n",
    "import numpy as np\n",
    "\n",
    "import matplotlib.pyplot as plt\n",
    "import matplotlib.gridspec\n",
    "%matplotlib inline"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Helper functions"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "bwr = plt.get_cmap(\"bwr\")\n",
    "\n",
    "def draw_grid(model, ax=None):\n",
    "    '''\n",
    "    Draw the current state of the grid, with Defecting agents in red\n",
    "    and Cooperating agents in blue.\n",
    "    '''\n",
    "    if not ax:\n",
    "        fig, ax = plt.subplots(figsize=(6,6))\n",
    "    grid = np.zeros((model.grid.width, model.grid.height))\n",
    "    for agent, x, y in model.grid.coord_iter():\n",
    "        if agent.move == \"D\":\n",
    "            grid[y][x] = 1\n",
    "        else:\n",
    "            grid[y][x] = 0\n",
    "    ax.pcolormesh(grid, cmap=bwr, vmin=0, vmax=1)\n",
    "    ax.axis('off')\n",
    "    ax.set_title(\"Steps: {}\".format(model.schedule.steps))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "def run_model(model):\n",
    "    '''\n",
    "    Run an experiment with a given model, and plot the results.\n",
    "    '''\n",
    "    fig = plt.figure(figsize=(12,8))\n",
    "    \n",
    "    ax1 = fig.add_subplot(231)\n",
    "    ax2 = fig.add_subplot(232)\n",
    "    ax3 = fig.add_subplot(233)\n",
    "    ax4 = fig.add_subplot(212)\n",
    "    \n",
    "    draw_grid(model, ax1)\n",
    "    model.run(10)\n",
    "    draw_grid(model, ax2)\n",
    "    model.run(10)\n",
    "    draw_grid(model, ax3)\n",
    "    model.datacollector.get_model_vars_dataframe().plot(ax=ax4)\n",
    "    \n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Set the random seed\n",
    "seed = 21"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Sequential Activation"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 18664 characters]\n",
      "text/plain": [
       "<Figure size 864x576 with 4 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "m = PdGrid(50, 50, \"Sequential\", seed=seed)\n",
    "run_model(m)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Random Activation"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 28060 characters]\n",
      "text/plain": [
       "<Figure size 864x576 with 4 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "m = PdGrid(50, 50, \"Random\", seed=seed)\n",
    "run_model(m)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true
   },
   "source": [
    "## Simultaneous Activation"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 29276 characters]\n",
      "text/plain": [
       "<Figure size 864x576 with 4 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "m = PdGrid(50, 50, \"Simultaneous\", seed=seed)\n",
    "run_model(m)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python [conda env:mesa]",
   "language": "python",
   "name": "conda-env-mesa-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.6.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
