Gurobi and Python
How Does it Work?
• Write a python script that builds and runs a model, using a Gurobi module
o Can write script in any editor or an Integrated Development Environment (IDE)
• Run the script from a terminal: using gurobi.sh (in mac/linux) or gurobi.bat (in windows), or using your native python installation
Getting Started
1. Go to Gurobi download website:
o https://www.gurobi.com/downloads/gurobi-optimizer-eula/
2. Register for a Gurobi academic account (using your UofT e-mail)
3. Login and download the latest Gurobi Optimizer
4. Install Gurobi (painless; can get help from README file:
https://packages.gurobi.com/9.0/README.txt)
5. Request then get a free academic license – Gurobi documentation explains this well (need to be
connected to the university network, ie UofT VPN, MIE VPN etc)
o https://www.gurobi.com/downloads/end-user-license-agreement-academic/ o Alsomaysee:
e.html
o Testlicenseinstallafter:https://support.gurobi.com/hc/en-us/articles/360034784572-
How-do-I-check-for-a-valid-license-file-
6. Install Gurobi module (called gurobipy) to access Gurobi via Python o Ieformac:
o Go to gurobi directory where there is setup.py file lies, e.g., the default location of “/Library/gurobi900/mac64″
o Execute:“sudopythonsetup.pyinstall”
o To test, try “import gurobipy” in a python script. Can also try an example,e.g., “python
mip1.py” (in the subfolder “examples/python/”) o Quickstartdocumentationcoversthis
Gurobi Quick Start Guides: https://www.gurobi.com/documentation/quickstart.html
• OS specific instructions
• Python examples
o https://www.gurobi.com/resource/functional-code-examples/
Gurobi Parameters: https://www.gurobi.com/documentation/9.0/refman/parameters.html
Some helpful MIP examples (located in install location examples) mip1.py and facility.py
• Much of the interaction with Gurobi is done through attributes of a Gurobi Model
• Gurobi Model attributes: numconstrs, numvars, modelname, runtime, itercount, nodecount,
objval, …
• Gurobi Var (variable) attributes: lb, ub, obj, vtype, varname, x (current solution value), rc
(reduced cost), …
• Gurobi Constr (constraint) attributes: sense, rhs, pi (dual variable value), slack, constrname, …