Core expressions (cpmpy.expressions.core
)
The Expression
superclass and common subclasses Comparison
and Operator
.
None of these objects should be directly created, they are automatically created through operator overloading on variables and expressions.
Here is a list of standard python operators and what object (with what expr.name) it creates:
Comparisons
Python Operator |
CPMpy Object |
---|---|
x == y |
Comparison(“==”, x, y) |
x != y |
Comparison(“!=”, x, y) |
x < y |
Comparison(“<”, x, y) |
x <= y |
Comparison(“<=”, x, y) |
x > y |
Comparison(“>”, x, y) |
x >= y |
Comparison(“>=”, x, y) |
Arithmetic Operators
Python Operator |
CPMpy Object |
---|---|
-x |
Operator(“-”, [x]) |
x + y |
Operator(“sum”, [x, y]) |
sum([x,y,z]) |
Operator(“sum”, [x, y, z]) |
sum([c0*x, c1*y, c2*z]) |
Operator(“wsum”, [[c0, c1, c2], [x, y, z]]) |
x - y |
Operator(“sum”, [x, -y]) |
x * y |
Operator(“mul”, [x, y]) |
x // y |
Operator(“div”, [x, y]) (integer division) |
x % y |
Operator(“mod”, [x, y]) (modulo) |
x ** y |
Operator(“pow”, [x, y]) (power) |
Logical Operators
Python Operator |
CPMpy Object |
---|---|
x & y |
Operator(“and”, [x, y]) |
x | y |
Operator(“or”, [x, y]) |
~x |
Operator(“not”, [x]) or NegBoolView(x) if Boolean |
x ^ y |
Xor([x, y]) |
Python has no built-in operator for implication that can be overloaded.
CPMpy hence has a function implies()
that can be called:
Python Operator |
CPMpy Object |
---|---|
x.implies(y) |
Operator(“->”, [x,y]) |
Apart from operator overloading, expressions implement two important functions:
is_bool()
which returns whether the return type of the expression is Boolean. If it does, the expression can be used as top-level constraint or in logical operators.
value()
computes the value of this expression, by calling .value() on its subexpressions and doing the appropriate computation this is used to conveniently print variable values, objective values and any other expression value (e.g. during debugging).
List of classes
An Expression represents a symbolic function with a self.name and self.args (arguments) |
|
Represents a comparison between two sub-expressions |
|
All kinds of mathematical and logical operators on expressions |
- class cpmpy.expressions.core.BoolVal(arg)[source]
Wrapper for python or numpy BoolVals
- property args
- deepcopy(memodict={})
- has_subexpr() bool [source]
Does it contains 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.
- is_bool()
is it a Boolean (return type) Operator? Default: yes
- set_description(txt, override_print=True, full_print=False)
- update_args(args)
Allows in-place update of the expression’s arguments. Resets all cached computations which depend on the expression tree.
- class cpmpy.expressions.core.Comparison(name, left, right)[source]
Represents a comparison between two sub-expressions
- allowed = {'!=', '<', '<=', '==', '>', '>='}
- property args
- deepcopy(memodict={})
- get_bounds()
- has_subexpr()
Does it contains 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)
- is_bool()
is it a Boolean (return type) Operator? Default: yes
- set_description(txt, override_print=True, full_print=False)
- update_args(args)
Allows in-place update of the expression’s arguments. Resets all cached computations which depend on the expression tree.
- class cpmpy.expressions.core.Expression(name, arg_list)[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:
is_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 expressionany
__op__
python operator overloading
- property args
- has_subexpr()[source]
Does it contains 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).
- class cpmpy.expressions.core.Operator(name, arg_list)[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 = {'-': (1, False), '->': (2, True), 'and': (0, True), 'div': (2, False), 'mod': (2, False), 'mul': (2, False), 'not': (1, True), 'or': (0, True), 'pow': (2, False), 'sub': (2, False), 'sum': (0, False), 'wsum': (2, False)}
- property args
- deepcopy(memodict={})
- get_bounds()[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()
Does it contains 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)
- printmap = {'div': '//', 'mul': '*', 'sub': '-', 'sum': '+'}
- set_description(txt, override_print=True, full_print=False)
- update_args(args)
Allows in-place update of the expression’s arguments. Resets all cached computations which depend on the expression tree.