Open
Conversation
IgnaceBleukx
reviewed
Apr 1, 2026
Collaborator
IgnaceBleukx
left a comment
There was a problem hiding this comment.
Some small comments
cpmpy/transformations/linearize.py
Outdated
| Their default decomposition does not do it this way, hence we use a more linear-friendly decomposition. | ||
| This is done by :func:`decompose_linear`. | ||
|
|
||
| - **Domain encodings** e.g. of `A=cp.intvar(0,4)` would create binary variables and constraints |
Collaborator
There was a problem hiding this comment.
I found this example non-inuitive. What is A? If it's a sum it is already linear...
Exlicitely using the predicates as subexpression is more intuitive to me.
Proposal:
Flattening and linearizing the constraint `(x = 3) + (x = 5) + (x = 7) >= 1` would introduce binary variables and reified constraints `B0 -> (x = 3), B1 -> (x = 5), B2 -> (x = 7)` and `sum(B0..B2) >= 1`.
However, each Bi -> (x = v) is linearized using a Big-M constraint which is generally unwanted.
Instead we can linearize the consrtaint directly using the domain encoding of `x` with constraints:
`sum(B1..B10) = 1, sum(B1..10 * [1..10]) = x` and `sum(B3,B5,B7) >= 1`.
...
cpmpy/transformations/linearize.py
Outdated
|
|
||
| - Call :func:`decompose_linear` / :func:`decompose_linear_objective` instead of the standard 'decompose_in_tree'. | ||
| - Put constraints in flat normal form (:func:`~cpmpy.transformations.flatten_model.flatten_constraint`). | ||
| - Apply :func:`linearize_reified_variables` to replace many reified equalities of the form |
cpmpy/transformations/linearize.py
Outdated
| ``bv == (x == val)`` by a single direct encoding of ``x`` (must be done before implication-only normalisation). | ||
| - Ensure implications are of the form ``bv -> <expr>`` (:func:`~cpmpy.transformations.reification.only_implies`). | ||
| - Call :func:`linearize_constraint` to transform the inequalities, strict equalities and implications. | ||
| - Optionally run post-passes such as :func:`only_positive_bv` and :func:`only_positive_coefficients`. |
Collaborator
There was a problem hiding this comment.
Maybe add for what these functions are?
Optionally run post-passes such as :func:only_positive_bv for ILP solvers and :func:only_positive_coefficients for (Pseudo-)Boolean solvers
cpmpy/transformations/linearize.py
Outdated
| - :func:`linearize_constraint`: Transforms a list of constraints to a linear form. | ||
| - :func:`decompose_linear`: Decompose unsupported global constraints in a linear-friendly way. | ||
| - :func:`decompose_linear_objective`: Decompose objective using linear-friendly decompositions. | ||
| - :func:`linearize_reified_variables`: Replace reifieds (BV <-> (x == val)) with entire direct encoding of 'x'. |
cpmpy/transformations/linearize.py
Outdated
| - :func:`get_linear_decompositions`: Returns the custom linear decomposition map for global constraints. | ||
|
|
||
| Optional post-linearisation transformations: | ||
| - :func:`only_positive_bv`: Transforms constraints so only boolean variables appear positively |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The doc update part of #808 (slightly updated)
Re-revisiting linearize, if done, should be done after the current mypy dance