Boolean Variables and Clauses in CP-SAT

Boolean variables in CP-SAT are integer variables restricted to the domain {0, 1}. This lesson introduces Boolean variables and their negated literals, then builds up the core CP-SAT clause types (AddBoolOr, AddBoolAnd, AddAtMostOne, AddExactlyOne, AddImplication) and shows how to combine them to model logical conditions.

Step 1 of 157%

Tutorial

Boolean Variables and Literals

In CP-SAT, a Boolean variable is an integer variable whose domain is restricted to {0,1}\{0, 1\}. We create one with the call

b=model.NewBoolVar("b").b = \texttt{model.NewBoolVar("b")}.

The value 11 represents true and the value 00 represents false.

Every Boolean variable bb has an associated negated literal, written b.Not()\texttt{b.Not()}, whose value is

b.Not()=1b.\texttt{b.Not()} = 1 - b.

So if b=1b = 1, then b.Not()=0\texttt{b.Not()} = 0; if b=0b = 0, then b.Not()=1\texttt{b.Not()} = 1.

A literal is either a Boolean variable or its negation. All of the clauses introduced in this lesson take lists of literals as arguments.

For example, if a=1a = 1 and c=0c = 0, the literals

a,c,a.Not(),c.Not()a,\qquad c, \qquad \texttt{a.Not()},\qquad \texttt{c.Not()}

have values 1, 0, 0, 11,\ 0,\ 0,\ 1 respectively.

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