diff --git a/cpmpy/expressions/python_builtins.py b/cpmpy/expressions/python_builtins.py index 76b24d7a7..a7c524932 100644 --- a/cpmpy/expressions/python_builtins.py +++ b/cpmpy/expressions/python_builtins.py @@ -22,7 +22,7 @@ import builtins # to use the original Python-builtins from .utils import is_false_cst, is_true_cst, is_any_list -from .variables import NDVarArray +from .variables import NDVarArray, cpm_array from .core import Expression, Operator from .globalfunctions import Minimum, Maximum, Abs from ..exceptions import CPMpyException @@ -136,7 +136,10 @@ def abs(element): if the element given is not a CPMpy expression, the built-in is called else an Absolute functional global constraint is constructed. """ - if is_any_list(element) or not isinstance(element, Expression): + if is_any_list(element): + return cpm_array([abs(elem) for elem in element]) + + if isinstance(element, Expression): return builtins.abs(element) return Abs(element) diff --git a/tests/test_builtins.py b/tests/test_builtins.py index 569aa3dba..84a00d4b6 100644 --- a/tests/test_builtins.py +++ b/tests/test_builtins.py @@ -35,6 +35,4 @@ def test_abs(self): model = cp.Model(cp.abs(iv[0]).decompose_comparison('!=', 4)) self.assertTrue(model.solve()) - self.assertNotEqual(str(cp.abs(iv[0].value())), '4') - - self.assertRaises(CPMpyException) + self.assertNotEqual(str(cp.abs(iv[0].value())), '4') \ No newline at end of file