{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "import cvxpy as cp\n",
    "import numpy as np\n",
    "import numpy.linalg as la\n",
    "%matplotlib inline"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Matching Pennies"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "primal solution :  [4.302711e-10 5.000000e-01 5.000000e-01]\n",
      "dual solution :  [0.5 0.5]\n"
     ]
    }
   ],
   "source": [
    "A=np.array([[-1,1],[1,-1]])\n",
    "y = cp.Variable(2)\n",
    "v = cp.Variable(1)\n",
    "ones=np.ones(2)\n",
    "prob = cp.Problem(cp.Maximize(v), [A@y>= v*ones, ones@y==1,y>=0])\n",
    "prob.solve()\n",
    "# Print result.\n",
    "print(\"primal solution : \", x.value)\n",
    "print(\"dual solution : \",prob.constraints[0].dual_value)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "value of w :  -4.3027109882345924e-10\n",
      "primal solution :  [0.5 0.5]\n",
      "dual solution :  [0.5 0.5]\n"
     ]
    }
   ],
   "source": [
    "A=np.array([[-1,1],[1,-1]])\n",
    "x = cp.Variable(2)\n",
    "w = cp.Variable(1)\n",
    "ones=np.ones(2)\n",
    "prob = cp.Problem(cp.Minimize(w), [A.T@x<= w*ones, ones@x==1, x>=0])\n",
    "prob.solve()\n",
    "# Print result.\n",
    "print('value of w : ', prob.value)\n",
    "print(\"primal solution : \", x.value)\n",
    "print(\"dual solution : \",prob.constraints[0].dual_value)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Rock Paper Scissors"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "value of w :  -2.445034710933577e-10\n",
      "primal solution :  [0.33333333 0.33333333 0.33333333]\n",
      "dual solution :  [0.33333333 0.33333333 0.33333333]\n"
     ]
    }
   ],
   "source": [
    "A=np.array([[0, -1, 1],[1, 0, -1],[-1, 1, 0]])\n",
    "x = cp.Variable(3)\n",
    "w = cp.Variable(1)\n",
    "ones=np.ones(3)\n",
    "prob = cp.Problem(cp.Minimize(w), [A.T@x<= w*ones, ones@x==1, x>=0])\n",
    "prob.solve()\n",
    "# Print result.\n",
    "print('value of w : ', prob.value)\n",
    "print(\"primal solution : \", x.value)\n",
    "print(\"dual solution : \",prob.constraints[0].dual_value)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Solving Bimatrix Games by iterating on LPs"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Consider the game in Example 3.3 in the notes which is defined as follows:\n",
    "$$\\begin{array}{ccc}\n",
    "        \\text{P1}\\backslash \\text{P2} & L & R \\\\\n",
    "           U & (5,1) & (0,0)\\\\\n",
    "      D & (4,4) & (1,5) \\end{array}$$\n",
    "The matrix form of this game is defined by the following matrices:\n",
    "        $$A=\\begin{bmatrix}5 & 0\\\\ 4 & 1\\end{bmatrix}\\ \\ \\text{and}\\ \\ B^\\top=\\begin{bmatrix}1 & 0\\\\ 4& 5\\end{bmatrix}$$\n",
    "        \n",
    "This game has three Nash equilibria:\n",
    "$$(U,L), \\ (D,R), \\ ((1/2,1/2),(1/2,1/2))$$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 99,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1, 4])"
      ]
     },
     "execution_count": 99,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "## Guess a feasible Solution for x\n",
    "## i.e., Bx-u1>=0, 1^Tx=1,x>=0\n",
    "A=np.array([[5,0],[4,1]]); B=np.array([[1,0],[4,5]])\n",
    "x_guess=np.random.dirichlet(np.ones(2),size=1).T\n",
    "B@x_guess"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 107,
   "metadata": {},
   "outputs": [],
   "source": [
    "A=np.array([[5,0],[4,1]]); B=np.array([[1,0],[4,5]]).T\n",
    "x_guess=np.array([2/3,1/3])\n",
    "u_guess=0\n",
    "ones=np.ones(2)\n",
    "for i in range(2):\n",
    "    if np.mod(i,2)==0:\n",
    "        #print(i)\n",
    "        y=cp.Variable(2)\n",
    "        v=cp.Variable(1)\n",
    "        prob=cp.Problem(cp.Minimize(x_guess.T@A@y+x_guess.T@B.T@y-u_guess-v), [A@y-v*ones>=0,ones.T@y==1,y>=0])\n",
    "        sol=prob.solve()\n",
    "        y_guess=y.value\n",
    "        v_guess=v.value\n",
    "    else:\n",
    "        x=cp.Variable(2)\n",
    "        u=cp.Variable(1)\n",
    "        prob=cp.Problem(cp.Minimize(x.T@A@y_guess+x.T@B.T@y_guess-u-v_guess), [B@x-u*ones>=0,ones.T@x==1,x>=0])\n",
    "        sol=prob.solve()\n",
    "        x_guess=x.value\n",
    "        u_guess=u.value\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 108,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(array([2.5]), array([0.5, 0.5]), array([0.5, 0.5]), array([2.5]))"
      ]
     },
     "execution_count": 108,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "v_guess, y_guess, x_guess, u_guess"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "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.9.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
