Search code examples
roptimizationsimplex

R - nolptr - Find the 50 better solutions, not only the best one


I'm using the nerldermead() function of the nolptr package and I would like to find, for instance, the 50 most likely solutions. In this example :

 opti= function(x){x-12}
      x0=c(0)
      lower=c(0)
      upper=c(100)
solution=neldermead(x0,opti,lower,upper,control=list(maxeval = 1000,stopval = -Inf))

I will only obtain solution=12, but I would obtain this best solution and 49 other around. Is there a way to extract this information of the nerldermead() function ?

Thanks a lot !


Solution

  • The simplex is a local algorithm that won't allow you to find different local optima, but only one optimum value (being global or local). You can iterate your simplex optimisation with something like a Multi-Level Single Linkage algorithm that will find different starting points for your simplex, depending on the results of the previous simplex. Here is an example with your function:

    require(nloptr)
    
    table <- NULL
    opti <- function(x){
      res <- x-12
      table <<- rbind(table, c(x, res))
      res
      }
    
    lower <- c(0)
    upper <- c(100)
    
    local_opts <- list( "algorithm" = "NLOPT_LN_NELDERMEAD",
                        maxeval=15,
                        "xtol_abs"=1.0e-4)
    
    opts <- list("algorithm" = "NLOPT_GN_MLSL_LDS",
                 "local_opts" = local_opts,
                 maxeval=50*15,
                 print_level=3)
    
    OPT <- nloptr(
      x0 = runif(1, min=lower, max=upper), # random starting point
      eval_f=opti,
      lb = lower,
      ub = upper,
      eval_grad_f=NULL,
      opts=opts
    )     
    
    table <- table[order(table[,2]),]
    table[1:50,]
    

    As your function is simple, your 50 results are the same but with a rougher surface you may expect interesting results. To my knowledge nloptr does not allow you to get the trace of your optim path so you have to write it in your evaluation function. Here the number of iteration is very low: you have 50-random starting 15-iteration simplex, don't forget to change that.