map.py 3.05 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
# Copyright 2010 Hakan Kjellerstrand hakank@gmail.com
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""

  Map coloring problem in Google CP Solver.


  From Pascal Van Hentenryck 'The OPL Optimization Programming Language',
  page 7, 42.

  Compare with the following models:
  * Comet: http://www.hakank.org/comet/map.co
  * Tailor/Essence': http://hakank.org/tailor/map_coloring.eprime
  * SICStus: http://hakank.org/sicstus/map_coloring.pl
  * ECLiPSe: http://hakank.org/eclipse/map.ecl
  * Gecode: http://hakank.org/gecode/map.cpp
  * MiniZinc: http://hakank.org/minizinc/map.mzn
  * Zinc: http://hakank.org/minizinc/map.zinc

  This model was created by Hakan Kjellerstrand (hakank@gmail.com)
  Also see my other Google CP Solver models:
  http://www.hakank.org/google_or_tools/
"""
from __future__ import print_function
from ortools.constraint_solver import pywrapcp


def main():
  # Create the solver.
  solver = pywrapcp.Solver("Map coloring")

  #
  # data
  #
  Belgium = 0
  Denmark = 1
  France = 2
  Germany = 3
  Netherlands = 4
  Luxembourg = 5

  n = 6
  max_num_colors = 4

  # declare variables
  color = [solver.IntVar(1, max_num_colors, "x%i" % i) for i in range(n)]

  #
  # constraints
  #
  solver.Add(color[Belgium] == 1)  # Symmetry breaking
  solver.Add(color[France] != color[Belgium])
  solver.Add(color[France] != color[Luxembourg])
  solver.Add(color[France] != color[Germany])
  solver.Add(color[Luxembourg] != color[Germany])
  solver.Add(color[Luxembourg] != color[Belgium])
  solver.Add(color[Belgium] != color[Netherlands])
  solver.Add(color[Belgium] != color[Germany])
  solver.Add(color[Germany] != color[Netherlands])
  solver.Add(color[Germany] != color[Denmark])

  #
  # solution and search
  #
  solution = solver.Assignment()
  solution.Add([color[i] for i in range(n)])

  collector = solver.AllSolutionCollector(solution)
  # collector = solver.FirstSolutionCollector(solution)
  # search_log = solver.SearchLog(100, x[0])
  solver.Solve(
      solver.Phase([color[i] for i in range(n)], solver.INT_VAR_SIMPLE,
                   solver.ASSIGN_MIN_VALUE), [collector])

  num_solutions = collector.SolutionCount()
  print("num_solutions: ", num_solutions)
  if num_solutions > 0:
    for s in range(num_solutions):
      colorval = [collector.Value(s, color[i]) for i in range(n)]
      print("color:", colorval)

    print()
    print("num_solutions:", num_solutions)
    print("failures:", solver.Failures())
    print("branches:", solver.Branches())
    print("WallTime:", solver.WallTime())

  else:
    print("No solutions found")


if __name__ == "__main__":
  main()