Documentation


Constraints

A constraint in Gurobi captures a restriction on the values that a set of variables may take. The simplest example is a linear constraint, which states that a linear expression on a set of variables take a value that is either less-than-or-equal, greater-than-or-equal, or equal another linear expression. Recall that Gurobi works in finite-precision arithmetic, so constraints are only satisfied to tolerances. Tolerances can be tightened to reduce such violations, but there are limits to how small the violations can be - errors are inherent in floating-point arithmetic.

The available constraint types are linear, SOS, quadratic, and general.

Linear Constraints

A linear constraint allows you to restrict the value of a linear expression. For example, you may require that any feasible solution satisfy the constraint $3 x + 4 y \leq 5z$. Note that the matrix-oriented Gurobi API's (C, MATLAB, and R) require the right-hand side of a linear constraint to be a constant, while the object-oriented APIs (C++, Java, .NET, and Python) allow arbitrary linear expressions on both sides of the comparator.

The computed solution should satisfy the stated constraint to within FeasibilityTol (although it may not in cases of numerical ill-conditioning - we'll discuss this shortly).

Gurobi supports a limited set of comparators. Specifically, you can constrain an expression to be less-than-or-equal, greater-than-or-equal, or equal another. We do not support strict less-than, strict greater-than, or not-equal comparators. While these other comparators may seem appropriate for mathematical programming, we exclude them to avoid potential confusion related to numerical tolerances. Consider a simple example of a strict inequality constraint on a pair of continuous variables: $x > y$. How large would $x-y$ need to be in order to satisfy the constraint? Rather than trying to embed a subtle and potentially confusing strategy for handling such constraints into the solver, we've chosen not to support them instead.

SOS Constraints

An Special-Ordered Set, or SOS constraint, is a highly specialized constraint that places restrictions on the values that variables in a given list can take. There are two types of SOS constraints. In an SOS constraint of type 1 (an SOS1 constraint), at most one variable in the specified list is allowed to take a non-zero value. In an SOS constraint of type 2 (an SOS2 constraint), at most two variables in the specified, ordered list are allowed to take a non-zero value, and those non-zero variables must be contiguous in the list. The variables in an SOS constraint can be continuous, integer, or binary.

Again, tolerances play an important role in SOS constraints. Specifically, variables that take values less than IntFeasTol (in absolute value) are considered to be zero for the purposes of determining whether an SOS constraint is satisfied.

An SOS constraint is described using a list of variables and a list of corresponding weights. While the weights have historically had intuitive meanings associated with them, we simply use them to order the list of variables. The weights should be unique. This is especially important for an SOS2 constraint, which relies on the notion of contiguous variables. Since the variables in the SOS are ordered by weight, contiguity becomes ambiguous when multiple variables have the same weight.

It is often more efficient to capture SOS structure using linear constraints rather than SOS constraints. The optimizer will often perform this conversion automatically. This is controlled with two parameters: PreSOS1BigM and PreSOS2BigM. The conversion is done by adding constraints of the form $x <= M b$, where $x$ is the variable that participates in the SOS constraint, $b$ is a binary variable, and $M$ is an upper bound on the value of variable $x$. Large values of $M$ can lead to numerical issues, so these parameters control the maximum value of $M$ that can be introduced by this conversion. SOS constraints that would require a larger value aren't converted.

Quadratic Constraints

A quadratic constraint allows you to restrict the value of a quadratic expression. For example, you may require that any feasible solution satisfy the constraint $3 x^2 + 4 y^2 + 5 z \leq 10$. Note that the matrix-oriented Gurobi API's (C, MATLAB, and R) require the right-hand side of a quadratic constraint to be a constant, while the object-oriented APIs (C++, Java, .NET, and Python) allow arbitrary quadratic expressions on both sides of the comparator.

The computed solution should satisfy the stated constraint to within FeasibilityTol. Quadratic constraints are often much more challenging to satisfy than linear constraints, so tightening the parameter may increase runtimes dramatically.

The algorithms that Gurobi uses to solve quadratically constrained problems can only handle certain types of quadratic constraints. Constraints of the following forms are always accepted:

  • $x^TQx + q^Tx \le b$, where $Q$ is Positive Semi-Definite (PSD)
  • $x^Tx \le y^{2}$, where $x$ is a vector of variables, and $y$ is a non-negative variable (a Second-Order Cone)
  • $x^Tx \le y z$, where $x$ is a vector of variables, and $y$ and $z$ are non-negative variables (a rotated Second-Order Cone)
If you add a constraint that isn't in one of these forms (and Gurobi presolve is unable to transform the constraint into one of these forms), you'll get an error when you try to solve the model. Constraints where the quadratic terms only involve binary variables will always be transformed into one of these forms.

General Constraints

The previously-described constraints are typically handled directly by the underlying optimization algorithms (although not always). Gurobi also includes an additional set of constraints, which we collectively refer to as general constraints. General constraints are a convenience feature, designed to allow you to capture certain relationships between variables without having to immerse yourself in the often esoteric details of how to model these relationships in terms of the more fundamental constraints of MIP. Capturing a single one of these general constraints can often require a large set of linear and SOS constraints, plus a number of auxiliary decision variables. By supporting them directly in the Gurobi API, we simplify the modeling process by performing the transformation to a corresponding MIP formulation automatically and transparently during the solution process.

Gurobi supports a number of different types of general constraints, each having its own syntax and semantics:

  • MAX constraint: The constraint $r = \max\{x_1,\ldots,x_k,c\}$ states that the resultant variable $r$ should be equal to the maximum of the operand variables $x_1,\ldots,x_k$ and the constant $c$. For example, a solution $(r=3, x_1=2, x_2=3, x_3=0)$ would be feasible for the constraint $r = \max\{x_1,x_2,x_3,1.7\}$ because $3$ is indeed the maximum of $2$, $3$, $0$, and $1.7$.
  • MIN constraint: Similar to a MAX constraint, the constraint $r = \min\{x_1,\ldots,x_k,c\}$ states that the resultant variable $r$ should be equal to the minimum of the operand variables $x_1,\ldots,x_k$ and the constant $c$.
  • ABS constraint: The constraint $r = \mbox{abs}\{x\}$ states that the resultant variable $r$ should be equal to the absolute value of the operand variable $x$. For example, a solution $(r=3, x=-3)$ would be feasible for the constraint $r = \mbox{abs}\{x\}$.
  • AND constraint: The constraint $r = \mbox{and}\{x_1,\ldots,x_k\}$ states that the binary resultant variable $r$ should be equal $1$ if and only if all of the binary operand variables $x_1,\ldots,x_k$ are equal to $1$. For example, a solution $(r=1, x_1=1, x_2=1, x_3=1)$ would be feasible for the constraint $r = \mbox{and}\{x_1,x_2,x_3\}$. Note that declaring an AND constraint implicitly declares all involved variables to be of binary type.
  • OR constraint: Similar to an AND constraint, the constraint $r = \mbox{or}\{x_1,\ldots,x_k\}$ states that the binary resultant variable $r$ should be $1$ if and only if at least one of the binary operand variables $x_1,\ldots,x_k$ is equal to $1$. Note that declaring an OR constraint implicitly declares all involved variables to be of binary type.
  • INDICATOR constraints: An indicator constraint $y = f \rightarrow a^Tx \leq b$ states that if the binary indicator variable $y$ has the value $f \in \{0,1\}$ in a given solution, then the linear constraint $a^Tx \leq b$ has to be satisfied. On the other hand, if $y \neq f$ (i.e., $y = 1-f$) then the linear constraint may be violated. Note that the sense of the linear constraint can also be $=$ or $\geq$; refer to this earlier section for a more detailed description of linear constraints. Note also that declaring an INDICATOR constraint implicitly declares the indicator variable to be of binary type.

As stated above, each general constraint has an equivalent MIP formulation that consists of linear and SOS constraints, and possibly auxiliary variables. Thus, you could always model such constraints yourself without using a Gurobi general constraint. For example, the MAX constraint $r = \max\{x_1,\ldots,x_k,c\}$ can be modeled as follows:

\begin{displaymath}
\begin{array}{rcll}
r & = & x_j + s_j & \mbox{ for all } j =...
...& \in & \{0,1\} & \mbox{ for all } j = 1,\ldots,k+1
\end{array}\end{displaymath}

The first two constraints state that $r \geq \max\{x_1,\ldots,x_k,c\}$, i.e., that the resultant variable $r$ has to be at least as large as each of the operand variables $x_j$ and the constant $c$. This can be modeled using inequalities, but we turned them into equations by introducing explicit continuous slack variables $s_j \geq 0$, which we will reuse below.

Those slack variables and the remaining constraints model $r \leq \max\{x_1,\ldots,x_k,c\}$, which is more complicated. In addition to the explicit slacks, this requires the introduction of binary auxiliary variables $z_j \in \{0,1\}$. The SOS1 constraints state that at most one of the two variables $s_j$ and $z_j$ can be non-zero, which models the implication $z_j = 1 \rightarrow s_j = 0$. Due to the third constraint, one $z_j$ will be equal to $1$ and thus at least one $s_j$ will be zero. Hence, $r = x_j$ for at least one $j$ due to the first constraint, or $r = c$ due to the second constraint.

Tolerances play a role in general constraints, although as you might expect, the exact role depends on the constraint type. Generally, violations in the resultant will be smaller than the feasibility tolerance, and integrality violations in integer resultants will also satisfy the integrality tolerance.

If a model contains general constraints, then Gurobi adds the respective MIP formulations for those constraints during the solution process. In this respect, general constraints are just a means of concisely capturing these relationships between variables while removing the burden of creating an equivalent MIP formulation. However, general constraints have another potential advantage: Gurobi might be able to simplify parts of the MIP formulation if it can prove during presolve that the simplified version suffices for the correctness of the model. For this reason, Gurobi might be able to produce a smaller or tighter representation of the general constraint than you would get from the most general formulation. For example, it might be the case that $r \leq \max\{x_1,\ldots,x_k,c\}$ is already implied by the other constraints in the model, so that a simple set of inequalities

\begin{eqnarray*}
r & \geq & x_j \;\;\mbox{ for all } j = 1,\ldots,k \\
r & \geq & c
\end{eqnarray*}


to describe $r \geq \max\{x_1,\ldots,x_k,c\}$ suffices to model the relevant part of the MAX constraint.