Negation (cpmpy.transformations.negation)
Transformations dealing with negations (used by other transformations).
After calling push_down_negation(), only two ‘negative’ expressions remain: - NegBoolView(var), which represents ~var - Operator(“not”, [GlobalConstraint]) in case GlobalConstraint did not overwrite the .negate() method.
The latter can be further handled by decompose_in_tree(), e.g. it will negate the decomposition.
- cpmpy.transformations.negation.negated_normal(expr)[source]
Deprecated since version 0.9.16: Please use
recurse_negation()instead.
- cpmpy.transformations.negation.push_down_negation(lst_of_expr: list[Expression], toplevel=True) list[Expression][source]
Recursively simplifies expressions by pushing down negation into the arguments. E.g., not(x >= 3 | y == 2) is simplified to (x < 3) & (y != 2).
Input is expected to be a flat list of Expressions. ‘Toplevel’ means ‘merge_and’: if a toplevel ‘and’ is created and the flag is True,
then the ‘and’ will not be added to the toplevel list, but all its arguments will be merged in. (actually only Expressions, and if a constant ‘False’ is found it adds BoolVal(False) and stops merging)
- Returns:
list of Expressions
- cpmpy.transformations.negation.push_down_negation_objective(expr: Expression) Expression[source]
Push down negation into the objective expression.
- cpmpy.transformations.negation.recurse_negation(expr: Expression | bool | bool) Expression[source]
Negate expr by pushing the negation down into it and its arguments.
The following cases are handled: - Boolean variables and constants: negate the variable or constant - Comparisons: swap comparison sign - Boolean operators (and/or/implies): apply DeMorgan - Global constraints: calls
negate()to negate the global constraint.Depending on the implementation, this may leave a “NOT” operator before the global constraint. Use
decompose_in_tree()to decompose the negated global constraint into simpler constraints if needed.Ensures no “NOT” operator is left in the expression tree of expr, apart from negated global constraints.
Returns the negated expression.