Skip to content

linearize: doc update#898

Open
tias wants to merge 2 commits intomasterfrom
linearize_doc
Open

linearize: doc update#898
tias wants to merge 2 commits intomasterfrom
linearize_doc

Conversation

@tias
Copy link
Copy Markdown
Collaborator

@tias tias commented Mar 31, 2026

The doc update part of #808 (slightly updated)

Re-revisiting linearize, if done, should be done after the current mypy dance

Copy link
Copy Markdown
Collaborator

@IgnaceBleukx IgnaceBleukx left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some small comments

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
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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`.
...


- 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
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"many"?

``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`.
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

- :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'.
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

typo "reifieds"

- :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
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Boolean

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants