Skip to content

Commit

Permalink
Move -IV (operator) correctly to lhs
Browse files Browse the repository at this point in the history
  • Loading branch information
Dimosts committed Sep 24, 2023
1 parent 5112111 commit d53dcb6
Showing 1 changed file with 17 additions and 23 deletions.
40 changes: 17 additions & 23 deletions cpmpy/transformations/linearize.py
Original file line number Diff line number Diff line change
Expand Up @@ -264,35 +264,29 @@ def canonical_comparison(lst_of_expr):
if isinstance(cpm_expr, Comparison):
lhs, rhs = cpm_expr.args

# bring all vars to lhs
if is_num(lhs) or isinstance(lhs, _NumVarImpl) or (isinstance(lhs, Operator) and lhs.name in {"sum", "wsum"}):
# bring all vars to lhs
lhs2 = []
if isinstance(rhs, _NumVarImpl):
if isinstance(lhs, Operator) and lhs.name == "sum":
lhs, rhs = sum([1 * a for a in lhs.args] + [-1 * rhs]), 0
elif isinstance(lhs, _NumVarImpl) or (isinstance(lhs, Operator) and lhs.name == "wsum"):
lhs, rhs = lhs + -1 * rhs, 0
else:
raise ValueError(
f"unexpected expression on lhs of expression, should be sum,wsum or intvar but got {lhs}")
lhs2, rhs = [-1 * rhs], 0
elif isinstance(rhs, Operator) and rhs.name == "sum":
if isinstance(lhs, Operator) and lhs.name == "sum":
lhs, rhs = sum([1 * a for a in lhs.args] + [-1 * b for b in rhs.args
if (isinstance(b, _NumVarImpl) or isinstance(b, Operator))]), sum(b for b in rhs.args if is_num(b))
elif isinstance(lhs, _NumVarImpl) or (isinstance(lhs, Operator) and lhs.name == "wsum"):
lhs, rhs = lhs + [-1 * b for b in rhs.args
if (isinstance(b, _NumVarImpl) or isinstance(b, Operator))], sum(b for b in rhs.args if is_num(b))
lhs2, rhs = [-1 * b if isinstance(b, _NumVarImpl) else 1 * b for b in rhs.args
if isinstance(b, _NumVarImpl) or isinstance(b, Operator)], \
sum(b for b in rhs.args if is_num(b))
elif isinstance(rhs, Operator) and rhs.name == "wsum":
if isinstance(lhs, Operator) and lhs.name == "sum":
lhs, rhs = sum([1 * a for a in lhs.args] + [-a * b for a, b in zip(rhs.args[0], rhs.args[1])
if isinstance(b, _NumVarImpl)]), \
sum(-a * b for a, b in zip(rhs.args[0], rhs.args[1])
if not isinstance(b, _NumVarImpl))
elif isinstance(lhs, _NumVarImpl) or (isinstance(lhs, Operator) and lhs.name == "wsum"):
lhs, rhs = lhs + sum([-a * b for a, b in zip(rhs.args[0], rhs.args[1])
if isinstance(b, _NumVarImpl)]), \
sum(-a * b for a, b in zip(rhs.args[0], rhs.args[1])
lhs2, rhs = [-a * b for a, b in zip(rhs.args[0], rhs.args[1])
if isinstance(b, _NumVarImpl)], \
sum(-a * b for a, b in zip(rhs.args[0], rhs.args[1])
if not isinstance(b, _NumVarImpl))

if isinstance(lhs, Operator) and lhs.name == "sum":
lhs, rhs = sum([1 * a for a in lhs.args] + lhs2), rhs
elif isinstance(lhs, _NumVarImpl) or (isinstance(lhs, Operator) and lhs.name == "wsum"):
lhs, rhs = lhs + lhs2, rhs
else:
raise ValueError(
f"unexpected expression on lhs of expression, should be sum,wsum or intvar but got {lhs}")

assert not is_num(lhs), "lhs cannot be an integer at this point!"

# bring all const to rhs
Expand Down

0 comments on commit d53dcb6

Please sign in to comment.