# Build Your Optimization Skills with Python

Jupyter Notebook Modeling Examples

### Introduction to Gurobi Jupyter Notebook Modeling Examples

Gurobi Jupyter Notebook Modeling Examples are mathematical optimization models coded using the Gurobi Python API and implemented with Jupyter Notebooks.

**These Jupyter Notebook Modeling Examples:**

- Teach you how to build mathematical optimization models of real-world business, engineering, or scientific problem using Python.
- Illustrate the broad applicability of mathematical optimization across various industries.
- Explain the important features of the Gurobi Python API modeling objects such as adding decision variables, constraints, and an objective function, and building linear expressions.

The Gurobi Jupyter Notebook Modeling Examples are based on real-world use cases and problems discussed in the fifth edition of Model Building in Mathematical Programming, by H.Paul Williams – an excellent educational resource for those interested in learning how to model. Our selection contains examples from a wide variety of industries including manufacturing, financial services, energy and utilities, telecommunications, and oil & gas as well as examples that have relevance across various industries such as marketing campaign optimization and supply network design.

No matter if you are a beginner, intermediate or an advanced user, these Gurobi Jupyter Notebook Modeling Examples will help you improve your optimization modeling skills using Python.

**Access the Examples Via Google Colab or GitHub**

You can access each Jupyter Notebook Modeling example in Google Colab via the links below, except where noted. Google Colab is a free, online Jupyter Notebook environment that allows you to write and execute Python code through your browser.

You can also download the whole collection of examples on GitHub here.

If you are looking for examples for our C, C++, C#, Java, Visual Basic and Python, MATLAB® and R interfaces, the Gurobi distribution includes an extensive set of examples that illustrate commonly used features of the Gurobi libraries. 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, visit our Functional Code Examples page here.

### Explore Our List of Examples to Find the Level That Is Right For You

### Tutorial Example

### Introductory

This modeling tutorial is at the introductory level, where we assume that you know Python and that you have a background in a discipline that uses quantitative methods.

Example | Description |
---|---|

Intro to Mathematical Optimization Modeling | Learn the key components in the formulation of mixed-integer programming (MIP) problems. You will learn how to use the Gurobi Optimizer to compute an optimal solution of the MIP model. |

### Modeling Examples

*Problems from the fifth edition of Model Building in Mathematical Programming, by H.Paul Williams.

### Beginner

These modeling examples are at the beginner level, where we assume you know Python and have some knowledge about building mathematical optimization models.

Example | Description | Industry |
---|---|---|

3D Tic-Tac-Toe* | Arrange X’s and O’s on a three-dimensional Tic-Tac-Toe board to minimize the number of completed lines or diagonals. | General |

Cell Tower Coverage | A simple covering problem that builds a network of cell towers to provide signal coverage to the largest number of people possible. | Telecommunications |

Curve Fitting* | A problem of fitting a linear function to a set of observations is formulated as a linear programming problem. | General |

Diet Problem | Using the standard diet problem to demonstrate the ticdat** package. | General |

Facility Location | A facility location problem that involves building warehouses to supply a certain number of supermarkets. | Logistics |

Marketing Campaign Optimization | A marketing campaign optimization problem common in the banking and financial services industry. | Financial Services |

Market Sharing* | Allocate retailers to two divisions of a company in order to optimize the trade-offs of several market sharing goals. | Logistics |

Offshore Wind Farming | Minimize the cost of laying underwater cables that collect electricity produced by an offshore wind farm. | Energy and Utilities |

Supply Network Design* | Determine how to satisfy customer demand, while minimizing shipping costs. | Logistics |

COVID-19 Facility Capacity Optimization* | This COVID-19 Healthcare Facility Capacity Optimization problem shows you how to determine the optimal location and capacity of temporary healthcare facilities. | Healthcare |

### Intermediate

These modeling examples are at the intermediate level, where we assume that you have some knowledge about building mathematical optimization models. In addition, you should know Python and be familiar with the Gurobi Python API.

Example | Description | Industry |
---|---|---|

Agricultural Pricing* | Determine the prices and demand for the dairy products of a country in order to maximize total revenue derived from the sales of those products | Food and Agriculture |

Best Feature Selection for Forecasting | A linear regression problem that minimizes the residual sum of squares subject to the constraint that the number of non-zero feature weights should be less than or equal to a given upper limit. | Research, Analytics, and Optimization |

Customer Assignment | Address the optimal placement of facilities (from a set of candidate locations) in order to minimize the distance between a company’s facilities and its customers. | Logistics |

Electrical Power Generation* | An electrical power generation problem (also known as a unit commitment problem) by selecting an optimal set of power stations to turn on in order to satisfy anticipated power demand over a 24-hour time horizon. | Energy and Utilities |

Factory Planning* | Solve a production planning problem and create an optimal production plan to maximize profit. | Manufacturing |

Food Manufacturing* | Solve a blending optimization problem with multiple raw materials combined in a way that meets the stated constraints for the lowest cost. | Manufacturing |

Logical Design* | A problem of constructing a circuit using the minimum number of NOR gates that will perform the logical function specified by a truth table. | Manufacturing |

Mining* | Solve a multi-period production planning problem to optimize mine production across a number of mines over a five-year period. | Metals and Mining |

Opencast Mining* | Identify which excavation locations to choose in order to maximize the gross margins of extracting ore. | Metals and Mining |

Refinery* | A production planning problem, where decisions must be made regarding which products to produce, and which resources to use to produce those products. | Oil and Gas |

Technician Routing and Scheduling Problem | Learn how to formulate and solve a multi-depot vehicle routing problem with time windows. | Transportation |

Vehicle Rental Optimization* | In this Car Rental problem you want to determine how many cars to own, where should they be located, and where to expand repair capacity to maximize profits. | Services |

Economic Planning* | This is an example of an economic planning problem that a country may face. The goal is to determine different possible growth patterns for the economy. This problem is formulated as a linear programming problem using the Gurobi Python API and solved with the Gurobi Optimizer. | Government |

Efficiency Analysis* | The Efficiency Analysis example is a linear programming problem solved using the Gurobi Python API. | All |

### Advanced

These modeling examples are at the advanced level, where we assume that you know Python and the Gurobi Python API and that you have advanced knowledge of building mathematical optimization models. Typically, the objective function and/or constraints of these examples are complex or require advanced features of the Gurobi Python API.

Example | Description | Industry |
---|---|---|

Decentralization Planning* | For a given a set of departments of a company, and potential cities where these departments can be located, determine the “best” location of each department in order to maximize gross margins. | Logistics |

Farm Planning* | A production planning problem, where decisions must be made regarding which products to produce, and which resources to use to produce those products. | Food and Agriculture |

Manpower Planning* | Solve staffing planning problems, where choices must be made regarding recruitment, training, redundancy and scheduling of staff. | Manufacturing |

Standard Pooling | Solve a pooling problem, which is common in various industries including petrochemicals, wastewater treatment, mining, food and liquor processing, pharmaceuticals, heat exchanger networks, and supply chain operations. | Oil and Gas |

Traveling Salesman | The goal of the Traveling Salesman Problem (TSP) is to find the shortest possible route that visits each city once and returns to the original city. | Research, Analytics and Optimization |

Workforce Scheduling | A workforce scheduling optimization problem that deals with the arrangement of work schedules and the assignment of personnel shifts in order to cover the demand for resources that vary over time. | Professional Services |

Protein Folding* | This model is an example of a Protein Folding problem formulated as a binary optimization problem using the Gurobi Python API and solved with the Gurobi Optimizer. | Healthcare |

Constraint Optimization* | In this example, we consider a constraint of an integer programming model where all the decision variables in the constraint are binary, the goal is to find another constraint that is logically equivalent to the original constraint, but that has the smallest possible absolute value of the right-hand side. | General |

Protein Comparison* | This is an example of a Protein Comparison problem formulated as a quadratic assignment problem using the Gurobi Python API and solved with the Gurobi Optimizer. | Healthcare |

Lost Luggage Distribution* | This is an example of a vehicle routing problem formulated as a binary optimization problem using the Gurobi Python API. | Transportation |

Milk Collection* | This is an example of a capacitated vehicle routing problem and it is formulated as a binary optimization problem using the Gurobi Python API and solved with the Gurobi Optimizer | Transportation |

Yield Management* | This is an example of a Yield Management problem formulated as a three-period stochastic programming problem using the Gurobi Python API. | Airlines |

Portfolio Selection Optimization | In this example, we want to find the fraction of the portfolio to invest among a set of stocks that balances risk and return. | Financial Services |

#### Supplementary Python Packages:

**ticdat is a Python open-source package that can simplify the coding work required of MIP developers to bridge the gap between proof-of-concept programming and safe, well-organized, production-grade code.

The Diet Problem ticdat example/template introduces the package and covers:

- distributing work in a versioned package
- automating testing processes with a unit test based testing suite
- and protecting mathematical engines against real world data integrity problems

## Related Resources

### Functional Code Examples

### What’s New in the Gurobi Optimizer 9.1

### Gurobi Optimizer

### Commercial License

**New Users:** Gurobi allows you to try a free, full-featured, commercial evaluation license for 30 days. During that time, you’ll also get:

- Free benchmarking services
- Free model tuning services
- Access to Gurobi’s world-class technical support
- Two free hours of one-on-one consulting services

**Note to Existing Customers Affected by COVID-19: **Please use this form to request a temporary license, if you are experiencing difficulties accessing the Gurobi Optimizer.

**Note to Academic Users:** Academic users at recognized degree-granting institutions can get a free academic license. You can learn about our academic program here. Can’t view the form? Please click here to open it in a new window.