Files
ortools-clone/examples/notebook/contrib/set_covering4.ipynb
2025-02-04 18:04:03 +01:00

235 lines
7.4 KiB
Plaintext

{
"cells": [
{
"cell_type": "markdown",
"id": "google",
"metadata": {},
"source": [
"##### Copyright 2025 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": [
"# set_covering4"
]
},
{
"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/set_covering4.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/set_covering4.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",
" Set partition and set covering in Google CP Solver.\n",
"\n",
" Example from the Swedish book\n",
" Lundgren, Roennqvist, Vaebrand\n",
" 'Optimeringslaera' (translation: 'Optimization theory'),\n",
" page 408.\n",
"\n",
" * Set partition:\n",
" We want to minimize the cost of the alternatives which covers all the\n",
" objects, i.e. all objects must be choosen. The requirement is than an\n",
" object may be selected _exactly_ once.\n",
"\n",
" Note: This is 1-based representation\n",
"\n",
" Alternative Cost Object\n",
" 1 19 1,6\n",
" 2 16 2,6,8\n",
" 3 18 1,4,7\n",
" 4 13 2,3,5\n",
" 5 15 2,5\n",
" 6 19 2,3\n",
" 7 15 2,3,4\n",
" 8 17 4,5,8\n",
" 9 16 3,6,8\n",
" 10 15 1,6,7\n",
"\n",
" The problem has a unique solution of z = 49 where alternatives\n",
" 3, 5, and 9\n",
" is selected.\n",
"\n",
" * Set covering:\n",
" If we, however, allow that an object is selected _more than one time_,\n",
" then the solution is z = 45 (i.e. less cost than the first problem),\n",
" and the alternatives\n",
" 4, 8, and 10\n",
" is selected, where object 5 is selected twice (alt. 4 and 8).\n",
" It's an unique solution as well.\n",
"\n",
"\n",
" Compare with the following models:\n",
" * MiniZinc: http://www.hakank.org/minizinc/set_covering4.mzn\n",
" * Comet : http://www.hakank.org/comet/set_covering4.co\n",
" * ECLiPSe : http://www.hakank.org/eclipse/set_covering4.ecl\n",
" * SICStus : http://www.hakank.org/sicstus/set_covering4.pl\n",
" * Gecode : http://www.hakank.org/gecode/set_covering4.cpp\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",
"\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "code",
"metadata": {},
"outputs": [],
"source": [
"from ortools.constraint_solver import pywrapcp\n",
"\n",
"\n",
"def main(set_partition=1):\n",
"\n",
" # Create the solver.\n",
" solver = pywrapcp.Solver(\"Set partition and set covering\")\n",
"\n",
" #\n",
" # data\n",
" #\n",
" num_alternatives = 10\n",
" num_objects = 8\n",
"\n",
" # costs for the alternatives\n",
" costs = [19, 16, 18, 13, 15, 19, 15, 17, 16, 15]\n",
"\n",
" # the alternatives, and their objects\n",
" a = [\n",
" # 1 2 3 4 5 6 7 8 the objects\n",
" [1, 0, 0, 0, 0, 1, 0, 0], # alternative 1\n",
" [0, 1, 0, 0, 0, 1, 0, 1], # alternative 2\n",
" [1, 0, 0, 1, 0, 0, 1, 0], # alternative 3\n",
" [0, 1, 1, 0, 1, 0, 0, 0], # alternative 4\n",
" [0, 1, 0, 0, 1, 0, 0, 0], # alternative 5\n",
" [0, 1, 1, 0, 0, 0, 0, 0], # alternative 6\n",
" [0, 1, 1, 1, 0, 0, 0, 0], # alternative 7\n",
" [0, 0, 0, 1, 1, 0, 0, 1], # alternative 8\n",
" [0, 0, 1, 0, 0, 1, 0, 1], # alternative 9\n",
" [1, 0, 0, 0, 0, 1, 1, 0] # alternative 10\n",
" ]\n",
"\n",
" #\n",
" # declare variables\n",
" #\n",
" x = [solver.IntVar(0, 1, \"x[%i]\" % i) for i in range(num_alternatives)]\n",
"\n",
" #\n",
" # constraints\n",
" #\n",
"\n",
" # sum the cost of the choosen alternative,\n",
" # to be minimized\n",
" z = solver.ScalProd(x, costs)\n",
"\n",
" #\n",
" for j in range(num_objects):\n",
" if set_partition == 1:\n",
" solver.Add(\n",
" solver.SumEquality([x[i] * a[i][j] for i in range(num_alternatives)],\n",
" 1))\n",
" else:\n",
" solver.Add(\n",
" solver.SumGreaterOrEqual(\n",
" [x[i] * a[i][j] for i in range(num_alternatives)], 1))\n",
"\n",
" objective = solver.Minimize(z, 1)\n",
"\n",
" #\n",
" # solution and search\n",
" #\n",
" solution = solver.Assignment()\n",
" solution.Add(x)\n",
" solution.AddObjective(z)\n",
"\n",
" collector = solver.LastSolutionCollector(solution)\n",
" solver.Solve(\n",
" solver.Phase([x[i] for i in range(num_alternatives)],\n",
" solver.INT_VAR_DEFAULT, solver.INT_VALUE_DEFAULT),\n",
" [collector, objective])\n",
"\n",
" print(\"z:\", collector.ObjectiveValue(0))\n",
" print(\n",
" \"selected alternatives:\",\n",
" [i + 1 for i in range(num_alternatives) if collector.Value(0, x[i]) == 1])\n",
"\n",
" print(\"failures:\", solver.Failures())\n",
" print(\"branches:\", solver.Branches())\n",
" print(\"WallTime:\", solver.WallTime())\n",
"\n",
"\n",
"print(\"Set partition:\")\n",
"main(1)\n",
"\n",
"print(\"\\nSet covering:\")\n",
"main(0)\n",
"\n"
]
}
],
"metadata": {
"language_info": {
"name": "python"
}
},
"nbformat": 4,
"nbformat_minor": 5
}