Files
ortools-clone/examples/notebook/contrib/assignment6_mip.ipynb

237 lines
6.8 KiB
Plaintext
Raw Normal View History

{
"cells": [
2020-09-27 16:03:47 +02:00
{
"cell_type": "markdown",
2021-12-14 13:05:00 +01:00
"id": "google",
2020-09-27 16:03:47 +02:00
"metadata": {},
"source": [
2025-02-04 18:04:03 +01:00
"##### Copyright 2025 Google LLC."
2020-09-27 16:03:47 +02:00
]
},
{
"cell_type": "markdown",
2021-12-14 13:05:00 +01:00
"id": "apache",
2020-09-27 16:03:47 +02:00
"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",
2021-12-14 13:05:00 +01:00
"id": "basename",
2020-09-27 16:03:47 +02:00
"metadata": {},
"source": [
"# assignment6_mip"
]
},
{
"cell_type": "markdown",
2021-12-14 13:05:00 +01:00
"id": "link",
2020-09-27 16:03:47 +02:00
"metadata": {},
"source": [
"<table align=\"left\">\n",
"<td>\n",
2022-06-27 15:42:26 +02:00
"<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",
2020-09-27 16:03:47 +02:00
"</td>\n",
"<td>\n",
2022-06-27 15:42:26 +02:00
"<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",
2020-09-27 16:03:47 +02:00
"</td>\n",
"</table>"
]
},
{
"cell_type": "markdown",
2021-12-14 13:05:00 +01:00
"id": "doc",
2020-09-27 16:03:47 +02:00
"metadata": {},
"source": [
"First, you must install [ortools](https://pypi.org/project/ortools/) package in this colab."
]
},
{
"cell_type": "code",
"execution_count": null,
2021-12-14 13:05:00 +01:00
"id": "install",
2020-09-27 16:03:47 +02:00
"metadata": {},
"outputs": [],
"source": [
"%pip install ortools"
2020-09-27 16:03:47 +02:00
]
},
{
2022-04-14 14:07:58 +02:00
"cell_type": "markdown",
"id": "description",
"metadata": {},
"source": [
2022-04-14 14:07:58 +02:00
"\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",
2022-04-14 14:07:58 +02:00
" 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",
2022-04-14 14:07:58 +02:00
"def main(sol='CBC'):\n",
"\n",
2022-04-14 14:07:58 +02:00
" # Create the solver.\n",
" print('Solver: ', sol)\n",
"\n",
2022-04-14 14:07:58 +02:00
" if sol == 'GLPK':\n",
2022-06-27 15:42:26 +02:00
" solver = pywraplp.Solver.CreateSolver('GLPK')\n",
2022-04-14 14:07:58 +02:00
" else:\n",
2022-06-27 15:42:26 +02:00
" solver = pywraplp.Solver.CreateSolver('CBC')\n",
" if not solver:\n",
" return\n",
"\n",
2022-04-14 14:07:58 +02:00
" #\n",
" # data\n",
" #\n",
"\n",
2022-04-14 14:07:58 +02:00
" # number of agents\n",
" m = 8\n",
"\n",
2022-04-14 14:07:58 +02:00
" # number of tasks\n",
" n = 8\n",
"\n",
2022-04-14 14:07:58 +02:00
" # set of agents\n",
" I = list(range(m))\n",
"\n",
2022-04-14 14:07:58 +02:00
" # set of tasks\n",
" J = list(range(n))\n",
"\n",
2022-04-14 14:07:58 +02:00
" # 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",
2022-04-14 14:07:58 +02:00
" #\n",
" # variables\n",
" #\n",
"\n",
2022-04-14 14:07:58 +02:00
" # For the output: the assignment as task number.\n",
" assigned = [solver.IntVar(0, 10000, 'assigned[%i]' % j) for j in J]\n",
"\n",
2022-04-14 14:07:58 +02:00
" costs = [solver.IntVar(0, 10000, 'costs[%i]' % i) for i in I]\n",
"\n",
2022-04-14 14:07:58 +02:00
" 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",
2022-04-14 14:07:58 +02:00
" # 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",
2022-04-14 14:07:58 +02:00
" #\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",
2022-04-14 14:07:58 +02:00
" # 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",
2022-04-14 14:07:58 +02:00
" # 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",
2022-04-14 14:07:58 +02:00
" # objective\n",
" objective = solver.Minimize(z)\n",
"\n",
2022-04-14 14:07:58 +02:00
" #\n",
" # solution and search\n",
" #\n",
" solver.Solve()\n",
"\n",
2022-04-14 14:07:58 +02:00
" print()\n",
" print('z: ', int(solver.Objective().Value()))\n",
"\n",
2022-04-14 14:07:58 +02:00
" print('Assigned')\n",
" for j in J:\n",
2022-04-14 14:07:58 +02:00
" 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",
2022-04-14 14:07:58 +02:00
" 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",
2022-04-14 14:07:58 +02:00
"main(sol)\n",
"\n"
]
}
],
2025-02-04 18:04:03 +01:00
"metadata": {
"language_info": {
"name": "python"
}
},
"nbformat": 4,
2021-09-30 01:24:08 +02:00
"nbformat_minor": 5
}