Learn R Programming

ROI (version 0.2-1)

OP: Optimization Problem Constructor

Description

Optimization problem constructor

Usage

OP(objective, constraints = NULL, types = NULL, bounds = NULL, maximum = FALSE)
as.OP(x)

Arguments

objective
an object inheriting from class "objective".
constraints
an object inheriting from class "constraints".
types
a character vector giving the types of the objective variables, with "C", "I", and "B" corresponding to continuous, integer, and binary, respectively, or NULL (default), taken as all-continuous. Recycled as needed.
bounds
NULL (default) or a list with elements upper and lower containing the indices and corresponding bounds of the objective variables. The default for each variable is a bound between 0 and Inf.
maximum
a logical giving the direction of the optimization. TRUE means that the objective is to maximize the objective function, FALSE (default) means to minimize it.
x
an R object.

Value

A list containing the optimal solution, with the following components.
solution
the vector of optimal coefficients
objval
the value of the objective function at the optimum
status
an integer with status information about the solution returned: 0 if the optimal solution was found, a non-zero value otherwise
msg
the status code and additional information about the solution provided by the solver.

Examples

Run this code
## Simple linear program.
## maximize:   2 x_1 + 4 x_2 + 3 x_3
## subject to: 3 x_1 + 4 x_2 + 2 x_3 <= 60
##             2 x_1 +   x_2 +   x_3 <= 40
##               x_1 + 3 x_2 + 2 x_3 <= 80
##               x_1, x_2, x_3 are non-negative real numbers

LP <- OP( c(2, 4, 3),
          L_constraint(L = matrix(c(3, 2, 1, 4, 1, 3, 2, 2, 2), nrow = 3),
                       dir = c("<=", "<=", "<="),
                       rhs = c(60, 40, 80)),
          max = TRUE )
LP

## Simple quadratic program.
## minimize: - 5 x_2 + 1/2 (x_1^2 + x_2^2 + x_3^2)
## subject to: -4 x_1 - 3 x_2       >= -8
##              2 x_1 +   x_2       >=  2
##                    - 2 x_2 + x_3 >=  0

QP <- OP( Q_objective (Q = diag(1, 3), L = c(0, -5, 0)),
          L_constraint(L = matrix(c(-4,-3,0,2,1,0,0,-2,1),
                                  ncol = 3, byrow = TRUE),
                       dir = rep(">=", 3),
                       rhs = c(-8,2,0)) )
QP

Run the code above in your browser using DataLab