Expression utilities (cpmpy.expressions.utils)

Internal utilities for expression handling.

is_bool

is it a boolean (incl numpy variants)

is_int

can it be interpreted as an integer? (incl bool and numpy variants)

is_num

is it an int or float? (incl numpy variants)

is_false_cst

is the argument the constant False (can be of type bool, np.bool and BoolVal)

is_true_cst

is the argument the constant True (can be of type bool, np.bool and BoolVal)

is_boolexpr

is the argument a boolean expression or a boolean value

is_pure_list

is it a list or tuple?

is_any_list

is it a list or tuple or numpy array?

flatlist

recursively flatten arguments into one single list

all_pairs

returns all pairwise combinations of elements in args

argval

returns .value() of Expression, otherwise the variable itself

argvals

eval_comparison

Internal function: evaluates the textual str_op comparison operator lhs <str_op> rhs

get_bounds

return the bounds of the expression returns appropriately rounded integers

cpmpy.expressions.utils.all_pairs(args)[source]

returns all pairwise combinations of elements in args

cpmpy.expressions.utils.argval(a)[source]

returns .value() of Expression, otherwise the variable itself

We check with hasattr instead of isinstance to avoid circular dependency

cpmpy.expressions.utils.argvals(arr)[source]
cpmpy.expressions.utils.argvals_intexpr(lst: Iterable[int | Expression]) list[int] | None[source]

A well-typed helper function to get the values of a list of int|Expression, or None if any expression is not assigned

cpmpy.expressions.utils.eval_comparison(str_op, lhs, rhs)[source]

Internal function: evaluates the textual str_op comparison operator lhs <str_op> rhs

Valid str_op’s: * ‘==’ * ‘!=’ * ‘>’ * ‘>=’ * ‘<’ * ‘<=’

Especially useful in decomposition and transformation functions that already involve a comparison.

cpmpy.expressions.utils.flatlist(args)[source]

recursively flatten arguments into one single list

cpmpy.expressions.utils.get_bounds(expr)[source]

return the bounds of the expression returns appropriately rounded integers

cpmpy.expressions.utils.get_bounds_intexpr(lst: Iterable[int | Expression]) tuple[list[int], list[int]][source]

A well-typed helper function to get the bounds of a list of int|Expression’s

cpmpy.expressions.utils.get_nonneg_args(args, condition=None)[source]

Replace arguments with negative lowerbound with their nonnegative counterpart :param - args: list of expressions :param - condition: list of boolean expressions, indicating whether the argument is present or not (e.g., optional tasks)

cpmpy.expressions.utils.implies(expr: NDVarArray, other: BoolExprLike, simplify: bool = False) NDVarArray[source]
cpmpy.expressions.utils.implies(expr: Expression | bool | np.bool_, other: BoolExprLike, simplify: bool = False) Expression

Implication constraint: self -> other.

Like implies(), but also safe when ‘expr’ is not an Expression

Parameters:
  • expr (NDVarArray|BoolExprLike) – the left-hand-side of the implication

  • other (BoolExprLike) – the right-hand-side of the implication

  • simplify (bool) – if True, simplify by eliminating True/False constants (might remove expressions & their variables from user-view)

Returns:

the implication constraint or a BoolVal if simplified

Return type:

Expression

Simplification rules:
  • Expr -> True :: BoolVal(True) (by expr.implies())

  • Expr -> False :: ~Expr (by expr.implies())

  • True -> other :: other

  • False -> other :: BoolVal(True)

cpmpy.expressions.utils.is_any_list(arg) TypeGuard[list | tuple | ndarray][source]

is it a list or tuple or numpy array?

cpmpy.expressions.utils.is_bool(arg)[source]

is it a boolean (incl numpy variants)

cpmpy.expressions.utils.is_boolexpr(expr)[source]

is the argument a boolean expression or a boolean value

cpmpy.expressions.utils.is_false_cst(arg)[source]

is the argument the constant False (can be of type bool, np.bool and BoolVal)

cpmpy.expressions.utils.is_int(arg)[source]

can it be interpreted as an integer? (incl bool and numpy variants)

cpmpy.expressions.utils.is_num(arg)[source]

is it an int or float? (incl numpy variants)

cpmpy.expressions.utils.is_pure_list(arg)[source]

is it a list or tuple?

cpmpy.expressions.utils.is_star(arg)[source]

Check if arg is star as used in the ShortTable global constraint

cpmpy.expressions.utils.is_true_cst(arg)[source]

is the argument the constant True (can be of type bool, np.bool and BoolVal)

cpmpy.expressions.utils.npint2int(iter: Iterable[ExprLike]) tuple[int | Expression, ...][source]

Convert numpy values in iterable to Python integers, return as tuple.