Skip to content

Commit

Permalink
only decompose if has_nested
Browse files Browse the repository at this point in the history
  • Loading branch information
Wout4 committed May 17, 2024
1 parent d79e5f4 commit 1565209
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 7 deletions.
3 changes: 0 additions & 3 deletions cpmpy/expressions/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,9 +135,6 @@ def __repr__(self):
def __hash__(self):
return hash(self.__repr__())

def has_nested(self):
return not all([is_leaf(x) for x in self.args])

def is_leaf(self):
return False # default

Expand Down
11 changes: 10 additions & 1 deletion cpmpy/expressions/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,16 @@ def argval(a):


def is_leaf(a):
return a.is_leaf() if hasattr(a, 'is_leaf') else True
if hasattr(a, 'is_leaf'):
return a.is_leaf()
if is_any_list(a):
return all([is_leaf(x) for x in a])
else:
return True


def has_nested(expr):
return not all([is_leaf(x) for x in expr.args])


def eval_comparison(str_op, lhs, rhs):
Expand Down
7 changes: 4 additions & 3 deletions cpmpy/transformations/decompose_global.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from ..expressions.globalfunctions import GlobalFunction
from ..expressions.core import Expression, Comparison, Operator, BoolVal
from ..expressions.variables import _BoolVarImpl, intvar, boolvar, _NumVarImpl, cpm_array, NDVarArray
from ..expressions.utils import is_any_list, eval_comparison
from ..expressions.utils import is_any_list, eval_comparison, has_nested
from ..expressions.python_builtins import all
from .flatten_model import flatten_constraint, normalized_numexpr

Expand Down Expand Up @@ -37,8 +37,9 @@ def decompose_in_tree(lst_of_expr, supported=set(), supported_reified=set(), _to

newlist = [] # decomposed constraints will go here
for expr in lst_of_expr:

if is_any_list(expr):
if not has_nested(expr):
newlist.append(expr)
elif is_any_list(expr):
assert nested is True, "Cannot have nested lists without passing trough an expression, make sure to run cpmpy.transformations.normalize.toplevel_list first."
newexpr = decompose_in_tree(expr, supported, supported_reified, _toplevel, nested=True)
if isinstance(expr, NDVarArray):
Expand Down

0 comments on commit 1565209

Please sign in to comment.