Skip to content

Instantly share code, notes, and snippets.

@pikulet
Created September 22, 2020 17:10
Show Gist options
  • Save pikulet/17006a04fa40a613fb91627b4178bd32 to your computer and use it in GitHub Desktop.
Save pikulet/17006a04fa40a613fb91627b4178bd32 to your computer and use it in GitHub Desktop.
nashpy_axelrod_example.ipynb
Display the source blob
Display the rendered blob
Raw
{
"nbformat": 4,
"nbformat_minor": 0,
"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.7.4"
},
"colab": {
"name": "nashpy_axelrod_example.ipynb",
"provenance": [],
"collapsed_sections": [],
"include_colab_link": true
}
},
"cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "view-in-github",
"colab_type": "text"
},
"source": [
"<a href=\"https://colab.research.google.com/gist/pikulet/17006a04fa40a613fb91627b4178bd32/nashpy_axelrod_example.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "m80rXVF8I5AN",
"colab_type": "text"
},
"source": [
"# Example of how to use nashpy, axelrod"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "fKYKZgVyI5AR",
"colab_type": "text"
},
"source": [
"## Prisoner's Dilemma\n",
"\n",
"![picture](https://drive.google.com/uc?id=1fw7j7O8XLGQR3Rt_c9UK_PE6KgLsFeEw)\n"
]
},
{
"cell_type": "code",
"metadata": {
"id": "vDUwerlDI5AS",
"colab_type": "code",
"colab": {}
},
"source": [
"# Import packages\n",
"!pip install nashpy\n",
"!pip install axelrod\n",
"\n",
"import nashpy as nash\n",
"import numpy as np"
],
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "D-rB1BgYI5Aa",
"colab_type": "code",
"colab": {}
},
"source": [
"# Create payoff matrix\n",
"\n",
"# p1 is the row player, p2 is the column player\n",
"p1 = np.array([[8,1], [15,3]])\n",
"p2 = np.array([[8,15], [1,3]])\n",
"prisoner_dilemma = nash.Game(P1, P2)\n",
"prisoner_dilemma"
],
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "ebCkVxbZu0gp",
"colab_type": "code",
"colab": {}
},
"source": [
"# Calculate utilities for Mixed Strategy\n",
"sigma_r = np.array([0.2, 0.8])\n",
"sigma_c = np.array([0.6, 0.4])\n",
"prisoner_dilemma = nash.Game(P1, P2)\n",
"prisoner_dilemma[sigma_r, sigma_c]"
],
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {
"id": "sF4K0KlOI5A7",
"colab_type": "text"
},
"source": [
"Strict and unique Nash Equilibrium\n",
"\n",
"![picture](https://drive.google.com/uc?id=1_B9Wk5Sb1jwK1AADXR1xj9n0tmALNykM)"
]
},
{
"cell_type": "code",
"metadata": {
"id": "U5u8UsoMu4Yu",
"colab_type": "code",
"colab": {}
},
"source": [
"# Find Nash Equilibria\n",
"equilibria = prisoner_dilemma.support_enumeration()\n",
"for eq in equilibria:\n",
" print(eq)\n",
"\n",
"# expected: single equilibrium point for each p1 and p2"
],
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {
"id": "NY2yrKJNI5BG",
"colab_type": "text"
},
"source": [
"## Hawk Dove - Multiple Nash Equilibria\n",
"\n",
"![picture](https://drive.google.com/uc?id=1b8kKho3qu1s5b7Qriq6NYWqJxd5uKI6x)"
]
},
{
"cell_type": "code",
"metadata": {
"id": "aQg_1AIMI5BG",
"colab_type": "code",
"colab": {}
},
"source": [
"# p3 is the row player, p4 is the column player\n",
"p3 = np.array([[3, 1], [4, 0]])\n",
"p4 = np.array([[3, 4], [1, 0]])\n",
"hawk_dove = nash.Game(P3, P4)\n",
"hawk_dove"
],
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {
"id": "HdUrrBQ9I5BK",
"colab_type": "text"
},
"source": [
"![picture](https://drive.google.com/uc?id=1JJxdwZ3y6U_hxMH-0l4i6LpuuTVhF5w0)\n"
]
},
{
"cell_type": "code",
"metadata": {
"id": "hCLjePSovi3z",
"colab_type": "code",
"colab": {}
},
"source": [
"equilibria = hawk_dove.support_enumeration()\n",
"for eq in equilibria:\n",
" print(eq)"
],
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {
"id": "5NhqPum6I5BX",
"colab_type": "text"
},
"source": [
"## Zero Sum Game - Matching coins\n",
"\n",
"![picture](https://drive.google.com/uc?id=1DJhLFiRbUah8Cvku03oGP5C2eFuDPxBQ)"
]
},
{
"cell_type": "code",
"metadata": {
"id": "3w_YHAZTI5BX",
"colab_type": "code",
"colab": {}
},
"source": [
"p5 = np.array([[1, -1], [-1, 1]])\n",
"mp = nash.Game(p5)"
],
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "7ZheZzjWuzGr",
"colab_type": "code",
"colab": {}
},
"source": [
"equilibria = mp.support_enumeration()\n",
"for eq in equilibria:\n",
" print(eq)"
],
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {
"id": "eVM0LDlfI5Br",
"colab_type": "text"
},
"source": [
"## Repeat games with Axelrod"
]
},
{
"cell_type": "code",
"metadata": {
"id": "-qiWUNZ2I5Bs",
"colab_type": "code",
"colab": {}
},
"source": [
"!pip install -U pyYAML\n",
"import axelrod as axl"
],
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "xAlZskFMI5Bv",
"colab_type": "code",
"colab": {}
},
"source": [
"players = (axl.Cooperator(), axl.Alternator()) # specify strategies\n",
"match = axl.Match(players, turns=5)\n",
"match.play()"
],
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "InpWBSpII5Bx",
"colab_type": "code",
"colab": {}
},
"source": [
"# view all strategies\n",
"axl.all_strategies "
],
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "eD9qeCgEI5B_",
"colab_type": "code",
"colab": {}
},
"source": [
"# analyse payoffs\n",
"match1.game\n",
"#R: Reward (default 3) C-C\n",
"#P: Punishment (default 1) D-D\n",
"#S: Loss (default 0) C-D\n",
"#T: Temptation (default 5) D-C"
],
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "xs7rQEyZI5CC",
"colab_type": "code",
"colab": {}
},
"source": [
"# retrieve scores\n",
"match1.scores()"
],
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "zCkP68lJI5CF",
"colab_type": "code",
"colab": {}
},
"source": [
"# visualise results using sparklines\n",
"# solid = cooperate, blank = defection\n",
"print(match1.sparklines())"
],
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {
"id": "B9h2o5YmI5CP",
"colab_type": "text"
},
"source": [
"#### References:\n",
"\n",
"Package Documentations\n",
"\n",
"https://nashpy.readthedocs.io/en/stable/index.html#\n",
"\n",
"https://axelrod.readthedocs.io/en/stable/#"
]
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment