Mixed-Integer Programming

The following sections describe how solve mixed-integer programming
(MIP) problems with OR-Tools.

MIP solvers

OR-Tools provides an interface to several MIP solvers.
By default, it uses
Coin-or branch and cut (CBC), an open-source
solver. If you build OR-Tools from source, you can install and use the following
third-party MIP solvers in place of CBC:

Note: To use a third-party solver other than CBC, you must install it
and then build OR-Tools from source.

Using a MIP solver with the OR-Tools linear solver wrapper

To use a MIP solver, you first declare it with the OR-Tools
linear solver wrapper —
a wrapper for several linear and mixed-integer optimization libraries. The following sections
show how to use a MIP solver.

Using a MIP solver

The following shows how to use the MIP solver CBC. Using other MIP solvers is similar.
First, you must include or import the linear solver header(s).

C++

const MPSolver::ResultStatus result_status = solver.Solve();
// Check that the problem has an optimal solution.
if (result_status != MPSolver::OPTIMAL) {
LOG(FATAL) << "The problem does not have an optimal solution!";
}

Java

final MPSolver.ResultStatus resultStatus = solver.solve();
// Check that the problem has an optimal solution.
if (resultStatus != MPSolver.ResultStatus.OPTIMAL) {
System.err.println("The problem does not have an optimal solution!");
return;
}
// Verify that the solution satisfies all constraints (when using solvers
// others than GLOP_LINEAR_PROGRAMMING, this is highly recommended!).
if (!solver.verifySolution(/*tolerance=*/1e-7, /*log_errors=*/true)) {
System.err.println("The solution returned by the solver violated the"
+ " problem constraints by at least 1e-7");
return;
}

C#

Solver.ResultStatus resultStatus = solver.Solve();
// Check that the problem has an optimal solution.
if (resultStatus != Solver.ResultStatus.OPTIMAL)
{
Console.WriteLine("The problem does not have an optimal solution!");
return;
}

MIP Example

The following is a simple example of a mixed-integer programming problem:

Maximize x + 10y
subject to the following constraints:

x + 7
y

≤

17.5

x

≤

3.5

x

≥

0

y

≥

0

x,
y integers

Since the contraints are linear, this is just a linear optimization problem in which the
solutions are required to be integers. The graph below shows the integer points in the feasible
region for the problem.

Notice that this problem looks quite similar to the linear optimization problem described in
Linear Optimization with Glop, except for the integer
conditions. Fortunately, you can easily modify the
linear optimization Python program,
described in that section, to solve this problem. All you have to change (besides the
constraints and objective function) is the solver and the types of variables defined.

The following sections present the main elements of a Python program that solves the integer
problem.

C++

const MPSolver::ResultStatus result_status = solver.Solve();
// Check that the problem has an optimal solution.
if (result_status != MPSolver::OPTIMAL) {
LOG(FATAL) << "The problem does not have an optimal solution!";
}

Java

final MPSolver.ResultStatus resultStatus = solver.solve();
// Check that the problem has an optimal solution.
if (resultStatus != MPSolver.ResultStatus.OPTIMAL) {
System.err.println("The problem does not have an optimal solution!");
return;
}
// Verify that the solution satisfies all constraints (when using solvers
// others than GLOP_LINEAR_PROGRAMMING, this is highly recommended!).
if (!solver.verifySolution(/*tolerance=*/1e-7, /*log_errors=*/true)) {
System.err.println("The solution returned by the solver violated the"
+ " problem constraints by at least 1e-7");
return;
}

C#

Solver.ResultStatus resultStatus = solver.Solve();
// Check that the problem has an optimal solution.
if (resultStatus != Solver.ResultStatus.OPTIMAL)
{
Console.WriteLine("The problem does not have an optimal solution!");
return;
}

Let's compare the solution to the integer optimization problem, shown above, with the
solution to the corresponding linear optimization problem, in which integer constraints are
removed. You might guess that the solution to the integer problem would be the integer point
in the feasible region closest to the linear solution — namely, the point
x = 0,
y = 2. But as you will see next, this is not the case.

You can easily modify the program in the preceding section to solve the linear problem by
making the following changes:

The solution to the linear problem occurs at the point x
= 0, y = 2.5, where the objective function equals
25. Here's a graph showing the solutions to both the linear and integer problems.

Notice that the integer solution is not close to the linear solution, compared with most other
integer points in the feasible region. In general, the solutions to a linear optimization
problem and the corresponding integer optimization problems can be far apart. Because of this,
the two types of problems require different methods for their solution.