The following example solves the Rosenbrock function (https://en.wikipedia.org/wiki/Rosenbrock_function).
minimize \ f(x) = 100 (x_2 - x_1^2)^2 + (1 - x_1)^2
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | library(ROI)
f <- function(x) {
return( 100 * (x[2] - x[1]^2)^2 + (1 - x[1])^2 )
}
f.gradient <- function(x) {
return( c( -400 * x[1] * (x[2] - x[1] * x[1]) - 2 * (1 - x[1]),
200 * (x[2] - x[1] * x[1])) )
}
control <- list(algorithm = "NLOPT_GD_MLSL",
maxeval = 10000,
population = 4,
local_opts = list(algorithm = "NLOPT_LD_LBFGS", xtol_rel = 1e-4))
x <- OP( objective = F_objective(f, n=1L, G=f.gradient),
bounds = V_bound(li=1:2, ui=1:2, lb=c(-3, -3), ub=c(3, 3)) )
nlp <- ROI_solve(x, solver="nloptr", control, start=c(-1.2, 1))
nlp
## Optimal solution found.
## The objective value is: 3.049556e-23
solution(nlp)
## [1] 1 1
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.