Skip to content

Commit

Permalink
missing argument
Browse files Browse the repository at this point in the history
  • Loading branch information
Wout4 committed Oct 9, 2023
1 parent d2f9f2a commit cc10b9e
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 11 deletions.
21 changes: 13 additions & 8 deletions cpmpy/expressions/globalconstraints.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ def my_circuit_decomp(self):
import warnings # for deprecation warning
import numpy as np
from ..exceptions import CPMpyException, IncompleteFunctionError, TypeError
from .core import Expression, Operator, Comparison
from .core import Expression, Operator, Comparison, BoolVal
from .variables import boolvar, intvar, cpm_array, _NumVarImpl, _IntVarImpl
from .utils import flatlist, all_pairs, argval, is_num, eval_comparison, is_any_list, is_boolexpr, get_bounds
from .globalfunctions import * # XXX make this file backwards compatible
Expand Down Expand Up @@ -284,6 +284,7 @@ def decompose(self):

return constraining, defining


def decompose_linear(self):
"""
Decomposition inspired by Miller-Tucker-Zemlin formulation for TSPs
Expand All @@ -295,22 +296,26 @@ def decompose_linear(self):

constraining = []
defining = []
defining += [AllDifferent(order)]
defining += [sum(row) == 1 for row in x] #every stop only has one successor
constraining += [AllDifferent(order)]
defining += [sum(row) <= 1 for row in x] #every stop only has one successor
constraining += [sum(col) == 1 for col in x.T] #every stop only has one predecessor
constraining += [succ[n-1] == 0] # symmetry breaking, last one is '0'
defining += [order[0] == 0] # symmetry breaking, last one is '0'

for i in range(n):
for j in range(n):
if i == j:
constraining += [x[i, j] <= 0] # cannot go from and to the same city
defining += [x[i, j] <= 0] # cannot go from and to the same city
else:
defining += [x[i, j].implies(succ[i] == j)] # link to `succ` variables
defining += [(succ[i] == j).implies(x[i, j])] # link to `succ` variables
if j != 0:
defining += [
x[i, j].implies(-1 * order[i] + 1 * order[j] == 1)] # eliminate subcircuits from solution
else:
if i != j:
defining += [
x[i, j].implies(order[i] <= order[j])] # eliminate subcircuits from solution
else:
pass
#defining += [x[i,j].implies(order[i] == i)] #symmetry breaking for negated circuits
elif j == 0:
constraining += [x[i,j].implies(order[i] == n - 1)] #the node that goes back to 0 is the last node, and therefor has the largest order_nb, being n - 1
pass

Expand Down
2 changes: 1 addition & 1 deletion cpmpy/transformations/decompose_global.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ def decompose_in_tree(lst_of_expr, supported=set(), supported_reified=set(), _to
return toplevel_list(newlist)
else:
# we are toplevel and some new constraints are introduced, decompose new constraints!
return toplevel_list(newlist) + decompose_in_tree(_toplevel, supported, supported_reified, nested=False)
return toplevel_list(newlist) + decompose_in_tree(_toplevel, supported, supported_reified, nested=False, linear=linear)


# DEPRECATED!
Expand Down
4 changes: 2 additions & 2 deletions tests/test_globalconstraints.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,8 +185,8 @@ def test_linear_circuit(self):
var._value = val
self.assertFalse(cp.Circuit(x).value())

self.assertEqual(len(circuit_sols),1)
self.assertEqual(len(not_circuit_sols),2)
self.assertEqual(len(circuit_sols),2)
self.assertEqual(len(not_circuit_sols),5)

self.assertEqual(total, len(circuit_sols) + len(not_circuit_sols))

Expand Down

0 comments on commit cc10b9e

Please sign in to comment.