ROI_solve | R Documentation |
Solve a given optimization problem. This function uses the given solver (or searches for an appropriate solver) to solve the supplied optimization problem.
ROI_solve(x, solver, control = list(), ...)
x |
an optimization problem of class |
solver |
a character vector specifying the solver to use. If
missing, then the default solver returned by
|
control |
a list with additional control parameters for the solver. This is solver specific so please consult the corresponding documentation. |
... |
a list of control parameters (overruling those
specified in |
a list containing the solution and a message from the solver.
solutionthe vector of optimal coefficients
objvalthe value of the objective function at the optimum
statusa list giving the status code and message form the solver. The status code is 0 on success (no error occurred) 1 otherwise.
messagea list giving the original message provided by the solver.
Stefan Theussl
Theussl S, Schwendinger F, Hornik K (2020). 'ROI: An Extensible R Optimization Infrastructure.' Journal of Statistical Software_, *94*(15), 1-64. doi: 10.18637/jss.v094.i15 (URL: https://doi.org/10.18637/jss.v094.i15).
## Rosenbrock Banana Function
## -----------------------------------------
## objective
f <- function(x) {
return( 100 * (x[2] - x[1] * x[1])^2 + (1 - x[1])^2 )
}
## gradient
g <- function(x) {
return( c( -400 * x[1] * (x[2] - x[1] * x[1]) - 2 * (1 - x[1]),
200 * (x[2] - x[1] * x[1])) )
}
## bounds
b <- V_bound(li = 1:2, ui = 1:2, lb = c(-3, -3), ub = c(3, 3))
op <- OP( objective = F_objective(f, n = 2L, G = g),
bounds = b )
res <- ROI_solve( op, solver = "nlminb", control = list(start = c( -1.2, 1 )) )
solution( res )
## Portfolio optimization - minimum variance
## -----------------------------------------
## get monthly returns of 30 US stocks
data( US30 )
r <- na.omit( US30 )
## objective function to minimize
obj <- Q_objective( 2*cov(r) )
## full investment constraint
full_invest <- L_constraint( rep(1, ncol(US30)), "==", 1 )
## create optimization problem / long-only
op <- OP( objective = obj, constraints = full_invest )
## solve the problem - only works if a QP solver is registered
## Not run:
res <- ROI_solve( op )
res
sol <- solution( res )
names( sol ) <- colnames( US30 )
round( sol[ which(sol > 1/10^6) ], 3 )
## End(Not run)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.