This page contains links to dozens of examples across a range of APIs that you can review to help you jump-start your work with Gurobi by exploring the three tabs below:

- Functional Examples — Functional example code available across a full range of programming languages
- Modeling Examples — Built-out example models using Python and based on the examples in the H.P. Williams
*Model Building in Mathematical Programming*book. - Interactive Examples — Interactive example models illustrated using Python and covering a range of common business problems.

In addition to the examples listed here, if you are switching from another solver, you may be interested in the helpful information contained on the following pages to help make your migration even easier:

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.

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. | C, C++, C#, Java, Python, VB |

mip2 |
Reads a MIP model from a file, optimizes it, and then solves the fixed version of the MIP model. | C, C++, C#, Java, Python, VB |

Example | Description | Available Languages |
---|---|---|

mip1 |
Builds a trivial MIP model, solves it, and prints the solution. | C, C++, C#, Java, MATLAB, Python, R, VB |

qp |
Builds a trivial QP model, solves it, converts it to an MIQP model, and solves it again. | C, C++, C#, Java, MATLAB, Python, R, VB |

qcp |
Builds and solves a trivial QCP model. | C, C++, C#, Java, MATLAB, Python, R, VB |

sos |
Builds and solves a trivial SOS model. | C, C++, C#, Java, MATLAB, Python, R, VB |

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. | C, C++, C#, Java, Python, VB |

genconstr |
Demonstrates the use of general constraints. | C, C++, C#, Java, Python, VB |

multiobj |
Demonstrates the use of multi-objective optimization. | C, C++, C#, Java, Python, VB |

piecewise |
Demonstrates the use of piecewise-linear objective functions. | C, C++, C#, Java, MATLAB, Python, R, VB |

poolsearch |
Demonstrates the use of solution pools. | C, C++, C#, Java, Python, VB |

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. | C, C++, C#, Java, MATLAB, Python, VB |

diet2, diet3, diet4, dietmodel |
Python-only variants of the diet example that illustrate model-data separation. | diet2.py, diet3.py, diet4.py, dietmodel.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. | C, C++, C#, Java, Python, VB |

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. | C, C++, C#, Java, Python, VB |

workforce1 |
Formulates and solves a workforce scheduling model. If the model is infeasible, the example computes and prints an Irreducible Inconsistent Subsystem (IIS). | C, C++, C#, Java, Python, VB |

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. |
C, C++, C#, Java, Python, VB |

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. |
C, C++, C#, Java, Python, VB |

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. | C, C++, C#, Java, Python, VB |

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. | C, C++, C#, Java, Python, VB |

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. |
C, C++, C#, Java, Python, VB |

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. | C, C++, C#, Java, Python, VB |

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. | C, C++, C#, Java, Python, VB |

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. |
C, C++, C#, Java, Python, VB |

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. | C, C++, C#, Java, Python, VB |

tune |
Uses the parameter tuning tool to search for improved parameter settings for a model. | C, C++, C#, Java, Python, VB |

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.). | C, C++, C#, Java, Python, VB |

Example | Description | Available Languages |
---|---|---|

tsp |
Solves a traveling salesman problem using lazy constraints. | C, C++, C#, Java, Python, VB |

callback |
Demonstrates the use of Gurobi callbacks. | C, C++, C#, Java, Python, VB |

We know many people like to start out by looking for examples of models that, even though not exact to their situation, give them a starting point that they can work from to build their own model.

One of the best books we've come across to help people think about how to model is the fifth edition of *Model Building in Mathematical Programming*, by H.P. Williams. You can find it on Amazon.com here.

However, the book does not actually show you the code required to build the example models. As a result, we have created implementations in Python using Gurobi for many of the examples. Our goal is to have you use the H.P. Williams book to learn more about how to think about modeling, and especially models addressing your particular needs, and then use the resources on these pages to better understand how to implement those models in Python using Gurobi.

As a result, while not including the overview information in the H.P. Williams book, each of the examples we've listed below includes:

- Problem description
- Model formulation
- Python implementation
- Gurobi output and analysis

Please note: these examples should be viewed as beta at this point. Feel free to read the information and try them out. Your feedback on what could be clearer, done better, or is missing/should be added is appreciated.

Model Name | Model Description |
---|---|

Food Manufacture I | This model is an example of a blending problem. In blending optimization problems, multiple raw materials are combined in a way the meets the stated constraints for the lowest cost. |

Food Manufacture II | This model extends the Food Manufacture I example above to include new constraints that change the problem from a fairly easy to solve linear programming model to an mixed integer model that is harder to solve. |

Factory Planning I | This model is an example of a production planning problem. In product planning problems the goal is to create an optimal production plan to maximize profit. |

Factory Planning II | This model extends the Factory Planning I example above to add complexity whereby the month where each machine is down will, instead of being fixed, be determined as a part of the optimized plan. |

Farm Planning | This model is an example of a multi-period production planning problem. In this case the application is to optimize the operation of a farm over 5 years. |

Manpower Planning | This model is an example of a staffing problem. In staffing planning problems, choices must be made regarding the recruitment, training, redundancy (retention) and scheduling of staff. |

Mining | This model is an example of a production problem. In production planning problems, choices must be made regarding the what resources to use to produce what products. |

Refinery | This model is an example of a production problem. In production planning problems, choices must be made regarding the what resources to use to produce what products. |

Below is a range of interactive example models covering different types of commonly faced business problems.

While the context of your problem may not be listed below, odds are your underlying problem is listed. For example, you may not be optimizing work done in an open-pit mine. However, perhaps your problem involves the underlying problem being represented — choosing from among a range of projects.

Each example includes an overview of the problem, the resulting mathematical model, an implementation of that model in python, and a live demo of that implementation that you can interact with.

Model Name | Model Description |
---|---|

Open-Pit Mining | This example shows how Gurobi can be used to select the order of blocks removed from an open-pit mine. This example includes the more general problem of project selection that can be applied to a broad range of industries. |

Offshore Wind Farming | This example shows how Gurobi can be used to connect offshore wind turbines. This example includes the more general fixed charge network flow model that is used in a broad range of industries. |

The Traveling Salesman Problem | This example shows how Gurobi can be used to solve the famous traveling salesman: finding the shortest tour of a set of cities. The Traveling Salesman Problem has applications in other industries such as vehicle routing, circuit design and DNA sequencing. |

Cell Tower Coverage | This example shows how Gurobi can be used to determine the best place to build cell towers to provide signal to largest number of people. This example includes the more general covering problem, which is used in a broad number of models outside the telecommunication industry. |

Facility Location | This example shows how Gurobi can be used to determine where to build warehouse to supply a large number of supermarkets. Facility location is used in a broad range of industries. |

Routing Tanker Trucks | This example shows how Gurobi can be used to route tanker trucks in order to drive the fewest miles while delivering diesel fuel to customers. This example also covers the more general vehicle routing problem, which is used in many industries. |

Production Scheduling | This examples shows how Gurobi can be used to schedule the production of a pulp and paper factory with many different products. This examples also covers piecewise-linear objectives, which appear in many different optimization models. |

Kidney Exchange | This example shows how Gurobi can be used to match donors with transplant patients in a kidney exchange. |