2020-03-04 14:33:52 +01:00
{
"cells": [
2020-09-27 16:03:47 +02:00
{
"cell_type": "markdown",
2021-12-14 13:05:00 +01:00
"id": "google",
2020-09-27 16:03:47 +02:00
"metadata": {},
"source": [
2025-02-04 18:04:03 +01:00
"##### Copyright 2025 Google LLC."
2020-09-27 16:03:47 +02:00
]
},
{
"cell_type": "markdown",
2021-12-14 13:05:00 +01:00
"id": "apache",
2020-09-27 16:03:47 +02:00
"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",
2021-12-14 13:05:00 +01:00
"id": "basename",
2020-09-27 16:03:47 +02:00
"metadata": {},
"source": [
"# map"
]
},
{
"cell_type": "markdown",
2021-12-14 13:05:00 +01:00
"id": "link",
2020-09-27 16:03:47 +02:00
"metadata": {},
"source": [
"<table align=\"left\">\n",
"<td>\n",
2022-06-27 15:42:26 +02:00
"<a href=\"https://colab.research.google.com/github/google/or-tools/blob/main/examples/notebook/contrib/map.ipynb\"><img src=\"https://raw.githubusercontent.com/google/or-tools/main/tools/colab_32px.png\"/>Run in Google Colab</a>\n",
2020-09-27 16:03:47 +02:00
"</td>\n",
"<td>\n",
2022-06-27 15:42:26 +02:00
"<a href=\"https://github.com/google/or-tools/blob/main/examples/contrib/map.py\"><img src=\"https://raw.githubusercontent.com/google/or-tools/main/tools/github_32px.png\"/>View source on GitHub</a>\n",
2020-09-27 16:03:47 +02:00
"</td>\n",
"</table>"
]
},
{
"cell_type": "markdown",
2021-12-14 13:05:00 +01:00
"id": "doc",
2020-09-27 16:03:47 +02:00
"metadata": {},
"source": [
"First, you must install [ortools](https://pypi.org/project/ortools/) package in this colab."
]
},
{
"cell_type": "code",
"execution_count": null,
2021-12-14 13:05:00 +01:00
"id": "install",
2020-09-27 16:03:47 +02:00
"metadata": {},
"outputs": [],
"source": [
2023-10-09 18:30:05 +02:00
"%pip install ortools"
2020-09-27 16:03:47 +02:00
]
},
2020-03-04 14:33:52 +01:00
{
2022-04-14 14:07:58 +02:00
"cell_type": "markdown",
"id": "description",
2020-03-04 14:33:52 +01:00
"metadata": {},
"source": [
2022-04-14 14:07:58 +02:00
"\n",
2020-03-04 14:33:52 +01:00
"\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",
2022-04-14 14:07:58 +02:00
" http://www.hakank.org/google_or_tools/\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "code",
"metadata": {},
"outputs": [],
"source": [
2020-03-04 14:33:52 +01:00
"from ortools.constraint_solver import pywrapcp\n",
"\n",
"\n",
2022-04-14 14:07:58 +02:00
"def main():\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",
"\n",
"main()\n",
2020-03-04 14:33:52 +01:00
"\n"
]
}
],
2025-02-04 18:04:03 +01:00
"metadata": {
"language_info": {
"name": "python"
}
},
2020-03-04 14:33:52 +01:00
"nbformat": 4,
2021-09-30 01:24:08 +02:00
"nbformat_minor": 5
2020-03-04 14:33:52 +01:00
}