Skip to content

The notebooks of the course

Every chapter with a model has its own notebook: one click on the badge opens it in Google Colab, it installs the solver by itself and runs in the browser — nothing to install on your machine. It is the very same code as the scripts in python/, cell by cell, with the figures appearing below the cells instead of being written to a file.

The pip licence is enough

The licence bundled with gurobipy is limited to 2000 variables and 2000 constraints, and every model of the course fits easily: the largest one — electric vehicle charging — uses 144 and 30. For larger models, activate the free academic licence at portal.gurobi.com.

Chapter Class Notebook
Production with quadratic smoothing convex QP Open in Colab
Supply chain with congestion and sustainability LP / convex NLP Open in Colab
The Markowitz portfolio convex QP Open in Colab
Pricing and revenue management concave / non-convex NLP Open in Colab
Advertising budget allocation convex NLP Open in Colab
Continuous location of a service convex NLP Open in Colab
Electric vehicle charging with a smooth profile convex QP Open in Colab
Service capacity and waiting times convex NLP (M/M/1 queue) Open in Colab
Support Vector Machine: optimization for machine learning convex QP Open in Colab
Least-squares regression convex QP Open in Colab

How they are made

The notebooks are not written by hand: they are generated from the scripts with

python3 python/make_notebooks.py

The chapter script remains the single source of the code — the notebook takes its docstring, sections and comments from it — and whoever prefers the command line keeps running, from the python/ folder:

python3 lab06_markowitz.py

Installation and licence

Working locally needs only gurobipy:

python3 -m pip install gurobipy

The pip package ships with a demo licence (up to 2000 variables and 2000 constraints): enough for every model in this lab. At start-up the line Restricted license - for non-production use only appears: this is normal.

Full academic licence (free of charge):

  1. register at https://portal.gurobi.com with your institutional email (@uniroma1.it);
  2. request a Named-User Academic License;
  3. run the command grbgetkey XXXXXXXX-... shown by the portal (you need the university network or a VPN);
  4. the licence is saved in ~/gurobi.lic and from that moment there are no size limits.

Quick check:

import gurobipy as gp
print(gp.gurobi.version())        # e.g. (13, 0, 3)

Regenerating everything

python3 -m pip install gurobipy matplotlib pandas scipy   # scipy: statistical functions only
python3 python/run_all.py             # regenerates data, results and figures

The repository holds all the Python scripts and the case study data in CSV format.