Gurobi Optimizer (Python API)
Gurobi Optimizer is an solver that incorporates mathematical models to make optimized decisions and supports a variety of different interfaces, such as Python, C, C++, Java, R, and more.
Other Gurobi APIs
This guide focuses exclusively on the Python API version of Gurobi installed using conda. If you are planning on using the Gurobi APIs to work with C, C++, or Java, then check out our other guide.
Availability
| Cluster | Module/Version |
|---|---|
| BOSE | gurobipy/12.0.3 |
| BGSC | Not Available |
Note: You can simply use module load gurobipy to activate the most recently installed version of this software.
Environment Variables
The following environment variables are available to use in your scripts.
| Variable | Purpose |
|---|---|
| $GUROBI_HOME | Points to the installation location of Gurobi files. |
Additional Python Packages
If you have a recommendation on what Python packages we should install for everyone, please contact us.
Right now the following have been installed in addition to Gurobi:
- numpy
- pandas
Executables
The following list are the most common executables one may use for running Gurobi-based optimizations.
| Executable | Purpose |
|---|---|
| gurobi.sh | Interactive Python-based shell |
| python script.py | Run Python-based .py scripts |
| python3.11 script.py | Run Python-based .py scripts (same as above) |
| gurobi_cl | Gurobi's command line interface |
Gurobi Examples
MIP Model
The follow commands show examples of different ways to forumlate and solve a simple MIP model, which is one of the examples provided by Gurobi in $GUROBI_HOME/examples.
From Gurobi:
Online Reference: mip1.py
There are two primary ways to run a Gurobi-based Python script on our clusters:
Method 1 - Calling 'python' or 'python3.11'
module load gurobipy
cp $GUROBI_HOME/examples/python/mip1.py ./ # Copy example from Gurobi (same as above link)
python mip1.py
Method 2 - Running directly as an executable
Note that this method requires your Python file to start with "#!/usr/bin/env python3.11".
Online Reference: mip1.py
This example is the same one as in the "Python" tab, but is shown how to run the commands interactively with the python shell.
Using Slurm?
Running commands interactively are best done either through using sinteract or Desktop Mode in Open OnDemand. We do not recommend doing this with the sbatch command.
1) Start an interactive session on the cluster to bring you to a compute node
2) Load the Gurobi Optimizer module
3) Start the Gurobi shell, which is based on Python
4) Type the following Python code and press 'Enter/Return' after each line
import gurobipy as gp
from gurobipy import GRB
# Create a new model
m = gp.Model("mip1")
# Create variables with the following code:
x = m.addVar(vtype=GRB.BINARY, name="x")
y = m.addVar(vtype=GRB.BINARY, name="y")
z = m.addVar(vtype=GRB.BINARY, name="z")
# Set objective
m.setObjective(x + y + 2 * z, GRB.MAXIMIZE)
# Add constraint: x + 2 y + 3 z <= 4
m.addConstr(x + 2 * y + 3 * z <= 4, "c0")
# Add constraint: x + y >= 1
m.addConstr(x + y >= 1, "c1")
5) Run the optimization
6) See the results
Gurobi Optimizer version 11.0.0 build v11.0.0rc2 (linux64 - "CentOS Linux 8")
CPU model: AMD EPYC 7452 32-Core Processor, instruction set [SSE2|AVX|AVX2]
Thread count: 64 physical cores, 64 logical processors, using up to 32 threads
Optimize a model with 2 rows, 3 columns and 5 nonzeros
Model fingerprint: 0x98886187
Variable types: 0 continuous, 3 integer (3 binary)
Coefficient statistics:
Matrix range [1e+00, 3e+00]
Objective range [1e+00, 2e+00]
Bounds range [1e+00, 1e+00]
RHS range [1e+00, 4e+00]
Found heuristic solution: objective 2.0000000
Presolve removed 2 rows and 3 columns
Presolve time: 0.00s
Presolve: All rows and columns removed
Explored 0 nodes (0 simplex iterations) in 0.00 seconds (0.00 work units)
Thread count was 1 (of 64 available processors)
Solution count 2: 3 2
Optimal solution found (tolerance 1.00e-04)
Best objective 3.000000000000e+00, best bound 3.000000000000e+00, gap 0.0000%
Command Line / LP File - Coins
Online Reference: Coin Tutorial
The following code is based on the above tutorial and uses the example file coins.lp from Gurobi. Through this model, you can specify the minerals required to produce coins, and find the combination that gives you the highest dollar value.
Note: This isn't specific to the Python API, but it's included in the conda variant of Gurobi so we're including it here as well.
module load gurobipy
cp $GUROBI_HOME/examples/data/coins.lp ./ # Copy example from Gurobi (same as above link)
gurobi_cl ResultFile=coins.sol coins.lp # Run and create a solution file
cat coins.sol # View solution file
Sample Slurm Script
#!/bin/bash
# -- SLURM SETTINGS -- #
# [..] other settings here [..]
# The following settings are for the overall request to Slurm
#SBATCH --ntasks-per-node=32 # How many CPU cores do you want to request
#SBATCH --nodes=1 # How many nodes do you want to request
# -- SCRIPT COMMANDS -- #
# Load the needed modules
module load gurobipy # Load Gurobi Optimizer
python script.py
Real Example
Has your research group used Gurobi Optimizer in a project? Contact the HPC Team and we'd be glad to feature your work.
Citation
Please include the following citation in your papers to support continued development of Gurobi Optimizer.
Gurobi Optimization, LLC. (2024). Gurobi Optimizer Reference Manual. Retrieved from https://www.gurobi.com