Implications and Equivalences Between Booleans

Translate logical implications and biconditionals between boolean variables into CP-SAT constraints using OnlyEnforceIf.

Step 1 of 157%

Tutorial

Implications

In constraint programming, we often need to express logical relationships between boolean variables. The most basic is the implication b1b2,b_1 \to b_2, read as "if b1b_1 then b2b_2."

The truth table is

b1b2b1b2001011100111\begin{array}{|c|c|c|} \hline b_1 & b_2 & b_1 \to b_2 \\ \hline 0 & 0 & 1 \\ 0 & 1 & 1 \\ 1 & 0 & 0 \\ 1 & 1 & 1 \\ \hline \end{array}

The implication is false only when b1=1b_1 = 1 and b2=0;b_2 = 0; a false hypothesis makes the implication vacuously true.

To encode b1b2b_1 \to b_2 in OR-Tools' CP-SAT, we use OnlyEnforceIf:\texttt{OnlyEnforceIf}{:}

model.Add(b2 == 1).OnlyEnforceIf(b1)\texttt{model.Add(b2 == 1).OnlyEnforceIf(b1)}

This activates the constraint b2=1b_2 = 1 exactly when b1b_1 is true. When b1=0,b_1 = 0, the constraint is dormant, so b2b_2 may take either value -- matching the truth table.

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