The Gurobi R interface can be used to solve optimization problems of
the following form:
minimize |
![]() |
|
subject to | ![]() |
(linear constraints) |
![]() |
(bound constraints) | |
some ![]() |
(integrality constraints) | |
some ![]() |
(cone constraints) | |
![]() |
(quadratic constraints) | |
some ![]() |
(special ordered set constraints) |
Many of the model components listed here are optional. For example, integrality constraints may be omitted. We'll discuss the details of how models are represented shortly.
The following function allows you to take a model represented using R data structures and solve it with the Gurobi Optimizer:
gurobi | ( model, params=NULL ) |
The two arguments to this function are R list
variables, each
consisting of multiple named components. The first argument
contains the optimization model to be solved. The second contains an
optional list of Gurobi parameters to be modified during the solution
process. The return value of this function is a list, also consisting
of multiple named components. It contains the result of performing
the optimization on the specified model. We'll now discuss the
details of each of these lists.
The optimization model
As we've mentioned, the model
argument to the gurobi()
function is a list
variable, containing multiple
named components that represent the various parts
of the optimization
model. Several of these components are optional.
Note that you refer to a
named component of an R list variable by appending a dollar sign
followed by the component name to the list variable name. For
example, model$A
refers to component A
of list
variable model
.
The following is an enumeration of all of the named components of the
model
argument that Gurobi will take into account when
optimizing the model:
sparseMatrix
from the Matrix
package, or simple_triplet_matrix
from the slam
package.
c
vector in the
problem statement above). You must specify one value for each column
of A
.
'='
, '<='
, or '>='
. You must specify one value for each row of
A
.
A
.
A
. When absent, each
variable has a lower bound of 0.
A
. When absent, the
variables have infinite upper bounds.
'C'
(continuous), 'B'
(binary), 'I'
(integer), 'S'
(semi-continuous), or
'N'
(semi-integer). Binary variables must be either 0 or 1.
Integer variables can take any integer value between the specified
lower and upper bounds. Semi-continuous variables can take any
value between the specified lower and upper bounds, or a value of
zero. Semi-integer variables can take any integer value between the
specified lower and upper bounds, or a value of zero. When present,
you must specify one value for each column of A
. When
absent, each variable is treated as being continuous.
Refer to this section
for more information on variable types.
A
.
vbasis
description for details. When present,
you must specify one value for each row of A
.
Q
must be a square matrix whose row and column counts are equal to the
number of columns in A
. The Q matrix can be dense or sparse.
Sparse matrices should be built using either sparseMatrix
from the Matrix
package, or simple_triplet_matrix
from the slam
package.
Qc
matrix must be a square matrix whose row and column counts are equal
to the number of columns of A
. The matrix associated
with quadratic constraint model$quadcon[[i]]$Qc
. The optional q
vector defines
the linear terms in the constraint. If present, you must specify one
value for q
for each column of A
.
It is stored in model$quadcon[[i]]$q
. The scalar
beta
defines the right-hand side of the constraint. It is stored in
model$quadcon[[i]]$rhs
.
model$sos[[i]]$type
. A type 1 SOS constraint is
a set of variables for which at most one variable in the set may
take a value other than zero. A type 2 SOS constraint is an ordered
set of variables where at most two variables in the set may take
non-zero values. If two take non-zeros values, they must be
contiguous in the ordered set. The members of an SOS constraint are
specified by placing their indices in vector
model$sos[[i]]$index
. Weights associated with SOS members
are provided in vector model$sos[[i]]$weight
.
Please refer to
this section
for details on SOS constraints.
model$pwlobj[[i]]$var
. The model$pwlobj[[i]]$x
. The values in the model$pwlobj[[i]]$y
.
NA
, which
instructs the MIP solver to try to fill in a value for that
variable.
gurobi()
function will return an error.
Below is an example that demonstrates the construction of a simple optimization model:
model <- list() model$A <- matrix(c(1,1,0,0,1,1), nrow=2, byrow=T) model$obj <- c(1,1,2) model$modelsense <- "max" model$rhs <- c(1,1) model$sense <- c('<=', '<=')
You can also build A
as a sparse matrix,
using either sparseMatrix
or simple_triplet_matrix
:
model$A <- spMatrix(2, 3, c(1, 1, 2, 2), c(1, 2, 2, 3), c(1, 1, 1, 1)) model$A <- simple_triplet_matrix(c(1, 1, 2, 2), c(1, 2, 2, 3), c(1, 1, 1, 1))
Note that the Gurobi interface allows you to specify a scalar value
for any of the array-valued components. The specified value will be
expanded to an array of the appropriate size, with each component of
the array equal to the scalar (e.g., model$rhs <- 1
would be
equivalent to model$rhs <- c(1,1)
in the example).
The parameter list
The optional params
argument to the gurobi()
function
is also a list of named components. For each component, the name
should be the name of a Gurobi parameter, and the associated value
should be the desired value of that parameter. Gurobi parameters
allow users to modify the default behavior of the Gurobi optimization
algorithms. You can find a complete list of the available Gurobi
parameters here.
To create a list that would set the Gurobi
Method parameter to 2 and the
ResultFile parameter
parameter to model.mps
,
you would do the following:
params <- list(Method=2, ResultFile='model.mps')
We should say a bit more about the
ResultFile
parameter. If this parameter is set, the optimization
model that is eventually passed to Gurobi will also be output to the
specified file. The filename suffix should be one of .mps
,
.lp
, .rew
, or .rlp
,
to indicate the desired file format (see the
file formats
section for details on Gurobi file formats).
The optimization result
The gurobi()
function returns a list, with the various results
of the optimization stored in its named components. The specific
results that are available depend on the type of model that was
solved, and the status of the optimization.
The following is a list of components that might be available
in the result list. We'll discuss the circumstances under which
each will be available after presenting the list.
"OPTIMAL"
, which indicates that an
optimal solution to the model was found. Other status are possible,
for example if the model has no feasible solution or if you set a
Gurobi parameter that leads to early solver termination. See the
Status Code section for further
information on the Gurobi status codes.
A
.
A
.
A
.
A
.
vbasis
and cbasis
arrays into
the corresponding components for the next model. This array
contains one entry for each column of A
.
A
.
status
component will be present in all cases. It
indicates whether Gurobi was able to find a proven optimal solution to
the model. In cases where a solution to the model was found, optimal
or otherwise, the objval
, x
, and slack
components will be present. For linear and quadratic programs, if a
solution is available, then the pi
and rc
components
will also be present. Finally, if the final solution is a
basic solution (computed by simplex), then vbasis
and
cbasis
will be present.
The following is an example of how the results of the gurobi()
call might be extracted and output:
result <- gurobi(model, params) print(result$objval) print(result$x)
Please consult this section for a discussion of some of the practical issues associated with solving a precisely defined mathematical model using finite-precision floating-point arithmetic.