Reification: OnlyEnforceIf

Use the OnlyEnforceIf method to conditionally activate constraints via a Boolean indicator, build full reifications by combining two half-reifications, and encode logical implications between constraints.

Step 1 of 157%

Tutorial

Half-Reification with OnlyEnforceIf

Many CP solvers (e.g., OR-Tools CP-SAT) let us conditionally activate a constraint via a Boolean indicator. Given a constraint CC and a Boolean variable bb, attaching \texttt{OnlyEnforceIf(b)} to CC imposes the rule

b=1    C holds.b = 1 \implies C \text{ holds.}

When b=0b = 0, the constraint CC is deactivated — it places no restriction on the model. This is called half-reification: the implication runs in one direction only. Even if CC happens to be satisfied, bb is not forced to 11.

For example, with x{0,1,2,3}x \in \{0,1,2,3\} and Boolean bb, the statement

model.Add(x >= 2).OnlyEnforceIf(b)\texttt{model.Add(x >= 2).OnlyEnforceIf(b)}

admits the following assignments:

  • (x,b)=(3,1)(x,b) = (3,1): feasible — b=1b=1 forces x2x \geq 2, and x=3x=3 satisfies it.
  • (x,b)=(0,0)(x,b) = (0,0): feasible — b=0b=0 deactivates the constraint.
  • (x,b)=(3,0)(x,b) = (3,0): feasible — b=0b=0 deactivates the constraint, even though x2x \geq 2 happens to hold.
  • (x,b)=(0,1)(x,b) = (0,1): infeasible — b=1b=1 requires x2x \geq 2, but x=0x=0 violates it.

Notice that nothing in the model forces bb to be 11 when x2x \geq 2. Half-reification is strictly one-way.

navigate · Enter open · Esc close · ⌘K/Ctrl K toggle