Core expressions (cpmpy.expressions.core)

class cpmpy.expressions.core.BoolVal(arg: bool | bool)[source]

Wrapper for python or numpy BoolVals

property args: tuple[Any, ...]

READ-ONLY access to the expression’s arguments. Use update_args() to update the arguments.

Subclasses can override this property to return a more precisely typed tuple.

deepcopy(memodict={})

DEPRECATED: use copy.deepcopy() instead

Will be removed in stable version.

get_bounds() tuple[int, int][source]
has_subexpr() bool[source]

Does it contain nested Expressions (anything other than a _NumVarImpl or a constant)? Is of importance when deciding whether certain transformations are needed along particular paths of the expression tree.

implies(other: Expression | bool | bool, simplify: bool = False) Expression[source]

Implication constraint: BoolVal -> other.

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

  • simplify (bool) – simplify the implication, even if it means other dissappears from user-view

Simplification rule:
  • BoolVal(False) -> other :: BoolVal(True)

Note: BoolVal(True).implies(other) will always return other

Returns:

the implication constraint or a BoolVal or other

Return type:

Expression

is_bool() bool

is it a Boolean (return type) Operator? Default: yes

set_description(txt: str, override_print: bool = True, full_print: bool = False) None
update_args(args: Iterable[Any], has_subexpr: bool | None = None) None

Allows in-place update of the expression’s arguments. Resets all cached computations which depend on the expression tree.

  • args (Iterable[Any]): new arguments

  • has_subexpr (Optional[bool]): provide this if you know the answer already, to avoid computing it

value() bool[source]
class cpmpy.expressions.core.Comparison(name: str, left: Expression | int | integer | bool, right: Expression | int | integer | bool)[source]

Represents a comparison between two sub-expressions

allowed: Final = frozenset({'!=', '<', '<=', '==', '>', '>='})
property args: tuple[Any, ...]

READ-ONLY access to the expression’s arguments. Use update_args() to update the arguments.

Subclasses can override this property to return a more precisely typed tuple.

deepcopy(memodict={})

DEPRECATED: use copy.deepcopy() instead

Will be removed in stable version.

get_bounds() tuple[int, int]
has_subexpr() bool

Does it contain nested Expressions (anything other than a _NumVarImpl or a constant)? Is of importance when deciding whether certain transformations are needed along particular paths of the expression tree. Results are cached for future calls and reset when the expression changes (in-place argument update).

implies(other: Expression | bool | bool, simplify: bool = False) Expression

Implication constraint: self -> other.

Python does not offer relevant syntax for implication, call this method instead. For double reification (<->), use equivalence self == other.

Parameters:
  • 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:
  • self -> True :: BoolVal(True)

  • self -> False :: ~self (Boolean inversion)

is_bool() bool

is it a Boolean (return type) Operator? Default: yes

set_description(txt: str, override_print: bool = True, full_print: bool = False) None
update_args(args: Iterable[Any], has_subexpr: bool | None = None) None

Allows in-place update of the expression’s arguments. Resets all cached computations which depend on the expression tree.

  • args (Iterable[Any]): new arguments

  • has_subexpr (Optional[bool]): provide this if you know the answer already, to avoid computing it

value() bool | None[source]
class cpmpy.expressions.core.Description(text: str, override_print: bool = True, full_print: bool = False)[source]

Human-readable print metadata for an Expression; set via set_description().

full_print: bool
override_print: bool
text: str
class cpmpy.expressions.core.Expression(name: str, arg_list: tuple[Any, ...], has_subexpr: bool | None = None)[source]

An Expression represents a symbolic function with a self.name and self.args (arguments)

Each Expression is considered to be a function whose value can be used

in other expressions

Expressions may implement:

  • args: can override it with a narrower type for the arguments

  • is_bool(): whether its return type is Boolean

  • value(): the value of the expression, default None

  • implies(x): logical implication of this expression towards x

  • __repr__(): for pretty printing the expression

  • set_description(): optional custom __str__() text (class default _description is None; set on the instance when used)

  • any __op__ python operator overloading

property args: tuple[Any, ...]

READ-ONLY access to the expression’s arguments. Use update_args() to update the arguments.

Subclasses can override this property to return a more precisely typed tuple.

deepcopy(memodict={})[source]

DEPRECATED: use copy.deepcopy() instead

Will be removed in stable version.

get_bounds() tuple[int, int][source]
has_subexpr() bool[source]

Does it contain nested Expressions (anything other than a _NumVarImpl or a constant)? Is of importance when deciding whether certain transformations are needed along particular paths of the expression tree. Results are cached for future calls and reset when the expression changes (in-place argument update).

implies(other: Expression | bool | bool, simplify: bool = False) Expression[source]

Implication constraint: self -> other.

Python does not offer relevant syntax for implication, call this method instead. For double reification (<->), use equivalence self == other.

Parameters:
  • 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:
  • self -> True :: BoolVal(True)

  • self -> False :: ~self (Boolean inversion)

is_bool() bool[source]

is it a Boolean (return type) Operator? Default: yes

set_description(txt: str, override_print: bool = True, full_print: bool = False) None[source]
update_args(args: Iterable[Any], has_subexpr: bool | None = None) None[source]

Allows in-place update of the expression’s arguments. Resets all cached computations which depend on the expression tree.

  • args (Iterable[Any]): new arguments

  • has_subexpr (Optional[bool]): provide this if you know the answer already, to avoid computing it

value() int | None[source]
class cpmpy.expressions.core.Operator(name: str, arg_list: Sequence[Expression | int | integer | bool | Sequence[Expression | int | integer | bool] | ndarray])[source]

All kinds of mathematical and logical operators on expressions

Convention for 2-ary operators: if one of the two is a constant, it is stored first (as expr[0]), this eases weighted sum detection

allowed: Final[dict[str, tuple[int, bool]]] = {'-': (1, False), '->': (2, True), 'and': (0, True), 'not': (1, True), 'or': (0, True), 'sub': (2, False), 'sum': (0, False), 'wsum': (2, False)}
property args: tuple[Any, ...]

READ-ONLY access to the expression’s arguments. Use update_args() to update the arguments.

Subclasses can override this property to return a more precisely typed tuple.

deepcopy(memodict={})

DEPRECATED: use copy.deepcopy() instead

Will be removed in stable version.

get_bounds() tuple[int, int][source]

Returns an estimate of lower and upper bound of the expression. These bounds are safe: all possible values for the expression agree with the bounds. These bounds are not tight: it may be possible that the bound itself is not a possible value for the expression.

has_subexpr() bool

Does it contain nested Expressions (anything other than a _NumVarImpl or a constant)? Is of importance when deciding whether certain transformations are needed along particular paths of the expression tree. Results are cached for future calls and reset when the expression changes (in-place argument update).

implies(other: Expression | bool | bool, simplify: bool = False) Expression

Implication constraint: self -> other.

Python does not offer relevant syntax for implication, call this method instead. For double reification (<->), use equivalence self == other.

Parameters:
  • 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:
  • self -> True :: BoolVal(True)

  • self -> False :: ~self (Boolean inversion)

is_bool() bool[source]

is it a Boolean (return type) Operator?

printmap: Final[dict[str, str]] = {'sub': '-', 'sum': '+'}
set_description(txt: str, override_print: bool = True, full_print: bool = False) None
update_args(args: Iterable[Any], has_subexpr: bool | None = None) None

Allows in-place update of the expression’s arguments. Resets all cached computations which depend on the expression tree.

  • args (Iterable[Any]): new arguments

  • has_subexpr (Optional[bool]): provide this if you know the answer already, to avoid computing it

value() int | None[source]

Returns the value of the expression (or None if not everything has a value).