Gurobi ------ Overview ######## `Gurobi`_ is an optimization solver, documentation is available `here`_. .. _Gurobi: http://www.gurobi.com/ .. _here: http://www.gurobi.com/documentation/ Executing ######### Modules $$$$$$$ This application is available using the modules command. Version 10.0.1 %%%%%%%%%%%%%% .. code-block:: bash [user@mercury ~]$ module load gurobi/10.0/10.0.1 .. code-block:: console [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 .. _Gurobi Optimizer Example Tour: http://www.gurobi.com/documentation/8.1/examples/index.html R $$$$$$ Code %%%% .. code-block:: R :linenos: :caption: qcp.R # 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 %%%% .. code-block:: python :linenos: :caption: qcp.py #!/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: .. code-block:: bash :linenos: :caption: submit_qcp.qsub #!/bin/bash #SBATCH --job-name gurobi_qcp # load the module module load python/booth/3.8 module load gurobi/10.0/10.0.1 # Run the Python code python3 qcp.py Submission %%%%%%%%%% Submit the job to the Slurm scheduler .. code-block:: bash [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. .. code-block:: console [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