google / or-tools

Google's Operations Research tools:
https://developers.google.com/optimization/
Apache License 2.0
11.15k stars 2.12k forks source link

Add support for `OnlyEnforceIf` with `AddAllowedAssignments` #2752

Closed ahyangyi closed 2 years ago

ahyangyi commented 3 years ago

What language and solver does this apply to? All, but I am describing the issue using the Python API

Describe the problem you are trying to solve. We are using ortools to check satisfiability of certain constraints. In a section of the constraint specification, we have two modes: in mode A, the constaint is best described with AddAllowedAssignments; in mode B, the constaint is best described with AddMultiplicationEquality.

The obvious API for this case is OnlyEnforceIf. However, OnlyEnforceIf does not support the two above-mentioned types of constaints.

Describe the solution you'd like The protobuf declaration has the following comments:

  // Important: as of September 2018, only a few constraint support enforcement:
  // - bool_or, bool_and, linear: fully supported.
  // - interval: only support a single enforcement literal.
  // - other: no support (but can be added on a per-demand basis).

which leads me to believe that a direct request for supporting these combinations would be the best.

Describe alternatives you've considered I could probably try to assemble AddAllowedAssignments and AddMultiplicationEquality with extra intermediate variables. However, that would both obscure the intention of the code (which defeats the purpose of using ortools in the first place) and would probably risk a slowdown.

Additional context N/A

lperron commented 3 years ago

I note the request. Do not expect anything in the short to medium term.

lperron commented 2 years ago

For the AddMultiplicationEquality (and Mod, and Div), the best way is to duplicate the target variable, use the copy in the AddMultiplicationEquality, and add an enforced equality between the original target variable and its copy.