172 lines
4.9 KiB
Plaintext
172 lines
4.9 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_covering"
|
|
]
|
|
},
|
|
{
|
|
"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_covering.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_covering.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 covering in Google CP Solver.\n",
|
|
"\n",
|
|
" Placing of firestations, from Winston 'Operations Research', page 486.\n",
|
|
"\n",
|
|
" Compare with the following models:\n",
|
|
" * MiniZinc: http://www.hakank.org/minizinc/set_covering.mzn\n",
|
|
" * ECLiPSe : http://www.hakank.org/eclipse/set_covering.ecl\n",
|
|
" * Comet : http://www.hakank.org/comet/set_covering.co\n",
|
|
" * Gecode : http://www.hakank.org/gecode/set_covering.cpp\n",
|
|
" * SICStus : http://www.hakank.org/sicstus/set_covering.pl\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(unused_argv):\n",
|
|
"\n",
|
|
" # Create the solver.\n",
|
|
" solver = pywrapcp.Solver(\"Set covering\")\n",
|
|
"\n",
|
|
" #\n",
|
|
" # data\n",
|
|
" #\n",
|
|
" min_distance = 15\n",
|
|
" num_cities = 6\n",
|
|
"\n",
|
|
" distance = [[0, 10, 20, 30, 30, 20], [10, 0, 25, 35, 20, 10],\n",
|
|
" [20, 25, 0, 15, 30, 20], [30, 35, 15, 0, 15, 25],\n",
|
|
" [30, 20, 30, 15, 0, 14], [20, 10, 20, 25, 14, 0]]\n",
|
|
"\n",
|
|
" #\n",
|
|
" # declare variables\n",
|
|
" #\n",
|
|
" x = [solver.IntVar(0, 1, \"x[%i]\" % i) for i in range(num_cities)]\n",
|
|
"\n",
|
|
" #\n",
|
|
" # constraints\n",
|
|
" #\n",
|
|
"\n",
|
|
" # objective to minimize\n",
|
|
" z = solver.Sum(x)\n",
|
|
"\n",
|
|
" # ensure that all cities are covered\n",
|
|
" for i in range(num_cities):\n",
|
|
" b = [x[j] for j in range(num_cities) if distance[i][j] <= min_distance]\n",
|
|
" solver.Add(solver.SumGreaterOrEqual(b, 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 + [z], solver.INT_VAR_DEFAULT, solver.INT_VALUE_DEFAULT),\n",
|
|
" [collector, objective])\n",
|
|
"\n",
|
|
" print(\"z:\", collector.ObjectiveValue(0))\n",
|
|
" print(\"x:\", [collector.Value(0, x[i]) for i in range(num_cities)])\n",
|
|
"\n",
|
|
" print(\"failures:\", solver.Failures())\n",
|
|
" print(\"branches:\", solver.Branches())\n",
|
|
" print(\"WallTime:\", solver.WallTime())\n",
|
|
"\n",
|
|
"\n",
|
|
"main(\"cp sample\")\n",
|
|
"\n"
|
|
]
|
|
}
|
|
],
|
|
"metadata": {
|
|
"language_info": {
|
|
"name": "python"
|
|
}
|
|
},
|
|
"nbformat": 4,
|
|
"nbformat_minor": 5
|
|
}
|