200 lines
5.9 KiB
Plaintext
200 lines
5.9 KiB
Plaintext
{
|
|
"cells": [
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "google",
|
|
"metadata": {},
|
|
"source": [
|
|
"##### Copyright 2024 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": [
|
|
"# assignment"
|
|
]
|
|
},
|
|
{
|
|
"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/assignment.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/assignment.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 in Google CP Solver.\n",
|
|
"\n",
|
|
" Winston 'Operations Research', Assignment Problems, page 393f\n",
|
|
" (generalized version with added test column)\n",
|
|
"\n",
|
|
" Compare with the following models:\n",
|
|
" * Comet : http://www.hakank.org/comet/assignment.co\n",
|
|
" * ECLiPSE : http://www.hakank.org/eclipse/assignment.ecl\n",
|
|
" * Gecode : http://www.hakank.org/gecode/assignment.cpp\n",
|
|
" * MiniZinc: http://www.hakank.org/minizinc/assignment.mzn\n",
|
|
" * Tailor/Essence': http://www.hakank.org/tailor/assignment.eprime\n",
|
|
" * SICStus: http://hakank.org/sicstus/assignment.pl\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": [
|
|
"from ortools.constraint_solver import pywrapcp\n",
|
|
"\n",
|
|
"\n",
|
|
"def main(cost, rows, cols):\n",
|
|
" # Create the solver.\n",
|
|
" solver = pywrapcp.Solver(\"n-queens\")\n",
|
|
"\n",
|
|
" #\n",
|
|
" # data\n",
|
|
" #\n",
|
|
"\n",
|
|
" # declare variables\n",
|
|
" total_cost = solver.IntVar(0, 100, \"total_cost\")\n",
|
|
" x = []\n",
|
|
" for i in range(rows):\n",
|
|
" t = []\n",
|
|
" for j in range(cols):\n",
|
|
" t.append(solver.IntVar(0, 1, \"x[%i,%i]\" % (i, j)))\n",
|
|
" x.append(t)\n",
|
|
" x_flat = [x[i][j] for i in range(rows) for j in range(cols)]\n",
|
|
"\n",
|
|
" #\n",
|
|
" # constraints\n",
|
|
" #\n",
|
|
"\n",
|
|
" # total_cost\n",
|
|
" solver.Add(total_cost == solver.Sum(\n",
|
|
" [solver.ScalProd(x_row, cost_row) for (x_row, cost_row) in zip(x, cost)]))\n",
|
|
"\n",
|
|
" # exacly one assignment per row, all rows must be assigned\n",
|
|
" [\n",
|
|
" solver.Add(solver.Sum([x[row][j]\n",
|
|
" for j in range(cols)]) == 1)\n",
|
|
" for row in range(rows)\n",
|
|
" ]\n",
|
|
"\n",
|
|
" # zero or one assignments per column\n",
|
|
" [\n",
|
|
" solver.Add(solver.Sum([x[i][col]\n",
|
|
" for i in range(rows)]) <= 1)\n",
|
|
" for col in range(cols)\n",
|
|
" ]\n",
|
|
"\n",
|
|
" objective = solver.Minimize(total_cost, 1)\n",
|
|
"\n",
|
|
" #\n",
|
|
" # solution and search\n",
|
|
" #\n",
|
|
" solution = solver.Assignment()\n",
|
|
" solution.Add(x_flat)\n",
|
|
" solution.Add(total_cost)\n",
|
|
"\n",
|
|
" # db: DecisionBuilder\n",
|
|
" db = solver.Phase(x_flat, solver.INT_VAR_SIMPLE, solver.ASSIGN_MIN_VALUE)\n",
|
|
"\n",
|
|
" solver.NewSearch(db, [objective])\n",
|
|
" num_solutions = 0\n",
|
|
" while solver.NextSolution():\n",
|
|
" print(\"total_cost:\", total_cost.Value())\n",
|
|
" for i in range(rows):\n",
|
|
" for j in range(cols):\n",
|
|
" print(x[i][j].Value(), end=\" \")\n",
|
|
" print()\n",
|
|
" print()\n",
|
|
"\n",
|
|
" for i in range(rows):\n",
|
|
" print(\"Task:\", i, end=\" \")\n",
|
|
" for j in range(cols):\n",
|
|
" if x[i][j].Value() == 1:\n",
|
|
" print(\" is done by \", j)\n",
|
|
" print()\n",
|
|
"\n",
|
|
" num_solutions += 1\n",
|
|
" solver.EndSearch()\n",
|
|
"\n",
|
|
" print()\n",
|
|
" print(\"num_solutions:\", num_solutions)\n",
|
|
" print(\"failures:\", solver.Failures())\n",
|
|
" print(\"branches:\", solver.Branches())\n",
|
|
" print(\"WallTime:\", solver.WallTime())\n",
|
|
"\n",
|
|
"\n",
|
|
"# Problem instance\n",
|
|
"# hakank: I added the fifth column to make it more\n",
|
|
"# interesting\n",
|
|
"rows = 4\n",
|
|
"cols = 5\n",
|
|
"cost = [[14, 5, 8, 7, 15], [2, 12, 6, 5, 3], [7, 8, 3, 9, 7], [2, 4, 6, 10, 1]]\n",
|
|
"\n",
|
|
"main(cost, rows, cols)\n",
|
|
"\n"
|
|
]
|
|
}
|
|
],
|
|
"metadata": {},
|
|
"nbformat": 4,
|
|
"nbformat_minor": 5
|
|
}
|