Gurobi

Overview

Gurobi is an optimization solver, documentation is available here.

Executing

Modules

This application is available using the modules command.

Version 8.1.1

[user@mercury ~]$ module load gurobi/8.1/8.1.1
[user@mercury ~]$ gurobi_cl --help
Gurobi command line optimizer, version 8.1.1 build v8.1.1rc0 (linux64).
Copyright (c) 2016, Gurobi Optimization, Inc.

Usage: gurobi_cl [--command]* [param=value]* filename

Gurobi parameters are documented in the Gurobi Reference Manual.

Accepted commands are:
   --version: version information
   --license: license information
   --tokens:  details on tokens currently in use
   --status:  current Gurobi Services status
   --adminpassword, --joblimit, --killjob, --server: Gurobi Services
         administrative commands

A few usage examples:
  gurobi_cl Method=2 ResultFile=afiro.sol afiro.mps
  gurobi_cl --server=localhost --status

Visit www.gurobi.com/documentation/8.1 for further details on how to use this program.

Version 9.0.3

[user@mercury ~]$ module load gurobi/9.0/9.0.3
[user@mercury ~]$ gurobi_cl --help
Gurobi command line optimizer, version 9.0.3 build v9.0.3rc0 (linux64).
Copyright (c) 2020, Gurobi Optimization, LLC

Usage: gurobi_cl [--command]* [param=value]* filename

Gurobi parameters are documented in the Gurobi Reference Manual.

Accepted commands are:
   --version: version information
   --license: license information
   --tokens:  details on tokens currently in use
   --status:  current Gurobi Services status
   --newadminpassword, --joblimit, --killjob, --server: Gurobi Services
         administrative commands

A few usage examples:
    gurobi_cl misc07.mps
    gurobi_cl Record=1 Method=2 ResultFile=p0033.sol InputFile=p0033.mst InputFile=p0033.hnt.gz LogFile=p0033.log p0033.mps
    gurobi_cl --server=localhost --status

Visit www.gurobi.com/documentation/9.0 for further details on how to use this program.

Submitting Batch

Code examples can be found by visiting the Gurobi Optimizer Example Tour. In addition, the code examples are hosted on the compute nodes at $GUROBI_HOME/examples. The following shows how to use Gurobi 8.1 to solve a simple quadratically constrained program (QCP) in Python 3.6.12. For gurobi 9.0, use Python 3.8.5

R

Code

qcp.R
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# This example formulates and solves the following simple QCP model:
#     maximize    x
#     subject to  x + y + z = 1
#                 x^2 + y^2 <= z^2 (second-order cone)
#                 x^2 <= yz        (rotated second-order cone)

>library('gurobi')
Loading required package: slam

>library(Matrix)

>model <- list()

>model$A          <- matrix(c(1,1,1), nrow=1, byrow=T)
>model$modelsense <- 'max'
>model$obj        <- c(1,0,0)
>model$rhs        <- c(1)
>model$sense      <- c('=')

# First quadratic constraint: x^2 + y^2 - z^2 <= 0
>qc1 <- list()
>qc1$Qc <- spMatrix(3, 3, c(1, 2, 3), c(1, 2, 3), c(1.0, 1.0, -1.0))
>qc1$rhs <- 0.0

# Second quadratic constraint: x^2 - yz <= 0
>qc2 <- list()
>qc2$Qc <- spMatrix(3, 3, c(1, 2), c(1, 3), c(1.0, -1.0))
>qc2$rhs <- 0.0

>model$quadcon <- list(qc1, qc2)

>result <- gurobi(model)
Optimize a model with 1 rows, 3 columns and 3 nonzeros
Model has 2 quadratic constraints
Coefficient statistics:
  Matrix range     [1e+00, 1e+00]
  QMatrix range    [1e+00, 1e+00]
  Objective range  [1e+00, 1e+00]
  Bounds range     [0e+00, 0e+00]
  RHS range        [1e+00, 1e+00]
Presolve time: 0.00s
Presolved: 6 rows, 6 columns, 13 nonzeros
Presolved model has 2 second-order cone constraints
Ordering time: 0.00s

Barrier statistics:
 AA' NZ     : 1.500e+01
 Factor NZ  : 2.100e+01
 Factor Ops : 9.100e+01 (less than 1 second per iteration)
 Threads    : 1

                  Objective                Residual
Iter       Primal          Dual         Primal    Dual     Compl     Time
   0   2.38095238e-01  2.38095238e-01  8.33e-17 4.33e-01  9.23e-02     0s
   1   3.20481543e-01  3.62123302e-01  2.78e-17 1.39e-02  7.95e-03     0s
   2   3.26649101e-01  3.28651430e-01  1.11e-14 5.44e-04  3.46e-04     0s
   3   3.26797051e-01  3.27019441e-01  5.69e-13 5.98e-10  2.78e-05     0s
   4   3.26990986e-01  3.26994814e-01  2.91e-13 3.48e-13  4.78e-07     0s
   5   3.26992304e-01  3.26992876e-01  8.91e-11 2.63e-14  7.15e-08     0s

Barrier solved model in 5 iterations and 0.00 seconds
Optimal objective 3.26992304e-01
>print(result$objval)
[1] 0.3269923
>print(result$x)
[1] 0.3269923 0.2570664 0.4159413
# Clear space
>rm(model, result)

Python

Code

qcp.py
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#!/usr/bin/env python
# Copyright 2017, Gurobi Optimization, Inc.
# This example formulates and solves the following simple QCP model:
#     maximize    x
#     subject to  x + y + z = 1
#                 x^2 + y^2 <= z^2 (second-order cone)
#                 x^2 <= yz        (rotated second-order cone)

from gurobipy import *

# The gurobi log file, this will be placed in the same directory as the code being run if you use the -cwd option on the grid engine
env = Env("model-out.log")

# Create a new model
m = Model("qcp", env)

# Create variables
x = m.addVar(name="x")
y = m.addVar(name="y")
z = m.addVar(name="z")

# Set objective: x
obj = 1.0*x
m.setObjective(obj, GRB.MAXIMIZE)

# Add constraint: x + y + z = 1
m.addConstr(x + y + z == 1, "c0")

# Add second-order cone: x^2 + y^2 <= z^2
m.addConstr(x*x + y*y <= z*z, "qc0")

# Add rotated cone: x^2 <= yz
m.addConstr(x*x <= y*z, "qc1")

# Optimize
m.optimize()

# dump output
for v in m.getVars():
print('%s %g' % (v.varName, v.x))
print('Obj: %g' % obj.getValue())

Submission script

Create a job submission script in the same directory as your code:

submit_qcp.qsub
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
#!/bin/bash

#SBATCH --job-name gurobi_qcp

# load the module
module load python/booth/3.6/3.6.12
module load gurobi/8.1/8.1.1

# Run the Python code
python3 qcp.py

Submission

Submit the job to the Slurm scheduler

[username@mercury gurobi]$ sbatch submit_qcp.sh

Output

There should now be stdout, stderr and a model-out.log file in the same directory as the code.

[username@mercury gurobi]$ more model-out.log
Gurobi 8.1.1 (linux64, Python) logging started Mon Oct 12 14:05:17 2020

Optimize a model with 1 rows, 3 columns and 3 nonzeros

Model has 2 quadratic constraints

Coefficient statistics:
  Matrix range     [1e+00, 1e+00]
  QMatrix range    [1e+00, 1e+00]
  Objective range  [1e+00, 1e+00]
  Bounds range     [0e+00, 0e+00]
  RHS range        [1e+00, 1e+00]

Presolve time: 0.00s
Presolved: 6 rows, 6 columns, 13 nonzeros
Presolved model has 2 second-order cone constraints
Ordering time: 0.00s

Barrier statistics:
 AA' NZ     : 1.500e+01
 Factor NZ  : 2.100e+01
 Factor Ops : 9.100e+01 (less than 1 second per iteration)
 Threads    : 1
                  Objective                Residual
Iter       Primal          Dual         Primal    Dual     Compl     Time
   0   2.38095238e-01  2.38095238e-01  8.33e-17 4.33e-01  9.23e-02     0s
   1   3.20481543e-01  3.62123302e-01  2.78e-17 1.39e-02  7.95e-03     0s
   2   3.26649101e-01  3.28651430e-01  1.11e-14 5.44e-04  3.46e-04     0s
   3   3.26797051e-01  3.27019441e-01  5.69e-13 5.98e-10  2.78e-05     0s
   4   3.26990986e-01  3.26994814e-01  2.91e-13 3.48e-13  4.78e-07     0s
   5   3.26992304e-01  3.26992876e-01  8.91e-11 2.63e-14  7.15e-08     0s
Barrier solved model in 5 iterations and 0.00 seconds

Optimal objective 3.26992304e-01

Errata and Miscellany

  • The Python modules for Gurobi 8.1.1 and 9.0.3 are installed on the Booth Python versions 3.6.12 and 3.8.5 respectively which are available using the module command.
  • The R packages for Gurobi 8.1.1 and 9.0.3 are installed on the Booth R versions 3.6.2 and 4.0.2 respectively which are available using the module command.
  • Note that in ALL version of Gurobi installed on Mercury, the Gurobi shell (gurobi.sh) will use the Gurobi internally included Python version 2.7.13 and not the ones supplied by Booth on Mercury. If you wish to use the shell with another version of Python, copy the gurobi.sh script to your home directory, modify it to use another Python and execute it from there. We will not support any alternate version.