Comparison (cpmpy.transformations.comparison)

Transforms non-equality comparisons into equality comparisons as needed.

Let <op> be one of == or !=, <, <=, >, >=. Numeric expressions in Flat Normal Form are of the kind:

  • NumExpr <op> IV

  • BoolVar == NumExpr <op> IV

  • BoolVar -> NumExpr <op> IV

  • NumExpr <op> IV -> BoolVar

The NumExpr can be a sum, wsum or global function with a non-bool return type.

This file implements:
cpmpy.transformations.comparison.only_integer_coefficients(constraints, csemap=None)[source]

Transforms constraints with float coefficients to integer coefficients by multiplying with the smallest power of 10 that makes all coefficients integer.

For expressions containing float coefficients (especially in ‘sum’ and ‘wsum’ expressions), this transformation converts them to equivalent integer coefficients to ensure compatibility with solvers that only support integer arithmetic.

The transformation identifies the smallest power of 10 needed to convert all float coefficients to integers while preserving the mathematical relationships.

Expects the input constraints to be flat. Only apply after applying flatten_constraint()

Parameters:

constraints – list of CPMpy expressions to transform

Returns:

list of CPMpy expressions with integer coefficients

Example

Input: [2.5*x + 1.25*y <= 7.75] Output: [20*x + 10*y <= 62] # multiplied by 8 (2^3)

cpmpy.transformations.comparison.only_numexpr_equality(constraints, supported=frozenset({}), csemap=None)[source]

Transforms NumExpr <op> IV to (NumExpr == A) & (A <op> IV) if not supported. Also for the reified uses of NumExpr

Parameters:

supported – a (frozen)set of expression names that supports all comparisons in the solver