Files
ortools-clone/examples/python/alldifferent_except_0.py

127 lines
3.6 KiB
Python
Raw Normal View History

2018-02-19 15:21:05 +01:00
# Copyright 2010 Hakan Kjellerstrand hakank@gmail.com
2010-10-06 21:16:22 +00:00
#
# 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
2010-10-06 21:16:22 +00:00
#
# http://www.apache.org/licenses/LICENSE-2.0
2010-10-06 21:16:22 +00:00
#
# 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
2010-10-06 21:16:22 +00:00
# limitations under the License.
"""
All different except 0 Google CP Solver.
2010-10-06 21:16:22 +00:00
Decomposition of global constraint alldifferent_except_0.
2010-10-06 21:16:22 +00:00
From Global constraint catalogue:
http://www.emn.fr/x-info/sdemasse/gccat/Calldifferent_except_0.html
'''
Enforce all variables of the collection VARIABLES to take distinct
2010-10-06 21:16:22 +00:00
values, except those variables that are assigned to 0.
2010-10-06 21:16:22 +00:00
Example
(<5, 0, 1, 9, 0, 3>)
The alldifferent_except_0 constraint holds since all the values
2010-10-06 21:16:22 +00:00
(that are different from 0) 5, 1, 9 and 3 are distinct.
'''
Compare with the following models:
* Comet: http://hakank.org/comet/alldifferent_except_0.co
* ECLiPSe: http://hakank.org/eclipse/alldifferent_except_0.ecl
* Tailor/Essence': http://hakank.org/tailor/alldifferent_except_0.eprime
* Gecode: http://hakank.org/gecode/alldifferent_except_0.cpp
* Gecode/R: http://hakank.org/gecode_r/all_different_except_0.rb
* MiniZinc: http://hakank.org/minizinc/alldifferent_except_0.mzn
* SICStus_ http://hakank.org/sicstus/alldifferent_except_0.pl
* Choco: http://hakank.org/choco/AllDifferentExcept0_test.java
* JaCoP: http://hakank.org/JaCoP/AllDifferentExcept0_test.java
* Zinc: http://hakank.org/minizinc/alldifferent_except_0.zinc
2018-02-19 15:21:05 +01:00
This model was created by Hakan Kjellerstrand (hakank@gmail.com)
2014-05-22 20:13:16 +00:00
Also see my other Google CP Solver models:
http://www.hakank.org/google_or_tools/
2010-10-06 21:16:22 +00:00
"""
from __future__ import print_function
2010-10-06 21:16:22 +00:00
from ortools.constraint_solver import pywrapcp
2010-10-06 21:16:22 +00:00
#
# Decomposition of alldifferent_except_0
# Thanks to Laurent Perron (Google) for
# suggestions of improvements.
#
2014-05-22 20:13:16 +00:00
2010-10-06 21:16:22 +00:00
def alldifferent_except_0(solver, a):
2014-05-22 20:13:16 +00:00
n = len(a)
for i in range(n):
for j in range(i):
solver.Add((a[i] != 0) * (a[j] != 0) <= (a[i] != a[j]))
2010-10-06 21:16:22 +00:00
2018-06-11 11:51:18 +02:00
2010-10-06 21:16:22 +00:00
# more compact version:
2014-05-22 20:13:16 +00:00
def alldifferent_except_0_b(solver, a):
n = len(a)
2018-06-11 11:51:18 +02:00
[
solver.Add((a[i] != 0) * (a[j] != 0) <= (a[i] != a[j]))
for i in range(n)
for j in range(i)
]
2010-10-06 21:16:22 +00:00
def main(unused_argv):
2014-05-22 20:13:16 +00:00
# Create the solver.
solver = pywrapcp.Solver("Alldifferent except 0")
# data
n = 7
# declare variables
x = [solver.IntVar(0, n - 1, "x%i" % i) for i in range(n)]
# Number of zeros.
z = solver.Sum([x[i] == 0 for i in range(n)]).VarWithName("z")
#
# constraints
#
alldifferent_except_0(solver, x)
# we require 2 0's
solver.Add(z == 2)
#
# solution and search
#
solution = solver.Assignment()
solution.Add([x[i] for i in range(n)])
solution.Add(z)
collector = solver.AllSolutionCollector(solution)
2018-06-11 11:51:18 +02:00
solver.Solve(
solver.Phase([x[i] for i in range(n)], solver.CHOOSE_FIRST_UNBOUND,
solver.ASSIGN_MIN_VALUE), [collector])
2014-05-22 20:13:16 +00:00
num_solutions = collector.SolutionCount()
for s in range(num_solutions):
print("x:", [collector.Value(s, x[i]) for i in range(n)])
print("z:", collector.Value(s, z))
print()
print("num_solutions:", num_solutions)
print("failures:", solver.Failures())
print("branches:", solver.Branches())
print("WallTime:", solver.WallTime())
2014-05-22 20:13:16 +00:00
if __name__ == "__main__":
main("cp sample")