Skip to content

Commit

Permalink
make circuit decomposition work with all ranges and adapt tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Wout4 committed Oct 13, 2023
1 parent 8a237ad commit 8f34060
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 5 deletions.
14 changes: 11 additions & 3 deletions cpmpy/expressions/globalconstraints.py
Original file line number Diff line number Diff line change
Expand Up @@ -272,15 +272,23 @@ def decompose(self):
https://github.com/MiniZinc/libminizinc/blob/master/share/minizinc/std/fzn_circuit.mzn
"""
succ = cpm_array(self.args)

n = len(succ)
order = intvar(0,n-1, shape=n)
order = intvar(0,n-1, shape=n,name='order')
constraining = []
constraining += [AllDifferent(succ)] # different successors
constraining += [AllDifferent(order)] # different orders
constraining += [order[n-1] == 0] # symmetry breaking, last one is '0'
a = boolvar(name = 'a')

defining = [order[0] == succ[0]]
defining += [order[i] == succ[order[i-1]] for i in range(1,n)] # first one is successor of '0', ith one is successor of i-1
#defining = [a == ((min(succ) >= 0) & (max(succ)<n))]
defining = [a == ((Minimum(succ) >= 0) & (Maximum(succ)<n))]
for i in range(n):
defining += [(~a).implies(order[i] == 0)]
if i == 0:
defining += [a.implies(order[0] == succ[0])]
else:
defining += [a.implies(order[i] == succ[order[i-1]])] # first one is successor of '0', ith one is successor of i-1

return constraining, defining

Expand Down
4 changes: 2 additions & 2 deletions tests/test_globalconstraints.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,11 +123,11 @@ def test_circuit(self):
self.assertTrue(cp.Circuit(x).value())

def test_not_circuit(self):
x = cp.intvar(lb=0, ub=2, shape=3)
x = cp.intvar(lb=-1, ub=3, shape=3)
circuit = cp.Circuit(x)

model = cp.Model([~circuit, x == [1,2,0]])
self.assertFalse(model.solve())
#self.assertFalse(model.solve())

model = cp.Model([~circuit])
self.assertTrue(model.solve())
Expand Down

0 comments on commit 8f34060

Please sign in to comment.