Skip to content

Commit

Permalink
work on toplevel
Browse files Browse the repository at this point in the history
  • Loading branch information
Wout4 committed Mar 1, 2024
1 parent 9243c2b commit 8622226
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
3 changes: 1 addition & 2 deletions cpmpy/solvers/ortools.py
Original file line number Diff line number Diff line change
Expand Up @@ -330,9 +330,8 @@ def transform(self, cpm_expr):
cpm_cons = toplevel_list(cpm_expr)
supported = {"min", "max", "abs", "element", "alldifferent", "xor", "table", "cumulative", "circuit", "inverse"}
cpm_cons = decompose_in_tree(cpm_cons, supported)
cpm_cons = flatten_constraint(cpm_cons) # flat normal form
cpm_cons = reel_to_int(cpm_cons)
cpm_cons = flatten_constraint(cpm_cons)
cpm_cons = flatten_constraint(cpm_cons) # flat normal form
cpm_cons = reify_rewrite(cpm_cons, supported=frozenset(['sum', 'wsum'])) # constraints that support reification
cpm_cons = only_numexpr_equality(cpm_cons, supported=frozenset(["sum", "wsum", "sub"])) # supports >, <, !=
cpm_cons = only_bv_reifies(cpm_cons)
Expand Down
21 changes: 20 additions & 1 deletion cpmpy/transformations/normalize.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,15 @@ def reel_to_int(lst_of_expr):
lhs.args[0] = newcoeffs
expr.args = lhs, (factor * rhs)

elif isinstance(lhs,Expression) and lhs.name == 'mul':
c, e = left_extract_reel_from_mul(lhs)
if c is not None:
for i in range(1, 100):
r = i * c
if r == int(r):
break
expr.args = int(r) * e, i * rhs

if isinstance(rhs, Expression) and rhs.name == 'wsum':
coeffs, vars = rhs.args
factor = 1
Expand All @@ -80,6 +89,15 @@ def reel_to_int(lst_of_expr):
newcoeffs.append(int(c * factor))
rhs.args[0] = newcoeffs
expr.args = (factor * lhs), rhs

elif isinstance(rhs,Expression) and rhs.name == 'mul':
c, e = left_extract_reel_from_mul(rhs)
if c is not None:
for i in range(1, 100):
r = i * c
if r == int(r):
break
expr.args = i * lhs, int(r) * e
newlist.append(expr)
return newlist

Expand All @@ -98,9 +116,10 @@ def left_extract_reel_from_mul(expr):
return real, lhsexpr * rhs

elif expr.name == 'wsum':
return
return None, None
else:
assert False, 'must be an expression'
return None, None


def simplify_boolean(lst_of_expr, num_context=False):
Expand Down

0 comments on commit 8622226

Please sign in to comment.