Files
ortools-clone/examples/notebook/contrib/assignment.ipynb
Corentin Le Molgat 27121a1068 Update examples/notebook
generated using ./tools/gen_all_notebook.sh
2020-03-04 14:34:33 +01:00

136 lines
4.5 KiB
Plaintext

{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Copyright 2010 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",
" 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",
"\"\"\"\n",
"from __future__ import print_function\n",
"from ortools.constraint_solver import pywrapcp\n",
"\n",
"\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",
"# interestingrows = 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"
]
}
],
"metadata": {},
"nbformat": 4,
"nbformat_minor": 4
}