Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save drvinceknight/6d4310debc1fd83d99f901194f77dcbd to your computer and use it in GitHub Desktop.
Save drvinceknight/6d4310debc1fd83d99f901194f77dcbd to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"A short demo of calculating the utility of PD strategies in asymetric games. The model assumed is an asymetric game where the total utility is taken as the mean of the utility for each player acting as both the row and column player."
]
},
{
"cell_type": "code",
"execution_count": 40,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"Playing matches: 100%|██████████| 15/15 [00:00<00:00, 91.39it/s] \n",
"Analysing: 100%|██████████| 150/150 [00:00<00:00, 712.41it/s]\n",
"Finishing: 100%|██████████| 23/23 [00:00<00:00, 2843.34it/s]\n"
]
}
],
"source": [
"import axelrod as axl\n",
"import pandas as pd\n",
"import numpy as np\n",
"players = [s() for s in axl.demo_strategies]\n",
"tournament = axl.Tournament(players)\n",
"results = tournament.play()"
]
},
{
"cell_type": "code",
"execution_count": 41,
"metadata": {},
"outputs": [],
"source": [
"summary = pd.DataFrame(results.summarise()) # Using pandas to make this easier to analyse"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The `summary` object is a summary of our tournament with many useful features. More information about this here: http://axelrod.readthedocs.io/en/stable/tutorials/getting_started/summarising_tournaments.html"
]
},
{
"cell_type": "code",
"execution_count": 42,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>Rank</th>\n",
" <th>Name</th>\n",
" <th>Median_score</th>\n",
" <th>Cooperation_rating</th>\n",
" <th>Wins</th>\n",
" <th>Initial_C_rate</th>\n",
" <th>CC_rate</th>\n",
" <th>CD_rate</th>\n",
" <th>DC_rate</th>\n",
" <th>DD_rate</th>\n",
" <th>CC_to_C_rate</th>\n",
" <th>CD_to_C_rate</th>\n",
" <th>DC_to_C_rate</th>\n",
" <th>DD_to_C_rate</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>0</td>\n",
" <td>Defector</td>\n",
" <td>2.520000</td>\n",
" <td>0.000000</td>\n",
" <td>4.0</td>\n",
" <td>0.000</td>\n",
" <td>0.000000</td>\n",
" <td>0.000000</td>\n",
" <td>0.378750</td>\n",
" <td>0.62125</td>\n",
" <td>0.000000</td>\n",
" <td>0.00000</td>\n",
" <td>0.000000</td>\n",
" <td>0.000000</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>1</td>\n",
" <td>Grudger</td>\n",
" <td>2.481250</td>\n",
" <td>0.504375</td>\n",
" <td>1.0</td>\n",
" <td>1.000</td>\n",
" <td>0.501875</td>\n",
" <td>0.002500</td>\n",
" <td>0.120125</td>\n",
" <td>0.37550</td>\n",
" <td>1.000000</td>\n",
" <td>0.00000</td>\n",
" <td>0.000000</td>\n",
" <td>0.000000</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>2</td>\n",
" <td>Tit For Tat</td>\n",
" <td>2.309375</td>\n",
" <td>0.627750</td>\n",
" <td>0.0</td>\n",
" <td>1.000</td>\n",
" <td>0.563750</td>\n",
" <td>0.064000</td>\n",
" <td>0.062250</td>\n",
" <td>0.31000</td>\n",
" <td>1.000000</td>\n",
" <td>0.00000</td>\n",
" <td>1.000000</td>\n",
" <td>0.000000</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>3</td>\n",
" <td>Cooperator</td>\n",
" <td>1.867500</td>\n",
" <td>1.000000</td>\n",
" <td>0.0</td>\n",
" <td>1.000</td>\n",
" <td>0.622750</td>\n",
" <td>0.377250</td>\n",
" <td>0.000000</td>\n",
" <td>0.00000</td>\n",
" <td>1.000000</td>\n",
" <td>1.00000</td>\n",
" <td>0.000000</td>\n",
" <td>0.000000</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>4</td>\n",
" <td>Random: 0.5</td>\n",
" <td>1.837500</td>\n",
" <td>0.497000</td>\n",
" <td>1.0</td>\n",
" <td>0.475</td>\n",
" <td>0.188375</td>\n",
" <td>0.308625</td>\n",
" <td>0.191250</td>\n",
" <td>0.31175</td>\n",
" <td>0.532596</td>\n",
" <td>0.51139</td>\n",
" <td>0.500055</td>\n",
" <td>0.484822</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" Rank Name Median_score Cooperation_rating Wins Initial_C_rate \\\n",
"0 0 Defector 2.520000 0.000000 4.0 0.000 \n",
"1 1 Grudger 2.481250 0.504375 1.0 1.000 \n",
"2 2 Tit For Tat 2.309375 0.627750 0.0 1.000 \n",
"3 3 Cooperator 1.867500 1.000000 0.0 1.000 \n",
"4 4 Random: 0.5 1.837500 0.497000 1.0 0.475 \n",
"\n",
" CC_rate CD_rate DC_rate DD_rate CC_to_C_rate CD_to_C_rate \\\n",
"0 0.000000 0.000000 0.378750 0.62125 0.000000 0.00000 \n",
"1 0.501875 0.002500 0.120125 0.37550 1.000000 0.00000 \n",
"2 0.563750 0.064000 0.062250 0.31000 1.000000 0.00000 \n",
"3 0.622750 0.377250 0.000000 0.00000 1.000000 1.00000 \n",
"4 0.188375 0.308625 0.191250 0.31175 0.532596 0.51139 \n",
"\n",
" DC_to_C_rate DD_to_C_rate \n",
"0 0.000000 0.000000 \n",
"1 0.000000 0.000000 \n",
"2 1.000000 0.000000 \n",
"3 0.000000 0.000000 \n",
"4 0.500055 0.484822 "
]
},
"execution_count": 42,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"summary"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Let us assume we use the following asymetric PD:\n",
"\n",
"$$\n",
"A_0 = \\begin{pmatrix}\n",
"3 & 0\\\\\n",
"5 & 1\n",
"\\end{pmatrix}\n",
"\\qquad\n",
"A_1 =\\begin{pmatrix}\n",
"4 & 1\\\\\n",
"6 & 2\n",
"\\end{pmatrix}\n",
"$$\n",
"\n",
"The game is asymetric because $A_0 \\ne A_1$. The total utility of a player is assumed to be the mean utility when they are the row player (so the utility from $A_0$) and when they are the column player (so the utility from $A_1$). This could be modified."
]
},
{
"cell_type": "code",
"execution_count": 43,
"metadata": {},
"outputs": [],
"source": [
"summary[\"Row_player_utility\"] = (summary[\"CC_rate\"] * games[0][0, 0] +\n",
" summary[\"CD_rate\"] * games[0][0, 1] +\n",
" summary[\"DC_rate\"] * games[0][1, 0] + \n",
" summary[\"DD_rate\"] * games[0][1, 1])"
]
},
{
"cell_type": "code",
"execution_count": 44,
"metadata": {},
"outputs": [],
"source": [
"summary[\"Col_player_utility\"] = (summary[\"CC_rate\"] * games[1][0, 0] + \n",
" summary[\"CD_rate\"] * games[1][1, 0] + \n",
" summary[\"DC_rate\"] * games[1][0, 1] + \n",
" summary[\"DD_rate\"] * games[1][1, 1])"
]
},
{
"cell_type": "code",
"execution_count": 45,
"metadata": {},
"outputs": [],
"source": [
"summary[\"Mean_utility\"] = (summary[\"Row_player_utility\"] + summary[\"Col_player_utility\"]) / 2"
]
},
{
"cell_type": "code",
"execution_count": 46,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>Name</th>\n",
" <th>CC_rate</th>\n",
" <th>CD_rate</th>\n",
" <th>DC_rate</th>\n",
" <th>DD_rate</th>\n",
" <th>Row_player_utility</th>\n",
" <th>Col_player_utility</th>\n",
" <th>Mean_utility</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>Defector</td>\n",
" <td>0.000000</td>\n",
" <td>0.000000</td>\n",
" <td>0.378750</td>\n",
" <td>0.62125</td>\n",
" <td>2.515000</td>\n",
" <td>1.621250</td>\n",
" <td>2.068125</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>Grudger</td>\n",
" <td>0.501875</td>\n",
" <td>0.002500</td>\n",
" <td>0.120125</td>\n",
" <td>0.37550</td>\n",
" <td>2.481750</td>\n",
" <td>2.893625</td>\n",
" <td>2.687688</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>Tit For Tat</td>\n",
" <td>0.563750</td>\n",
" <td>0.064000</td>\n",
" <td>0.062250</td>\n",
" <td>0.31000</td>\n",
" <td>2.312500</td>\n",
" <td>3.321250</td>\n",
" <td>2.816875</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>Cooperator</td>\n",
" <td>0.622750</td>\n",
" <td>0.377250</td>\n",
" <td>0.000000</td>\n",
" <td>0.00000</td>\n",
" <td>1.868250</td>\n",
" <td>4.754500</td>\n",
" <td>3.311375</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>Random: 0.5</td>\n",
" <td>0.188375</td>\n",
" <td>0.308625</td>\n",
" <td>0.191250</td>\n",
" <td>0.31175</td>\n",
" <td>1.833125</td>\n",
" <td>3.420000</td>\n",
" <td>2.626562</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" Name CC_rate CD_rate DC_rate DD_rate Row_player_utility \\\n",
"0 Defector 0.000000 0.000000 0.378750 0.62125 2.515000 \n",
"1 Grudger 0.501875 0.002500 0.120125 0.37550 2.481750 \n",
"2 Tit For Tat 0.563750 0.064000 0.062250 0.31000 2.312500 \n",
"3 Cooperator 0.622750 0.377250 0.000000 0.00000 1.868250 \n",
"4 Random: 0.5 0.188375 0.308625 0.191250 0.31175 1.833125 \n",
"\n",
" Col_player_utility Mean_utility \n",
"0 1.621250 2.068125 \n",
"1 2.893625 2.687688 \n",
"2 3.321250 2.816875 \n",
"3 4.754500 3.311375 \n",
"4 3.420000 2.626562 "
]
},
"execution_count": 46,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"summary[[\"Name\", \"CC_rate\", \"CD_rate\", \"DC_rate\", \"DD_rate\", \n",
" \"Row_player_utility\", \"Col_player_utility\", \"Mean_utility\"]] "
]
}
],
"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": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment