Integer Variables and Linear Constraints in CP-SAT

Learn how to declare bounded integer variables and add linear (in)equality constraints to a CP-SAT model in Google OR-Tools, and how to check whether a candidate assignment is feasible.

Step 1 of 157%

Tutorial

Integer Variables in CP-SAT

In CP-SAT, an integer variable is a decision variable whose value the solver must choose from a finite range of integers. We declare one with the syntax

x = model.NewIntVar(lb, ub, name)\texttt{x = model.NewIntVar(lb, ub, name)}

This creates a variable named name\texttt{name} whose domain is the set of integers from lb\texttt{lb} to ub\texttt{ub}, inclusive on both ends. Both bounds must be integers, and name\texttt{name} is a string used only for display.

For example, given a model object created with model = cp_model.CpModel()\texttt{model = cp\_model.CpModel()}, the lines

x = model.NewIntVar(0, 10, "x")y = model.NewIntVar(-5, 5, "y")\begin{array}{l}\texttt{x = model.NewIntVar(0, 10, "x")} \\ \texttt{y = model.NewIntVar(-5, 5, "y")}\end{array}

declare x{0,1,2,,10}x \in \{0, 1, 2, \ldots, 10\} and y{5,4,,5}.y \in \{-5, -4, \ldots, 5\}. In general, the domain of NewIntVar(lb, ub, name)\texttt{NewIntVar(lb, ub, name)} contains exactly

ublb+1\texttt{ub} - \texttt{lb} + 1

integer values.

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