{
 "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": "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
}
