Gurobi

Overview

Gurobi is an optimization solver, documentation is available here.

Executing

Modules

This application is available using the modules command.

Version 10.0.1

[user@mercury ~]$ module load gurobi/10.0/10.0.1
[user@mercury ~]$ gurobi_cl --help
Gurobi command line optimizer, version 10.0.1 build v10.0.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/10.0.1 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 10.0 to solve a simple quadratically constrained program (QCP) in Python 3.8.16

R

Code

qcp.R
 1# This example formulates and solves the following simple QCP model:
 2#     maximize    x
 3#     subject to  x + y + z = 1
 4#                 x^2 + y^2 <= z^2 (second-order cone)
 5#                 x^2 <= yz        (rotated second-order cone)
 6
 7>library('gurobi')
 8Loading required package: slam
 9
10>library(Matrix)
11
12>model <- list()
13
14>model$A          <- matrix(c(1,1,1), nrow=1, byrow=T)
15>model$modelsense <- 'max'
16>model$obj        <- c(1,0,0)
17>model$rhs        <- c(1)
18>model$sense      <- c('=')
19
20# First quadratic constraint: x^2 + y^2 - z^2 <= 0
21>qc1 <- list()
22>qc1$Qc <- spMatrix(3, 3, c(1, 2, 3), c(1, 2, 3), c(1.0, 1.0, -1.0))
23>qc1$rhs <- 0.0
24
25# Second quadratic constraint: x^2 - yz <= 0
26>qc2 <- list()
27>qc2$Qc <- spMatrix(3, 3, c(1, 2), c(1, 3), c(1.0, -1.0))
28>qc2$rhs <- 0.0
29
30>model$quadcon <- list(qc1, qc2)
31
32>result <- gurobi(model)
33Optimize a model with 1 rows, 3 columns and 3 nonzeros
34Model has 2 quadratic constraints
35Coefficient statistics:
36  Matrix range     [1e+00, 1e+00]
37  QMatrix range    [1e+00, 1e+00]
38  Objective range  [1e+00, 1e+00]
39  Bounds range     [0e+00, 0e+00]
40  RHS range        [1e+00, 1e+00]
41Presolve time: 0.00s
42Presolved: 6 rows, 6 columns, 13 nonzeros
43Presolved model has 2 second-order cone constraints
44Ordering time: 0.00s
45
46Barrier statistics:
47 AA' NZ     : 1.500e+01
48 Factor NZ  : 2.100e+01
49 Factor Ops : 9.100e+01 (less than 1 second per iteration)
50 Threads    : 1
51
52                  Objective                Residual
53Iter       Primal          Dual         Primal    Dual     Compl     Time
54   0   2.38095238e-01  2.38095238e-01  8.33e-17 4.33e-01  9.23e-02     0s
55   1   3.20481543e-01  3.62123302e-01  2.78e-17 1.39e-02  7.95e-03     0s
56   2   3.26649101e-01  3.28651430e-01  1.11e-14 5.44e-04  3.46e-04     0s
57   3   3.26797051e-01  3.27019441e-01  5.69e-13 5.98e-10  2.78e-05     0s
58   4   3.26990986e-01  3.26994814e-01  2.91e-13 3.48e-13  4.78e-07     0s
59   5   3.26992304e-01  3.26992876e-01  8.91e-11 2.63e-14  7.15e-08     0s
60
61Barrier solved model in 5 iterations and 0.00 seconds
62Optimal objective 3.26992304e-01
63>print(result$objval)
64[1] 0.3269923
65>print(result$x)
66[1] 0.3269923 0.2570664 0.4159413
67# Clear space
68>rm(model, result)

Python

Code

qcp.py
 1#!/usr/bin/env python
 2# Copyright 2017, Gurobi Optimization, Inc.
 3# This example formulates and solves the following simple QCP model:
 4#     maximize    x
 5#     subject to  x + y + z = 1
 6#                 x^2 + y^2 <= z^2 (second-order cone)
 7#                 x^2 <= yz        (rotated second-order cone)
 8
 9from gurobipy import *
10
11# 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
12env = Env("model-out.log")
13
14# Create a new model
15m = Model("qcp", env)
16
17# Create variables
18x = m.addVar(name="x")
19y = m.addVar(name="y")
20z = m.addVar(name="z")
21
22# Set objective: x
23obj = 1.0*x
24m.setObjective(obj, GRB.MAXIMIZE)
25
26# Add constraint: x + y + z = 1
27m.addConstr(x + y + z == 1, "c0")
28
29# Add second-order cone: x^2 + y^2 <= z^2
30m.addConstr(x*x + y*y <= z*z, "qc0")
31
32# Add rotated cone: x^2 <= yz
33m.addConstr(x*x <= y*z, "qc1")
34
35# Optimize
36m.optimize()
37
38# dump output
39for v in m.getVars():
40print('%s %g' % (v.varName, v.x))
41print('Obj: %g' % obj.getValue())

Submission script

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

submit_qcp.qsub
 1#!/bin/bash
 2
 3#SBATCH --job-name gurobi_qcp
 4
 5# load the module
 6module load python/booth/3.8
 7module load gurobi/10.0/10.0.1
 8
 9# Run the Python code
10python3 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