[R] Non linear optimization with nloptr package fail to produce true optimal result

Daniel Lobo d@n|e|obo9976 @end|ng |rom gm@||@com
Fri Dec 13 18:52:15 CET 2024


Hi,

I have below non-linear constraint optimization problem

#Original artificial data

library(nloptr)

set.seed(1)
A <- 1.34
B <- 0.5673
C <- 6.356
D <- -1.234
x <- seq(0.5, 20, length.out = 500)
y <- A + B * x + C * x^2 + D * log(x) + runif(500, 0, 3)

#Objective function

X <- cbind(1, x, x^2, log(x))
f <- function(theta) {
sum(abs(X %*% theta - y))
}

#Constraint

eps <- 1e-4

hin <- function(theta) {
  abs(sum(X %*% theta) - sum(y)) - 1e-3 + eps
}

Hx <- function(theta) {
  X[100, , drop = FALSE] %*% theta - (120 - eps)
}

#Optimization with nloptr

Sol = nloptr(rep(0, 4), f, eval_g_ineq = hin, eval_g_eq = Hx, opts =
list("algorithm" = "NLOPT_LN_COBYLA", "xtol_rel" = 1.0e-8))$solution
# -0.2186159 -0.5032066  6.4458823 -0.4125948

However this does not appear to be optimal value. For example, if I
use below set,
0.222, 6.999, 6.17, -19.371, value of my objective function is lower
that that using nloptr

I just wonder in the package nloptr is good for non-linear optimization?



More information about the R-help mailing list