Gurobi is one of the most powerful mathematical optimization solver. Here I introduce an planning optimization model first, and try to model it with python and gurobi python package.
Planning Model
There are 7 products want to be made by a factory, and each produces need to use a range of machines. The factory have 4 grinders, 2 vertical drills, 3 horizontal drills, 1 borer, and 1 planer.
| Time | P1 | P2 | P3 | P4 | P5 | P6 | P7 |
|---|---|---|---|---|---|---|---|
| Profit | 10 | 6 | 8 | 4 | 16 | 9 | 3 |
| Grinding | 0.5 | 0.7 | - | - | 0.3 | 0.2 | 0.5 |
| V drilling | 0.1 | 0.2 | - | 0.3 | - | 0.6 | - |
| H drilling | 0.2 | - | 0.8 | - | - | - | 0.6 |
| Boring | 0.05 | 0.03 | - | 0.07 | 0.1 | - | 0.08 |
| Planing | - | - | 0.01 | - | 0.05 | - | 0.05 |
According to this table, product 1 contribute 10 $ to profit, and it need manufacturing time on grinding is 0.5 hours, 0.1 hours in vertical drilling, 0.2 hours on horizon drilling, and 0.05 hours in boring. Machines also scheduled for maintenance. See as follows.
| Month | Machine |
|---|---|
| Jan | One Grinder |
| Feb | Two H Drillers |
| Mar | One Borer |
| Apr | One V Drill |
| May | One Grinder + One V Drill |
| June | One H Drill |
Besides, there have limitations to how many of each product can be sold in a given month. At the Jan, there is no product inventory, but at June, there should be 50 units of each products in inventory.
| Month | P1 | P2 | P3 | P4 | P5 | P6 | P7 |
|---|---|---|---|---|---|---|---|
| Jan | 500 | 1000 | 300 | 300 | 800 | 200 | 100 |
| Feb | 600 | 500 | 200 | 0 | 400 | 300 | 150 |
| Mar | 300 | 600 | 0 | 0 | 500 | 400 | 100 |
| Apr | 200 | 300 | 400 | 500 | 200 | 0 | 100 |
| May | 0 | 100 | 500 | 100 | 1000 | 300 | 0 |
| June | 500 | 500 | 100 | 300 | 1100 | 500 | 60 |
Mathematical Modeling
Sets
$T:$ Set of monthes, $t{0}$ is the first month, and $t{e}$ is the last month.
$P:$ Set of products.
$M:$ Set of machines.
Parameters
$f{pm}:$ Hours given to product $p$ manufactured on machine $m$.
$l{tp}:$ Upper bound on sales for each product $p$ in month $t$.
$k{p}:$ Profit for each product $p$.
$q{tm}:$ Number of avaiable machines $m$ in month $t$.
$g:$ Machine can work $g$ hours per month.
$r:$ Store cost.
$z:$ Store capacity.
Varibales
$b{tp}:$ Number of product $p$ produced in month $t$.
$u{tp}:$ Number of product $p$ selled in month $t$.
$s_{tp}:$ Number of product $p$ stored in month $t$.
Objective Function
Constraints
The last constraint ensures that per month the time all products needs on a certain kind of machines is lower or equal than the available hours for that machine in that month multiplied by the number of available machines in that month.
Gurobi Python Modeling
Basic Gurobi Concepts

Parameters control the operations of Gurobi solver, for example, the optimizaiton running time. Attributes are primary machanism for querying and modifying properties of a Gurobi model, you can set the variable name by variable.VarName = 'Cost'. Environments are the container for models and global parameters settings.

When we build a model with Gurobi Python API, the whole process is as follows:

The Gurobi Python API support linear and quadratic experssion. Here we give a toy example of solving optimization problem use Gurobi Python API.
The Python code:
Solve The Planning Model
Here we give the Python code:
1 | # Data |
Conclusion
Gurobi is a very powful mathematical programming solver, and Python is friendly to be used in modelling optimizaiton problem. Here I just post an example with code to learn how to use Gurobi solve real word planning problem. The orginal figures and codes are refer to Gurobi seminars.