Core expressions (cpmpy.expressions.core)

The Expression superclass and common subclasses Expression 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:

  • 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)

Mathematical operators:

  • -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])

  • x % y Operator(“mod”, [x,y])

  • x ** y Operator(“pow”, [x,y])

Logical operators:

  • x & y Operator(“and”, [x,y])

  • x | y Operator(“or”, [x,y])

  • ~x Operator(“not”, [x])

    or NegBoolView(x) in case x is a Boolean variable

  • x ^ y Xor([x,y]) # a global constraint

Python has no built-in operator for __implication__ that can be overloaded. CPMpy hence has a function ‘implies()’ that can be called:

  • 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

Expression

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

Comparison

Represents a comparison between two sub-expressions

Operator

All kinds of mathematical and logical operators on expressions

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

Wrapper for python or numpy BoolVals

deepcopy(memodict={})
get_bounds()
implies(other)
is_bool()

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

set_description(txt, override_print=True, full_print=False)
value()[source]
class cpmpy.expressions.core.Comparison(name, left, right)[source]

Represents a comparison between two sub-expressions

allowed = {'!=', '<', '<=', '==', '>', '>='}
deepcopy(memodict={})
get_bounds()
implies(other)
is_bool()

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

set_description(txt, override_print=True, full_print=False)
value()[source]
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 Boolean - value(): the value of the expression, default None - implies(x): logical implication of this expression towards x - __repr__(): for pretty printing the expression - any __op__ python operator overloading

deepcopy(memodict={})[source]
get_bounds()[source]
implies(other)[source]
is_bool()[source]

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

set_description(txt, override_print=True, full_print=False)[source]
value()[source]
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)}
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.

implies(other)
is_bool()[source]

is it a Boolean (return type) Operator?

printmap = {'div': '//', 'mul': '*', 'sub': '-', 'sum': '+'}
set_description(txt, override_print=True, full_print=False)
value()[source]