233 lines
6.7 KiB
Plaintext
233 lines
6.7 KiB
Plaintext
{
|
|
"cells": [
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "google",
|
|
"metadata": {},
|
|
"source": [
|
|
"##### Copyright 2022 Google LLC."
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "apache",
|
|
"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",
|
|
"id": "basename",
|
|
"metadata": {},
|
|
"source": [
|
|
"# assignment6_mip"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "link",
|
|
"metadata": {},
|
|
"source": [
|
|
"<table align=\"left\">\n",
|
|
"<td>\n",
|
|
"<a href=\"https://colab.research.google.com/github/google/or-tools/blob/main/examples/notebook/contrib/assignment6_mip.ipynb\"><img src=\"https://raw.githubusercontent.com/google/or-tools/main/tools/colab_32px.png\"/>Run in Google Colab</a>\n",
|
|
"</td>\n",
|
|
"<td>\n",
|
|
"<a href=\"https://github.com/google/or-tools/blob/main/examples/contrib/assignment6_mip.py\"><img src=\"https://raw.githubusercontent.com/google/or-tools/main/tools/github_32px.png\"/>View source on GitHub</a>\n",
|
|
"</td>\n",
|
|
"</table>"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "doc",
|
|
"metadata": {},
|
|
"source": [
|
|
"First, you must install [ortools](https://pypi.org/project/ortools/) package in this colab."
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "install",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"!pip install ortools"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "description",
|
|
"metadata": {},
|
|
"source": [
|
|
"\n",
|
|
"\n",
|
|
" Assignment problem using MIP in Google or-tools.\n",
|
|
"\n",
|
|
" From GLPK:s example assign.mod:\n",
|
|
" '''\n",
|
|
" The assignment problem is one of the fundamental combinatorial\n",
|
|
" optimization problems.\n",
|
|
"\n",
|
|
" In its most general form, the problem is as follows:\n",
|
|
"\n",
|
|
" There are a number of agents and a number of tasks. Any agent can be\n",
|
|
" assigned to perform any task, incurring some cost that may vary\n",
|
|
" depending on the agent-task assignment. It is required to perform all\n",
|
|
" tasks by assigning exactly one agent to each task in such a way that\n",
|
|
" the total cost of the assignment is minimized.\n",
|
|
"\n",
|
|
" (From Wikipedia, the free encyclopedia.)\n",
|
|
" '''\n",
|
|
"\n",
|
|
" Compare with the Comet model:\n",
|
|
" http://www.hakank.org/comet/assignment6.co\n",
|
|
"\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"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "code",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"import sys\n",
|
|
"from ortools.linear_solver import pywraplp\n",
|
|
"\n",
|
|
"\n",
|
|
"def main(sol='CBC'):\n",
|
|
"\n",
|
|
" # Create the solver.\n",
|
|
" print('Solver: ', sol)\n",
|
|
"\n",
|
|
" if sol == 'GLPK':\n",
|
|
" solver = pywraplp.Solver.CreateSolver('GLPK')\n",
|
|
" else:\n",
|
|
" solver = pywraplp.Solver.CreateSolver('CBC')\n",
|
|
" if not solver:\n",
|
|
" return\n",
|
|
"\n",
|
|
" #\n",
|
|
" # data\n",
|
|
" #\n",
|
|
"\n",
|
|
" # number of agents\n",
|
|
" m = 8\n",
|
|
"\n",
|
|
" # number of tasks\n",
|
|
" n = 8\n",
|
|
"\n",
|
|
" # set of agents\n",
|
|
" I = list(range(m))\n",
|
|
"\n",
|
|
" # set of tasks\n",
|
|
" J = list(range(n))\n",
|
|
"\n",
|
|
" # cost of allocating task j to agent i\n",
|
|
" # \"\"\"\n",
|
|
" # These data correspond to an example from [Christofides].\n",
|
|
" #\n",
|
|
" # Optimal solution is 76\n",
|
|
" # \"\"\"\n",
|
|
" c = [[13, 21, 20, 12, 8, 26, 22, 11], [12, 36, 25, 41, 40, 11, 4, 8],\n",
|
|
" [35, 32, 13, 36, 26, 21, 13, 37], [34, 54, 7, 8, 12, 22, 11, 40],\n",
|
|
" [21, 6, 45, 18, 24, 34, 12, 48], [42, 19, 39, 15, 14, 16, 28, 46],\n",
|
|
" [16, 34, 38, 3, 34, 40, 22, 24], [26, 20, 5, 17, 45, 31, 37, 43]]\n",
|
|
"\n",
|
|
" #\n",
|
|
" # variables\n",
|
|
" #\n",
|
|
"\n",
|
|
" # For the output: the assignment as task number.\n",
|
|
" assigned = [solver.IntVar(0, 10000, 'assigned[%i]' % j) for j in J]\n",
|
|
"\n",
|
|
" costs = [solver.IntVar(0, 10000, 'costs[%i]' % i) for i in I]\n",
|
|
"\n",
|
|
" x = {}\n",
|
|
" for i in range(n):\n",
|
|
" for j in range(n):\n",
|
|
" x[i, j] = solver.IntVar(0, 1, 'x[%i,%i]' % (i, j))\n",
|
|
"\n",
|
|
" # total cost, to be minimized\n",
|
|
" z = solver.Sum([c[i][j] * x[i, j] for i in I for j in J])\n",
|
|
"\n",
|
|
" #\n",
|
|
" # constraints\n",
|
|
" #\n",
|
|
" # each agent can perform at most one task\n",
|
|
" for i in I:\n",
|
|
" solver.Add(solver.Sum([x[i, j] for j in J]) <= 1)\n",
|
|
"\n",
|
|
" # each task must be assigned exactly to one agent\n",
|
|
" for j in J:\n",
|
|
" solver.Add(solver.Sum([x[i, j] for i in I]) == 1)\n",
|
|
"\n",
|
|
" # to which task and what cost is person i assigned (for output in MiniZinc)\n",
|
|
" for i in I:\n",
|
|
" solver.Add(assigned[i] == solver.Sum([j * x[i, j] for j in J]))\n",
|
|
" solver.Add(costs[i] == solver.Sum([c[i][j] * x[i, j] for j in J]))\n",
|
|
"\n",
|
|
" # objective\n",
|
|
" objective = solver.Minimize(z)\n",
|
|
"\n",
|
|
" #\n",
|
|
" # solution and search\n",
|
|
" #\n",
|
|
" solver.Solve()\n",
|
|
"\n",
|
|
" print()\n",
|
|
" print('z: ', int(solver.Objective().Value()))\n",
|
|
"\n",
|
|
" print('Assigned')\n",
|
|
" for j in J:\n",
|
|
" print(int(assigned[j].SolutionValue()), end=' ')\n",
|
|
" print()\n",
|
|
"\n",
|
|
" print('Matrix:')\n",
|
|
" for i in I:\n",
|
|
" for j in J:\n",
|
|
" print(int(x[i, j].SolutionValue()), end=' ')\n",
|
|
" print()\n",
|
|
" print()\n",
|
|
"\n",
|
|
" print()\n",
|
|
" print('walltime :', solver.WallTime(), 'ms')\n",
|
|
" if sol == 'CBC':\n",
|
|
" print('iterations:', solver.Iterations())\n",
|
|
"\n",
|
|
"\n",
|
|
"\n",
|
|
"sol = 'CBC'\n",
|
|
"if len(sys.argv) > 1:\n",
|
|
" sol = sys.argv[1]\n",
|
|
" if sol != 'GLPK' and sol != 'CBC':\n",
|
|
" print('Solver must be either GLPK or CBC')\n",
|
|
" sys.exit(1)\n",
|
|
"\n",
|
|
"main(sol)\n",
|
|
"\n"
|
|
]
|
|
}
|
|
],
|
|
"metadata": {},
|
|
"nbformat": 4,
|
|
"nbformat_minor": 5
|
|
}
|