The Gurobi™ distribution includes an extensive set of examples that illustrate commonly used features of the Gurobi libraries. Most examples have versions for C, C++, C#, Java, Visual Basic and Python. A few, however, illustrate features that are specific to the Python interface. A brief note for users of the Gurobi MATLAB® and R interfaces: our interfaces to these languages are built around the assumption that you will use the rich matrix-oriented capabilities of the underlying languages to build your optimization models. Thus, our examples for these languages don’t attempt to show you how to build models.

We have instead chosen to provide a few simple examples that demonstrate how to pass matrices into our interface. We recommend that you begin by reading the overview of the examples (available here). However, if you’d like to dive directly into specific examples, the following is a list of all of the examples included in the Gurobi distribution, organized by basic function. The source for the examples can be found by following the provided links, or in the examples directory of the Gurobi distribution. If you would like further details on any of the Gurobi routines used in these examples, please consult the Gurobi Reference Manual.

In addition to these Functional Code Examples, we also offer a set of examples that capture interesting models from a variety of application domains, implemented as Jupyter Notebooks using the Gurobi Python API.

## Read a model from a file

Example Description Available Languages
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. CC++C#JavaPythonVB
mip2 Reads a MIP model from a file, optimizes it, and then solves the fixed version of the MIP model. CC++C#JavaPythonVB

## Build a simple model

Example Description Available Languages
mip1 Builds a trivial MIP model, solves it, and prints the solution. CC++C#JavaMATLABPythonRVB
qp Builds a trivial QP model, solves it, converts it to an MIQP model, and solves it again. CC++C#JavaMATLABPythonRVB
qcp Builds and solves a trivial QCP model. CC++C#JavaMATLABPythonRVB
sos Builds and solves a trivial SOS model. CC++C#JavaMATLABPythonRVB
dense Solves a model stored using dense matrices. We don’t recommend using dense matrices, but this example may be helpful if your data is already in this format. CC++C#JavaPythonVB
genconstr Demonstrates the use of general constraints. CC++C#JavaPythonVB
multiobj Demonstrates the use of multi-objective optimization. CC++C#JavaPythonVB
piecewise Demonstrates the use of piecewise-linear objective functions. CC++C#JavaMATLABPythonRVB
poolsearch Demonstrates the use of solution pools. CC++C#JavaPythonVB

## A few simple applications

Example Description Available Languages
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. CC++C#JavaMATLABPythonVB
diet2, diet3, diet4, dietmodel Python-only variants of the diet example that illustrate model-data separation. diet2.pydiet3.pydiet4.pydietmodel.py
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. CC++C#JavaPythonVB
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. Python
portfolio A Python-only example that solves a financial portfolio optimization model, where the historical return data is stored using the pandas package and the result is plotted using the matplotlib package. It demonstrates the use of pandas, NumPy, and Matplotlib in conjunction with Gurobi. Python
sudoku Reads a Sudoku puzzle dataset from a file, builds a MIP model to solve that model, solves it, and prints the solution. CC++C#JavaPythonVB
workforce1 Formulates and solves a workforce scheduling model. If the model is infeasible, the example computes and prints an Irreducible Inconsistent Subsystem (IIS). CC++C#JavaPythonVB
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. CC++C#JavaPythonVB
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. CC++C#JavaPythonVB
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. CC++C#JavaPythonVB
workforce5 An alternative enhancement of workforce3. This example solves the same workforce scheduling model, but it starts with artificial variables in each constraint. It formulates a multi-objective model where the primary objective is to minimize the sum of the artificial variables (uncovered shifts), and the secondary objective is to minimize the maximum difference in the number of shifts worked between any pair of workers. Demonstrates multi-objective optimization. CC++C#JavaPythonVB

## Illustrating specific features

Example Description Available Languages
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. CC++C#JavaPythonVB
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. CC++C#JavaPythonVB
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. CC++C#JavaPythonVB
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. CC++C#JavaPythonVB
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. CC++C#JavaPythonVB
tune Uses the parameter tuning tool to search for improved parameter settings for a model. CC++C#JavaPythonVB
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.). CC++C#JavaPythonVB
matrix1.py A Python-only example that formulates and solves a simple MIP model using the matrix API. Python
matrix2.py A Python-only example that uses the Python matrix API to formulate the n-queens problem; it maximizes the number queens placed on an n x n chessboard without threatening each other. Python

Example Description Available Languages
tsp Solves a traveling salesman problem using lazy constraints. CC++C#JavaPythonVB
callback Demonstrates the use of Gurobi callbacks. CC++C#JavaPythonVB

### Gurobi: Always Free for Academics

We make it easy for students, faculty, and researchers to work with mathematical optimization.

When you face complex optimization challenges, you can trust our Gurobi Alliance partners for expert services.

Our global team of helpful, PhD-level experts are here to support you—with responses in hours, not days.

## What's New at Gurobi

News
##### Gurobi 10.0 Delivers Blazing-Fast Speed, Innovative Data Science Integration, and an Enterprise Development and Deployment Experience
Latest release enables data professionals to easily integrate machine learning models into optimization models to solve new types of problems.
Event
##### Webinar: What’s New in Gurobi 10.0
In this webinar, attendees will get a first look at our upcoming product release, Gurobi 10.0. We will summarize the performance improvements and highlight some of the underlying algorithmic advances, such as the network simplex algorithm, enhancements in concurrent LP, and optimization based bound tightening.
Event
new content
##### Cost Savings & Business Benefits for Gurobi Customers
2022 Total Economic Impact™ Study Reveals A 518% ROI with Gurobi