forked from google/or-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
circuit.py
132 lines (99 loc) · 3.13 KB
/
circuit.py
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# Copyright 2010 Hakan Kjellerstrand [email protected]
#
# 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.
"""
Decomposition of the circuit constraint in Google CP Solver.
Cf Global constraint catalog:
http://www.emn.fr/x-info/sdemasse/gccat/Ccircuit.html
Solution of n=4:
x: [2, 0, 3, 1]
x: [3, 0, 1, 2]
x: [1, 3, 0, 2]
x: [3, 2, 0, 1]
x: [1, 2, 3, 0]
x: [2, 3, 1, 0]
The 'orbit' method that is used here is based on some
observations on permutation orbits.
Compare with the following models:
* MiniZinc: http://www.hakank.org/minizinc/circuit_test.mzn
* Gecode: http://www.hakank.org/gecode/circuit_orbit.mzn
This model was created by Hakan Kjellerstrand ([email protected])
Also see my other Google CP Solver models:
http://www.hakank.org/google_or_tools/
"""
from __future__ import print_function
import sys
from ortools.constraint_solver import pywrapcp
#
# circuit(x)
# constraints x to be an circuit
#
# Note: This assumes that x is has the domain 0..len(x)-1,
# i.e. 0-based.
#
def circuit(solver, x):
n = len(x)
z = [solver.IntVar(0, n - 1, "z%i" % i) for i in range(n)]
solver.Add(solver.AllDifferent(x))
solver.Add(solver.AllDifferent(z))
# put the orbit of x[0] in in z[0..n-1]
solver.Add(z[0] == x[0])
for i in range(1, n - 1):
# The following constraint give the error
# "TypeError: list indices must be integers, not IntVar"
# solver.Add(z[i] == x[z[i-1]])
# solution: use Element instead
solver.Add(z[i] == solver.Element(x, z[i - 1]))
#
# Note: At least one of the following two constraint must be set.
#
# may not be 0 for i < n-1
for i in range(1, n - 1):
solver.Add(z[i] != 0)
# when i = n-1 it must be 0
solver.Add(z[n - 1] == 0)
def main(n=5):
# Create the solver.
solver = pywrapcp.Solver("Send most money")
# data
print("n:", n)
# declare variables
# Note: domain should be 0..n-1
x = [solver.IntVar(0, n - 1, "x%i" % i) for i in range(n)]
#
# constraints
#
circuit(solver, x)
#
# solution and search
#
solution = solver.Assignment()
solution.Add(x)
collector = solver.AllSolutionCollector(solution)
solver.Solve(
solver.Phase(x, solver.CHOOSE_FIRST_UNBOUND, solver.ASSIGN_MIN_VALUE),
[collector])
num_solutions = collector.SolutionCount()
for s in range(num_solutions):
print("x:", [collector.Value(s, x[i]) for i in range(len(x))])
print()
print("num_solutions:", num_solutions)
print("failures:", solver.Failures())
print("branches:", solver.Branches())
print("WallTime:", solver.WallTime())
print()
n = 5
if __name__ == "__main__":
if len(sys.argv) > 1:
n = int(sys.argv[1])
main(n)