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.
- 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:
- 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
- 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_NumVarImplor 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:
- 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
- class cpmpy.expressions.core.Description(text: str, override_print: bool = True, full_print: bool = False)[source]
Human-readable print metadata for an
Expression; set viaset_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 argumentsis_bool(): whether its return type is Booleanvalue(): the value of the expression, default Noneimplies(x): logical implication of this expression towards x__repr__(): for pretty printing the expressionset_description(): optional custom__str__()text (class default_descriptionisNone; 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.
- has_subexpr() bool[source]
Does it contain nested
Expressions(anything other than a_NumVarImplor 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:
- Simplification rules:
self -> True :: BoolVal(True)
self -> False :: ~self (Boolean inversion)
- 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
- 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_NumVarImplor 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:
- Simplification rules:
self -> True :: BoolVal(True)
self -> False :: ~self (Boolean inversion)
- 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