Example tour
This document provides a
quick guided tour of the Gurobi examples; we will try to highlight
some of the most important features of these examples. Full source
code is provided in this document, so you are free to explore the
examples in full detail.
Wherever possible, we try to discuss the examples in a manner that is independent of programming languages. We will refer to each example
using a brief, language independent name. You will need to map this
name to the specific source file name for your language. For example,
the facility example corresponds to six different
implementations, one in C
(facility_c.c), one in C++
(facility_c++.cpp),
one in Java
(Facility.java),
one in C#
(facility_cs.cs),
one in Visual Basic
(facility_vb.vb),
and one
in Python (facility.py). If
you would like to look at the language implementation for a particular
example, please refer to the appropriate example source file.
The Gurobi examples
The following is a list of all of the examples included in the Gurobi
distribution. The source code for these examples can be found in the
examples directory of the distribution, or in the
Source Code section of this document.
- callback - Demonstrates the use of Gurobi callbacks.
- dense - Solves a model stored using dense matrices.
We don't recommend that you use dense matrices for your models, but
this example may be helpful if your data is already in this format.
- diet - Builds and solves the classic diet problem.
Demonstrates model construction and simple model modification -
after the initial model is solved, a constraint is added to limit the
number of dairy servings.
- diet2, diet3, diet4, dietmodel - Python-only variants of the
diet example that illustrate model-data separation.
- facility - Simple facility location model: given a set
of plants and a set of warehouses, with transportation costs between
them, this example finds the least expensive set of plants to open
in order to satisfy product demand. This example demonstrates the
use of MIP starts -- the example computes an initial, heuristic
solution and passes that solution to the MIP solver.
- feasopt - Reads a MIP model from a file, adds artificial slack
variables to relax each constraint, and then minimizes the sum of
the artificial variables. It then computes the same relaxation using
the feasibility relaxation feature. The example demonstrates
simple model modification by adding slack variables. It also
demonstrates the feasibility relaxation feature.
- fixanddive - Implements a simple MIP heuristic. It
reads a MIP model from a file, relaxes the integrality conditions,
and then solves the relaxation. It then chooses a set of integer
variables that take integer or nearly integer values in the
relaxation, fixes them to the nearest integer, and solves the
relaxation again. This process is repeated until the relaxation is
either integer feasible or linearly infeasible. The example
demonstrates different types of model modification (relaxing
integrality conditions, changing variable bounds, etc.).
- lp - A very simple example that reads a continuous model
from a file, optimizes it, and writes the solution to a file. If the
model is infeasible, it writes an Irreducible Inconsistent Subsystem
(IIS) instead.
- lpmethod - Demonstrates the use of different LP algorithms.
Reads a continuous model from a file and solves it using multiple
algorithms, reporting which is the quickest for that model.
- lpmod - Demonstrates the use of advanced starts in LP.
Reads a continuous model from a file, solves it, and
then modifies one variable bound. The resulting model is then
solved in two different ways: starting from the solution of the
original model, or restarting from scratch.
- mip1 - Builds a trivial MIP model, solves it, and prints
the solution.
- mip2 - Reads a MIP model from a file, optimizes it, and
then solves the fixed version of the MIP model.
- netflow - A Python-only example that solves a multi-commodity
network flow model. It demonstrates the use of several Python modeling
constructs, including dictionaries, tuples, and tuplelist objects.
- params - Demonstrates the use of Gurobi parameters.
Reads a MIP model from a file, and then spends 5
seconds solving the model with each of four different values of the
MIPFocus parameter. It compares the optimality gaps for
the four different runs, and continues with the MIPFocus
value that produced the smallest gap.
- qp - Builds a trivial QP model, solves it, converts it to
an MIQP model, and solves it again.
- qcp - Builds and solves a trivial QCP model.
- sensitivity - MIP sensitivity analysis. Reads a MIP
model, solves it, and then computes the objective impact of fixing
each binary variable in the model to 0 or 1. Demonstrates simple
MIP model modification by changing variable bounds.
- sos - Builds and solves a trivial SOS models.
- sudoku - Reads a Sudoku puzzle dataset from a file, builds
a MIP model to solve that model, solves it, and prints the solution.
- tsp - Solves a traveling salesman problem using lazy
constraints.
- workforce1 - Formulates and solves a workforce scheduling
model. If the model is infeasible, the example computes and prints
an Irreducible Inconsistent Subsystem (IIS).
- workforce2 - An enhancement of workforce1. This
example solves the same workforce scheduling model, but if the model
is infeasible, it computes an IIS, removes one of the associated
constraints from the model, and re-solves. This process is repeated
until the model becomes feasible. Demonstrates constraint removal.
- workforce3 - A different enhancement of
workforce1. This example solves the same workforce
scheduling model, but if the model is infeasible, it adds artificial
variables to each constraint and minimizes the sum of the artificial
variables. This corresponds to finding the minimum total change in
the right-hand side vector required in order to make the model
feasible. Demonstrates variable addition.
- workforce4 - An enhancement of workforce3. This example
solves the same workforce scheduling model, but it starts with
artificial variables in each constraint. It first minimizes the sum
of the artificial variables. Then, it introduces a new quadratic
objective to balance the workload among the workers. Demonstrates
optimization with multiple objective functions.
The example tour
The easiest place to start your introduction to the Gurobi examples is
probably with the examples that load and solve a model from a file. These
demonstrate the most basic capabilities of the Gurobi libraries. They
also demonstrate the use of model attributes, which are an important
concept in the Gurobi optimizer.
Once you are comfortable with these examples, you should move on to
the examples that build a model
from scratch. These show you how to create variables and constraints,
and add them to an optimization model. They also illustrate the use
of lazy updates, which you will need to understand in order to
use the Gurobi libraries.
The next topic covered in this document is model
modification. The Gurobi distribution
includes examples that add and remove constraints, add variables, and
change variable types, bounds and objective coefficients. You modify
a model in much the same way that you build a model from scratch, but
there are some important differences involving the use of the solution
information.
Next, this document covers parameter
changes. The params example
shows you how to change parameters, and in particular how to use
different parameter settings for different models.
The infeasibility section
considers a few examples that cope with model infeasibility. Some use
an Irreducible Inconsistent Subsystem (IIS) to handle the
infeasibility, while others relax constraints.
One useful MIP feature that is worth understanding is MIP
starts. A MIP start allows you to specify a
known feasible solution to the MIP solver. The solution provides a
bound on the objective of the best possible solution, which can help
to limit the MIP search. The solution also provides a potential start
point for the local search heuristics that are utilized by the Gurobi
MIP solver.
It is possible to achieve model-data separation when using our Python
interface, as is often done in modeling languages, but you need to
make use of Python modules to do so. The model-data
separation section provides an
example of how this is done. It considers two versions of the diet
example, which obtain model data from very different places. Both use
the same file to formulate and solve the actual optimization model
on that data.
The final topic we cover in this document is Gurobi
callbacks. Callbacks allow the user to
obtain periodic progress information related to the optimization.
Subsections