From 7d46eb382c89954d7c0c6b9df7a30cb3641f62e0 Mon Sep 17 00:00:00 2001 From: wout4 Date: Tue, 4 Jun 2024 17:33:38 +0200 Subject: [PATCH 1/4] fix maximum decomposition --- cpmpy/expressions/globalfunctions.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/cpmpy/expressions/globalfunctions.py b/cpmpy/expressions/globalfunctions.py index 82bc00174..c7cd4505b 100644 --- a/cpmpy/expressions/globalfunctions.py +++ b/cpmpy/expressions/globalfunctions.py @@ -174,8 +174,7 @@ def decompose_comparison(self, cpm_op, cpm_rhs): """ from .python_builtins import any, all if cpm_op == "==": # can avoid creating aux var here - return [any(x >= cpm_rhs for x in self.args), - all(x <= cpm_rhs for x in self.args)], [] + return [any(x >= cpm_rhs for x in self.args)], [all(x <= cpm_rhs for x in self.args)] lb, ub = self.get_bounds() _max = intvar(lb, ub) From 1dfc1f8651d2daac02b1de9774753de09ded566c Mon Sep 17 00:00:00 2001 From: wout4 Date: Tue, 4 Jun 2024 17:36:28 +0200 Subject: [PATCH 2/4] fix minimum decomposition --- cpmpy/expressions/globalfunctions.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/cpmpy/expressions/globalfunctions.py b/cpmpy/expressions/globalfunctions.py index c7cd4505b..73afd3d37 100644 --- a/cpmpy/expressions/globalfunctions.py +++ b/cpmpy/expressions/globalfunctions.py @@ -133,8 +133,7 @@ def decompose_comparison(self, cpm_op, cpm_rhs): """ from .python_builtins import any, all if cpm_op == "==": # can avoid creating aux var - return [any(x <= cpm_rhs for x in self.args), - all(x >= cpm_rhs for x in self.args)], [] + return [any(x <= cpm_rhs for x in self.args)], [all(x >= cpm_rhs for x in self.args)] lb, ub = self.get_bounds() _min = intvar(lb, ub) From 8c711e2087c657d9427194cb311c25ff9fe3f03f Mon Sep 17 00:00:00 2001 From: wout4 Date: Tue, 4 Jun 2024 22:47:13 +0200 Subject: [PATCH 3/4] remove simplification from min and max --- cpmpy/expressions/globalfunctions.py | 6 ------ 1 file changed, 6 deletions(-) diff --git a/cpmpy/expressions/globalfunctions.py b/cpmpy/expressions/globalfunctions.py index 73afd3d37..ed43c2589 100644 --- a/cpmpy/expressions/globalfunctions.py +++ b/cpmpy/expressions/globalfunctions.py @@ -132,9 +132,6 @@ def decompose_comparison(self, cpm_op, cpm_rhs): they should be enforced toplevel. """ from .python_builtins import any, all - if cpm_op == "==": # can avoid creating aux var - return [any(x <= cpm_rhs for x in self.args)], [all(x >= cpm_rhs for x in self.args)] - lb, ub = self.get_bounds() _min = intvar(lb, ub) return [eval_comparison(cpm_op, _min, cpm_rhs)], \ @@ -172,9 +169,6 @@ def decompose_comparison(self, cpm_op, cpm_rhs): they should be enforced toplevel. """ from .python_builtins import any, all - if cpm_op == "==": # can avoid creating aux var here - return [any(x >= cpm_rhs for x in self.args)], [all(x <= cpm_rhs for x in self.args)] - lb, ub = self.get_bounds() _max = intvar(lb, ub) return [eval_comparison(cpm_op, _max, cpm_rhs)], \ From 7e86304c53d506c8806f690152fb79e06806abc8 Mon Sep 17 00:00:00 2001 From: wout4 Date: Tue, 4 Jun 2024 23:38:07 +0200 Subject: [PATCH 4/4] update tests --- tests/test_transf_decompose.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/test_transf_decompose.py b/tests/test_transf_decompose.py index e7649b166..afe9c2b83 100644 --- a/tests/test_transf_decompose.py +++ b/tests/test_transf_decompose.py @@ -89,15 +89,15 @@ def test_decompose_nested(self): cons = [min(ivs) == max(ivs)] self.assertEqual(str(decompose_in_tree(cons, supported={"min"})), - "[or([(x) >= (min(x,y,z)), (y) >= (min(x,y,z)), (z) >= (min(x,y,z))]), (x) <= (min(x,y,z)), (y) <= (min(x,y,z)), (z) <= (min(x,y,z))]") + '[(IV0) == (min(x,y,z)), or([(x) >= (IV0), (y) >= (IV0), (z) >= (IV0)]), (x) <= (IV0), (y) <= (IV0), (z) <= (IV0)]') self.assertEqual(str(decompose_in_tree(cons, supported={"max"})), - "[or([(x) <= (max(x,y,z)), (y) <= (max(x,y,z)), (z) <= (max(x,y,z))]), (x) >= (max(x,y,z)), (y) >= (max(x,y,z)), (z) >= (max(x,y,z))]") + "[(IV1) == (max(x,y,z)), or([(x) <= (IV1), (y) <= (IV1), (z) <= (IV1)]), (x) >= (IV1), (y) >= (IV1), (z) >= (IV1)]") # numerical in non-comparison context cons = [AllEqual([min(ivs[:-1]),ivs[-1]])] self.assertEqual(str(decompose_in_tree(cons, supported={"allequal"})), - "[allequal(IV0,z), ((x) <= (IV0)) or ((y) <= (IV0)), (x) >= (IV0), (y) >= (IV0)]") + "[allequal(IV2,z), (IV3) == (IV2), ((x) <= (IV3)) or ((y) <= (IV3)), (x) >= (IV3), (y) >= (IV3)]") self.assertEqual(str(decompose_in_tree(cons, supported={"min"})), "[(min(x,y)) == (z)]")