Classic OR Examples

The examples on this page are considered classic OR examples. Historically, we have published the code for these examples on the Examples page in our documentation.

For this AI modeling project, we have translated these models back into problem descriptions. Note that the model generated by the LLM might not reflect Gurobi’s best practices. For the best practices we recommend looking at the model code in the Examples page.

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.

The reference Python code showing Gurobi best practices can be found in our documentation here.

Facility
A company currently ships its product from 5 plants to 4 warehouses.
It is considering closing one or more plants to reduce cost.

Objective: Which plant(s) should the company close, in order to minimize transportation and fixed costs?

Constraints:
- The demand for the warehouses 1-4 is 15, 18, 14, 20 products respectively (per thousand)
- The capacity for the plants 1-5 is 20, 22, 17, 19, 18 products respectively (per thousand)
- The fixed costs for the plants 1-5 is 12000, 15000, 17000, 13000, 16000 respectively

Data:
Transportation Costs (per 1000 products) can be found in facility.csv with the following columns: Warehouse,Plant 1,Plant 2,Plant 3,Plant 4,Plant 5

Diet

Builds and solves the classic diet problem.

The reference Python code showing Gurobi best practices can be found in our documentation here.

I want to optimize my diet.

Objective: I want to minimize the cost of food while upholding my dietary needs

Constraints:
- I want to eat between 1800 and 2200 calories per day
- At least 91 gram of protein
- At most 65 gram of fat
- At most 1779 mg of sodium
- Portions are non-divisible

Data:
The food I want to choose between can be found in diet.csv with the following columns: food, price, calories, protein, fat, sodium

Sudoku

A simple sudoku solving problem.

The reference Python code showing Gurobi best practices can be found in our documentation here.

Sudoku
Solve the following sudoku, the periods indicate empty cells:

.284763..
...839.2.
7..512.8.
..179..4.
3........
..9...1..
.5..8....
..692...5
..2645..8

Workforce

Assigning workers optimally to fit a work schedule.

The reference Python code showing Gurobi best practices can be found in our documentation here.

We have also described this use-case in our OptiMods toolbox, which is a convenient data-driven API for running particular problem types like this one! See the Workforce Scheduling or OptiMods to learn more.

Workforce
Assign workers to shifts while minimizing costs.

Shift requirements:
Mon1: 3
Tue2: 2
Wed3: 4
Thu4: 2
Fri5: 5
Sat6: 4
Sun7: 3
Mon8: 2
Tue9: 2
Wed10: 3
Thu11: 4
Fri12: 4
Sat13: 7
Sun14: 5

Workers and their pay:
Amy: 10
Bob: 12
Cathy: 10
Dan: 8
Ed: 8
Fred: 9
Gu: 11

Availability:
Amy: Tue2,Wed3,Fri5,Sun7,Tue9,Wed10,Thu11,Fri12,Sat13,Sun14
Bob: Mon1,Tue2,Fri5,Sat6,Mon8,Thu11,Sat13
Cathy: Wed3,Thu4,Fri5,Sun7,Mon8,Tue9,Wed10,Thu11,Fri12,Sat13,Sun14
Dan: Tue2,Wed3,Fri5,Sat6,Mon8,Tue9,Wed10,Thu11,Fri12,Sat13,Sun14
Ed: Mon1,Tue2,Wed3,Thu4,Fri5,Sun7,Mon8,Tue9,Thu11,Sat13,Sun14
Fred: Mon1,Tue2,Wed3,Sat6,Mon8,Tue9,Fri12,Sat13,Sun14
Gu: Mon1,Tue2,Wed3,Fri5,Sat6,Sun7,Mon8,Tue9,Wed10,Thu11,Fri12,Sat13,Sun14

Portfolio Optimization

This example solves a financial portfolio optimization model, using historical return data.

The reference Python code showing Gurobi best practices can be found in our documentation here.

Portfolio Optimization
I have data on some stocks that I enclosed in a csv.

The columns are the stocks and rows are their value at different time points.

Step 1: I want to calculate the minimum risk portfolio
Step 2: Solve for efficient frontier by varying the expected return.
Step 3: Create a plot with the following
  - Volatility versus expected return for individual stocks (in blue dots) and include an annotation with the stock name
  - Volatility versus expected return for minimum risk portfolio (in green dots)
  - Efficient frontier showing the volatility (green line)

Make sure you set: `model.setParam("OutputFlag", 0)`, to suppress Gurobi output.