Skip to content
This repository has been archived by the owner on Apr 24, 2023. It is now read-only.

Commit

Permalink
Adding SciPy2015 presentation material
Browse files Browse the repository at this point in the history
  • Loading branch information
dmasad committed Jul 9, 2015
1 parent 3d3bb8e commit 5d820f0
Show file tree
Hide file tree
Showing 6 changed files with 916 additions and 0 deletions.
59 changes: 59 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]

*py~
*swp

# C extensions
*.so

# Distribution / packaging
.Python
env/
build/
develop-eggs/
dist/
downloads/
eggs/
lib/
lib64/
parts/
sdist/
var/
*.egg-info/
.installed.cfg
*.egg

# PyInstaller
# Usually these files are written by a python script from a template
# before PyInstaller builds the exe, so as to inject date/other infos into it.
*.manifest
*.spec

# Installer logs
pip-log.txt
pip-delete-this-directory.txt

# Unit test / coverage reports
htmlcov/
.tox/
.coverage
.cache
nosetests.xml
coverage.xml

# Translations
*.mo
*.pot

# Django stuff:
*.log

# Sphinx documentation
docs/_build/

# PyBuilder
target/

*.ipynb_checkpoints
Original file line number Diff line number Diff line change
@@ -0,0 +1,207 @@
{
"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": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"from pd_grid import PD_Model\n",
"\n",
"import random\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": null,
"metadata": {
"collapsed": true
},
"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": null,
"metadata": {
"collapsed": true
},
"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"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"# Set the random seed\n",
"seed = 21"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Sequential Activation"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"random.seed(seed)\n",
"m = PD_Model(50, 50, \"Sequential\")\n",
"run_model(m)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Random Activation"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"random.seed(seed)\n",
"m = PD_Model(50, 50, \"Random\")\n",
"run_model(m)"
]
},
{
"cell_type": "markdown",
"metadata": {
"collapsed": true
},
"source": [
"## Simultaneous Activation"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"random.seed(seed)\n",
"m = PD_Model(50, 50, \"Simultaneous\")\n",
"run_model(m)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"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.4.2"
}
},
"nbformat": 4,
"nbformat_minor": 0
}
Loading

0 comments on commit 5d820f0

Please sign in to comment.