183 lines
5.4 KiB
Plaintext
183 lines
5.4 KiB
Plaintext
{
|
|
"cells": [
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"##### Copyright 2020 Google LLC."
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"Licensed under the Apache License, Version 2.0 (the \"License\");\n",
|
|
"you may not use this file except in compliance with the License.\n",
|
|
"You may obtain a copy of the License at\n",
|
|
"\n",
|
|
" http://www.apache.org/licenses/LICENSE-2.0\n",
|
|
"\n",
|
|
"Unless required by applicable law or agreed to in writing, software\n",
|
|
"distributed under the License is distributed on an \"AS IS\" BASIS,\n",
|
|
"WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n",
|
|
"See the License for the specific language governing permissions and\n",
|
|
"limitations under the License.\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"# game_theory_taha"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"<table align=\"left\">\n",
|
|
"<td>\n",
|
|
"<a href=\"https://colab.research.google.com/github/google/or-tools/blob/master/examples/notebook/contrib/game_theory_taha.ipynb\"><img src=\"https://raw.githubusercontent.com/google/or-tools/master/tools/colab_32px.png\"/>Run in Google Colab</a>\n",
|
|
"</td>\n",
|
|
"<td>\n",
|
|
"<a href=\"https://github.com/google/or-tools/blob/master/examples/contrib/game_theory_taha.py\"><img src=\"https://raw.githubusercontent.com/google/or-tools/master/tools/github_32px.png\"/>View source on GitHub</a>\n",
|
|
"</td>\n",
|
|
"</table>"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"First, you must install [ortools](https://pypi.org/project/ortools/) package in this colab."
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"!pip install ortools"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"# Copyright 2011 Hakan Kjellerstrand hakank@gmail.com\n",
|
|
"#\n",
|
|
"# Licensed under the Apache License, Version 2.0 (the \"License\");\n",
|
|
"# you may not use this file except in compliance with the License.\n",
|
|
"# You may obtain a copy of the License at\n",
|
|
"#\n",
|
|
"# http://www.apache.org/licenses/LICENSE-2.0\n",
|
|
"#\n",
|
|
"# Unless required by applicable law or agreed to in writing, software\n",
|
|
"# distributed under the License is distributed on an \"AS IS\" BASIS,\n",
|
|
"# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n",
|
|
"# See the License for the specific language governing permissions and\n",
|
|
"# limitations under the License.\n",
|
|
"\"\"\"\n",
|
|
"\n",
|
|
" Game theory in Google or-tools.\n",
|
|
"\n",
|
|
" 2 player zero sum game.\n",
|
|
"\n",
|
|
" From Taha, Operations Research (8'th edition), page 528.\n",
|
|
"\n",
|
|
" This model was created by Hakan Kjellerstrand (hakank@gmail.com)\n",
|
|
" Also see my other Google CP Solver models:\n",
|
|
" http://www.hakank.org/google_or_tools/\n",
|
|
"\"\"\"\n",
|
|
"from __future__ import print_function\n",
|
|
"import sys\n",
|
|
"from ortools.linear_solver import pywraplp\n",
|
|
"\n",
|
|
"\n",
|
|
"\n",
|
|
"# Create the solver.\n",
|
|
"\n",
|
|
"# using GLPK\n",
|
|
"if sol == 'GLPK':\n",
|
|
" solver = pywraplp.Solver('CoinsGridGLPK',\n",
|
|
" pywraplp.Solver.GLPK_LINEAR_PROGRAMMING)\n",
|
|
"else:\n",
|
|
" # Using CLP\n",
|
|
" solver = pywraplp.Solver('CoinsGridCLP',\n",
|
|
" pywraplp.Solver.CLP_LINEAR_PROGRAMMING)\n",
|
|
"\n",
|
|
"# data\n",
|
|
"rows = 3\n",
|
|
"cols = 3\n",
|
|
"\n",
|
|
"game = [[3.0, -1.0, -3.0], [-2.0, 4.0, -1.0], [-5.0, -6.0, 2.0]]\n",
|
|
"\n",
|
|
"#\n",
|
|
"# declare variables\n",
|
|
"#\n",
|
|
"\n",
|
|
"#\n",
|
|
"# row player\n",
|
|
"#\n",
|
|
"x1 = [solver.NumVar(0, 1, 'x1[%i]' % i) for i in range(rows)]\n",
|
|
"\n",
|
|
"v = solver.NumVar(-2, 2, 'v')\n",
|
|
"\n",
|
|
"for i in range(rows):\n",
|
|
" solver.Add(v - solver.Sum([x1[j] * game[j][i] for j in range(cols)]) <= 0)\n",
|
|
"\n",
|
|
"solver.Add(solver.Sum(x1) == 1)\n",
|
|
"\n",
|
|
"objective = solver.Maximize(v)\n",
|
|
"\n",
|
|
"solver.Solve()\n",
|
|
"\n",
|
|
"print()\n",
|
|
"print('row player:')\n",
|
|
"print('v = ', solver.Objective().Value())\n",
|
|
"print('Strategies: ')\n",
|
|
"for i in range(rows):\n",
|
|
" print(x1[i].SolutionValue(), end=' ')\n",
|
|
"print()\n",
|
|
"print()\n",
|
|
"\n",
|
|
"#\n",
|
|
"# For column player:\n",
|
|
"#\n",
|
|
"x2 = [solver.NumVar(0, 1, 'x2[%i]' % i) for i in range(cols)]\n",
|
|
"\n",
|
|
"v2 = solver.NumVar(-2, 2, 'v2')\n",
|
|
"\n",
|
|
"for i in range(cols):\n",
|
|
" solver.Add(v2 - solver.Sum([x2[j] * game[i][j] for j in range(rows)]) >= 0)\n",
|
|
"\n",
|
|
"solver.Add(solver.Sum(x2) == 1)\n",
|
|
"\n",
|
|
"objective = solver.Minimize(v2)\n",
|
|
"\n",
|
|
"solver.Solve()\n",
|
|
"\n",
|
|
"print()\n",
|
|
"print('column player:')\n",
|
|
"print('v2 = ', solver.Objective().Value())\n",
|
|
"print('Strategies: ')\n",
|
|
"for i in range(rows):\n",
|
|
" print(x2[i].SolutionValue(), end=' ')\n",
|
|
"print()\n",
|
|
"\n",
|
|
"print()\n",
|
|
"print('walltime :', solver.WallTime(), 'ms')\n",
|
|
"print('iterations:', solver.Iterations())\n",
|
|
"print()\n",
|
|
"\n"
|
|
]
|
|
}
|
|
],
|
|
"metadata": {},
|
|
"nbformat": 4,
|
|
"nbformat_minor": 4
|
|
}
|