Search code examples
rmathematical-optimizationnonlinear-optimizationnlopt

Formulating inequality constraints in NLOPTR's ISRES algorithm


I got stuck while applying NLOPTR's ISRES algorithm to a nonlinear problem with inequality constraints. I formulated it like this:

library(nloptr)
fn <- function(x) {
(x[1]-10)^2 + 5*(x[2]-12)^2 + x[3]^4 + 3*(x[4]-11)^2 + 10*x[5]^6 + 7*x[6]^2 + x[7]^4 - 4*x[6]*x[7] - 10*x[6] - 8*x[7]
}

hin <- function(x) {
h <- numeric(4)
h[1] <- 127 - 2*x[1]^2 - 3*x[2]^4 - x[3] - 4*x[4]^2 - 5*x[5]
h[2] <- 282 - 7*x[1] - 3*x[2] - 10*x[3]^2 - x[4] + x[5]
h[3] <- 196 - 23*x[1] - x[2]^2 - 6*x[6]^2 + 8*x[7]
h[4] <- -4*x[1]^2 - x[2]^2 + 3*x[1]*x[2] -2*x[3]^2 - 5*x[6] +11*x[7]
return(h)
}

x0 <- c(1, 2, 0, 4, 0, 1, 1)

isres(x0 = x0, fn = fn, hin = hin)

I get a message which says "Error in match(hin): argument "table" is missing, with no default"

I guess I'm not doing something right with the inequality constraints. Can you please tell me how to solve this? Many Many thanks!


Solution

  • This is not really an answer. But I currently cannot comment.

    I think the problem are not your constraints but the function isres. If you take a look into the code of the function (just type isres into your console), the start looks like this:

    function (x0, fn, lower, upper, hin = NULL, heq = NULL, maxeval = 10000, 
              pop.size = 20 * (length(x0) + 1), xtol_rel = 1e-06, nl.info = FALSE, 
              ...) 
    {
      opts <- list()
      opts$maxeval <- maxeval
      opts$xtol_rel <- xtol_rel
      opts$population <- pop.size
      opts$algorithm <- "NLOPT_GN_ISRES"
      fun <- match.fun(fn)
      fn <- function(x) fun(x, ...)
      if (!is.null(hin)) {
        .hin <- match(hin)
        hin <- function(x) (-1) * .hin(x)
      } 
    ...
    

    Now, please note the line .hin <- match(hin). The match function is base R and should take two arguments, i.e.: x and table as it executes x %in% table (check ?match in the R documentation for more info). But, isres does not provide a table to the function.

    As it stands know you have 3 options to solve your problem:

    • (I) Using nloptr directly with the algorithm "NLOPT_GN_ISRES".
    • (II) The mantainer of the package told me that the bug is probably allready known. Thus, you are likely fine if you use the developement package on github instead of the CRAN version. Note that the package did not compile on my computer
    • (III) Wait until the bug is fixed. Which should be pretty soon, as they now know about the problem.