Files
ortools-clone/examples/notebook/contrib/map.ipynb
2021-12-14 13:05:00 +01:00

183 lines
5.9 KiB
Plaintext

{
"cells": [
{
"cell_type": "markdown",
"id": "google",
"metadata": {},
"source": [
"##### Copyright 2021 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": [
"# map"
]
},
{
"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/master/examples/notebook/contrib/map.ipynb\"><img src=\"https://raw.githubusercontent.com/google/or-tools/master/tools/colab_32px.png\"/>Run in Google Colab</a>\n",
"</td>\n",
"<td>\n",
"<a href=\"https://github.com/google/or-tools/blob/master/examples/contrib/map.py\"><img src=\"https://raw.githubusercontent.com/google/or-tools/master/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": "code",
"execution_count": null,
"id": "code",
"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",
" Map coloring problem in Google CP Solver.\n",
"\n",
"\n",
" From Pascal Van Hentenryck 'The OPL Optimization Programming Language',\n",
" page 7, 42.\n",
"\n",
" Compare with the following models:\n",
" * Comet: http://www.hakank.org/comet/map.co\n",
" * Tailor/Essence': http://hakank.org/tailor/map_coloring.eprime\n",
" * SICStus: http://hakank.org/sicstus/map_coloring.pl\n",
" * ECLiPSe: http://hakank.org/eclipse/map.ecl\n",
" * Gecode: http://hakank.org/gecode/map.cpp\n",
" * MiniZinc: http://hakank.org/minizinc/map.mzn\n",
" * Zinc: http://hakank.org/minizinc/map.zinc\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 ortools.constraint_solver import pywrapcp\n",
"\n",
"\n",
"# Create the solver.\n",
"solver = pywrapcp.Solver(\"Map coloring\")\n",
"\n",
"#\n",
"# data\n",
"#\n",
"Belgium = 0\n",
"Denmark = 1\n",
"France = 2\n",
"Germany = 3\n",
"Netherlands = 4\n",
"Luxembourg = 5\n",
"\n",
"n = 6\n",
"max_num_colors = 4\n",
"\n",
"# declare variables\n",
"color = [solver.IntVar(1, max_num_colors, \"x%i\" % i) for i in range(n)]\n",
"\n",
"#\n",
"# constraints\n",
"#\n",
"solver.Add(color[Belgium] == 1) # Symmetry breaking\n",
"solver.Add(color[France] != color[Belgium])\n",
"solver.Add(color[France] != color[Luxembourg])\n",
"solver.Add(color[France] != color[Germany])\n",
"solver.Add(color[Luxembourg] != color[Germany])\n",
"solver.Add(color[Luxembourg] != color[Belgium])\n",
"solver.Add(color[Belgium] != color[Netherlands])\n",
"solver.Add(color[Belgium] != color[Germany])\n",
"solver.Add(color[Germany] != color[Netherlands])\n",
"solver.Add(color[Germany] != color[Denmark])\n",
"\n",
"#\n",
"# solution and search\n",
"#\n",
"solution = solver.Assignment()\n",
"solution.Add([color[i] for i in range(n)])\n",
"\n",
"collector = solver.AllSolutionCollector(solution)\n",
"# collector = solver.FirstSolutionCollector(solution)\n",
"# search_log = solver.SearchLog(100, x[0])\n",
"solver.Solve(\n",
" solver.Phase([color[i] for i in range(n)], solver.INT_VAR_SIMPLE,\n",
" solver.ASSIGN_MIN_VALUE), [collector])\n",
"\n",
"num_solutions = collector.SolutionCount()\n",
"print(\"num_solutions: \", num_solutions)\n",
"if num_solutions > 0:\n",
" for s in range(num_solutions):\n",
" colorval = [collector.Value(s, color[i]) for i in range(n)]\n",
" print(\"color:\", colorval)\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",
"else:\n",
" print(\"No solutions found\")\n",
"\n"
]
}
],
"metadata": {},
"nbformat": 4,
"nbformat_minor": 5
}