1

假设我们有一个成本函数:

my_func <- function(x1,x2,x3,x4,x5,x6,x7,x8,x9,x10,x11){
  rst <- (x1^2) * x2 + x2 + x3*x4*x5*x6*x7*x8*x9 + x10^3 + (1-x11-x10)
  return(rst)
}

真实示例具有相同数量的变量,更复杂的成本函数。

然后说我对每个变量都有约束:

0 <= x1 <= (1/11)
0 <= x2 <= (1/20)
0 <= x3 <= (1/5)
....
0 <= x11 <= (1/10)

我想为此找到最低成本。

我目前的解决方案是,首先为每个变量构建序列,具有给定的精度(0.001),比如 x1:

x1.seq <- seq(0, 1/11, by = 0.001)
....

然后我运行 11 个 for 循环,并尝试每种组合,以尝试找到最小值:

hold <- NULL # Preallocation of this variable does help, a bit...
for (i in 1:length(x1.seq){
  x1 <- x1.seq[i]
  for (j in 1:length(x2.seq){
    x2 <- x2.seq[j]
      ....
      hold <- c(hold, my_func(x1 = x1, ..., x11 = x11))
      ....
  }
}
min(hold)

现在你们可能都知道,鉴于准确性并非微不足道,这将永远运行。那么在 R 中有没有更快的方法呢?我知道其他方法,如偏导数或拉格朗日乘数等,但它们也很耗时(而且我的非线性问题很粗糙),我正在寻找一种 R 代码方法,如果存在的话。

4

1 回答 1

0

我将使用一些非线性优化包:

  1. nolptr https://cran.r-project.org/web/packages/nloptr/index.html
  2. NlcOptim https://cran.r-project.org/web/packages/NlcOptim/index.html
  3. optimxhttps ://cran.r-project.org/web/packages/optimx/index.html
于 2019-03-10T21:03:44.523 回答