R - nolptr - 找到 50 个更好的解决方案,而不仅仅是最好的一个

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

我正在使用 nolptr 包的 nerldermead() 函数,我想找到 50 个最有可能的解决方案。在这个例子中:

 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))

我只会得到solution=12,但我会得到这个最佳解和其他49个。有没有办法提取 nerldermead() 函数的信息?

非常感谢!

单纯形法是一种局部算法,不允许您找到不同的局部最优值,但只能找到一个最优值(全局或局部)。您可以使用诸如多级单链接算法之类的东西来迭代您的单纯形优化,该算法会根据先前单纯形的结果为您的单纯形找到不同的起点。这是您的函数的示例:

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,]

由于您的函数很简单,所以您的 50 个结果是相同的,但如果表面较粗糙,您可能会得到有趣的结果。据我所知, nloptr 不允许您获得最佳路径的踪迹,因此您必须将其写在您的评估函数中。这里的迭代次数非常低:您有 50 个随机起始的 15 次迭代单纯形,不要忘记更改它。