Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Normalize numexpr in decompose in tree #378

Merged
merged 3 commits into from
Jul 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion cpmpy/transformations/decompose_global.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from ..expressions.variables import _BoolVarImpl, intvar, boolvar, _NumVarImpl, cpm_array, NDVarArray
from ..expressions.utils import is_any_list, eval_comparison
from ..expressions.python_builtins import all
from .flatten_model import flatten_constraint
from .flatten_model import flatten_constraint, normalized_numexpr


def decompose_in_tree(lst_of_expr, supported=set(), supported_reified=set(), _toplevel=None, nested=False):
Expand Down Expand Up @@ -47,6 +47,9 @@ def decompose_in_tree(lst_of_expr, supported=set(), supported_reified=set(), _to
continue

elif isinstance(expr, Operator):
if any(isinstance(a,GlobalFunction) for a in expr.args):
expr, base_con = normalized_numexpr(expr)
_toplevel.extend(base_con) # should be added toplevel
# recurse into arguments, recreate through constructor (we know it stores no other state)
args = decompose_in_tree(expr.args, supported, supported_reified, _toplevel, nested=True)
newlist.append(Operator(expr.name, args))
Expand Down
2 changes: 1 addition & 1 deletion cpmpy/transformations/flatten_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -536,7 +536,7 @@ def normalized_numexpr(expr):
newexpr = Operator(expr.name, flatvars)
return (newexpr, [c for con in flatcons for c in con])
else:
# Global constraint (non-Boolean) (examples: Max,Min,Element)
# Globalfunction (examples: Max,Min,Element)

# just recursively flatten args, which can be lists
if all(__is_flat_var_or_list(arg) for arg in expr.args):
Expand Down