Skip to content

Gurobi Optimizer

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.

Availability

Cluster Module/Version
BOSE gurobi/11.0.0
BGSC Not Available

Note: You can simply use module load gurobi 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 installation location of Gurobi files such as examples, lib, or include.

Executables

The following list are the most common executables one may use for running Gurobi-based optimizations.

Executable Purpose
gurobi_cl Gurobi's command line interface
gurobi.sh Interactive Python-based shell
gurobipy script.py Run Python-based .py scripts (same as python3.11 below)
python3.11 script.py Run Python-based .py scripts (official)

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:

     maximize    x +   y + 2 z
     subject to  x + 2 y + 3 z <= 4
                 x +   y       >= 1
                 x, y, z binary

Online Reference: mip1.py

Gurobi provides its own version of Python under the executable python3.11, which we created a shortcut for called gurobipy.

There are two primary ways to run a Gurobi-based Python script on our clusters:

Method 1 - Calling 'gurobipy' or 'python3.11'

module load gurobi
cp $GUROBI_HOME/examples/python/mip1.py ./      # Copy example from Gurobi (same as above link)
gurobipy mip1.py    # Or use the official 'python3.11' executable

Method 2 - Running directly as an executable

Note that this method requires your Python file to start with "#!/usr/bin/env python3.11".

module load gurobi
cp $GUROBI_HOME/examples/python/mip1.py ./      # Copy example from Gurobi (same as above link)
chmod +x mip1.py    # Make the file executable
./mip1.py

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 gurobi.sh 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

sinteract

2) Load the Gurobi Optimizer module

module load gurobi

3) Start the Gurobi shell, which is based on Python

gurobi.sh

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

m.optimize()

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%

Online Reference: mip1_c++.cpp

module load gcc         # Needed to compile C++ programs
module load gurobi
cp $GUROBI_HOME/examples/c++/mip1_c++.cpp ./      # Copy example from Gurobi (same as above link)
g++ -I$GUROBI_HOME/include -L$GUROBI_HOME/lib -o mip1 mip1_c++.cpp -lgurobi_c++ -lgurobi110
./mip1

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.

module load gurobi
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

submit.sh
#!/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 gurobi    # Load Gurobi Optimizer
gurobi_cl ResultFile=solution.sol input-file.lp

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. (2023). Gurobi Optimizer Reference Manual. Retrieved from https://www.gurobi.com

Resources