Expression transformations (cpmpy.transformations)

Transformations are used by solvers to convert (high-level) CPMpy expressions into the low-level constraints they support.

Typical users never need to use these functions directly.

CPMpy’s transformations selectively rewrite only those constraint expressions that a solver does not support. While solvers can use any transformation they need, lower-level solvers largely reuse those of higher-level ones, creating a waterfall pattern:

Waterfall from model to solvers

The rest of this documentation is for solver developers and other advanced users. Make sure you read Adding a new solver first.

A transformation can not modify expressions in-place but in that case should create and return new expression objects (copy-on-write). In this way, the expressions prior to the transformation remain intact, and could be used for other purposes too.

List of submodules

Input and output to transformations are always CPMpy expressions, so transformations can be chained and called multiple times, as needed. While there is no fixed ordering, the following ordering corresponds to the waterfall:

get_variables

Getting and printing variables in the model or expression.

normalize

Normalize a toplevel list, or simplify Boolean expressions.

safening

Transforms partial functions into total functions.

decompose_global

Decompose global constraints not supported by the solver.

negation

Transformations dealing with negations (used by other transformations).

flatten_model

Flattening a model (or individual constraints) into 'flat normal form'.

reification

Transformations that rewrite reified constraints as needed.

comparison

Transforms non-equality comparisons into equality comparisons as needed.

linearize

Transforms flat constraints into linear constraints.

to_cnf

Transform constraints to Conjunctive Normal Form (i.e. an and of or`s of literals, i.e. Boolean variables or their negation, e.g. from `x xor y to (x or ~y) and (~x or y)) using a back-end encoding library and its transformation pipeline.