Skip to content

Commit

Permalink
Merge branch 'extended_testsuite' of github.com:CPMpy/cpmpy into exte…
Browse files Browse the repository at this point in the history
…nded_testsuite
  • Loading branch information
IgnaceBleukx committed May 8, 2024
2 parents 88705eb + daf505d commit 23b89b0
Show file tree
Hide file tree
Showing 6 changed files with 119 additions and 132 deletions.
26 changes: 10 additions & 16 deletions cpmpy/expressions/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,8 @@
import numpy as np


from .utils import is_num, is_any_list, flatlist, argval, get_bounds, is_boolexpr, is_true_cst, is_false_cst
from ..exceptions import IncompleteFunctionError, TypeError
from .utils import is_num, is_any_list, flatlist, get_bounds, is_boolexpr, is_true_cst, is_false_cst, argvals
from ..exceptions import TypeError


class Expression(object):
Expand Down Expand Up @@ -400,10 +400,7 @@ def __repr__(self):
# return the value of the expression
# optional, default: None
def value(self):
try:
arg_vals = [argval(a) for a in self.args]
except IncompleteFunctionError:
return False
arg_vals = argvals(self.args)

if any(a is None for a in arg_vals): return None
if self.name == "==": return arg_vals[0] == arg_vals[1]
Expand Down Expand Up @@ -533,14 +530,9 @@ def value(self):

if self.name == "wsum":
# wsum: arg0 is list of constants, no .value() use as is
arg_vals = [self.args[0], [argval(arg) for arg in self.args[1]]]
arg_vals = [self.args[0], argvals(self.args[1])]
else:
try:
arg_vals = [argval(arg) for arg in self.args]
except IncompleteFunctionError as e:
if self.is_bool(): return False
raise e

arg_vals = argvals(self.args)

if any(a is None for a in arg_vals): return None
# non-boolean
Expand All @@ -555,10 +547,12 @@ def value(self):
try:
return arg_vals[0] // arg_vals[1]
except ZeroDivisionError:
raise IncompleteFunctionError(f"Division by zero during value computation for expression {self}")
return np.nan # we use NaN to represent undefined values

# boolean context, replace NaN with False
arg_vals = [False if np.isnan(x) else x for x in arg_vals]

# boolean
elif self.name == "and": return all(arg_vals)
if self.name == "and": return all(arg_vals)
elif self.name == "or" : return any(arg_vals)
elif self.name == "->": return (not arg_vals[0]) or arg_vals[1]
elif self.name == "not": return not arg_vals[0]
Expand Down
Loading

0 comments on commit 23b89b0

Please sign in to comment.