Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/83.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在R中使用GenSA函数设置数学约束_R_Optimization_Simulated Annealing - Fatal编程技术网

如何在R中使用GenSA函数设置数学约束

如何在R中使用GenSA函数设置数学约束,r,optimization,simulated-annealing,R,Optimization,Simulated Annealing,我目前正在尝试使用模拟退火软件包GenSA,以最小化以下功能: efficientFunction <- function(v) { t(v) %*% Cov_Mat %*% v } 此外,由于我打算依赖GenSA函数,我想在约束条件下使用类似的东西: v <- c(0.25, 0.25, 0.25, 0.25) dimension <- 4 lower <- rep(0, dimension) upper <- rep(1, dimension)

我目前正在尝试使用模拟退火软件包GenSA,以最小化以下功能:

efficientFunction <- function(v) {
  t(v)  %*% Cov_Mat   %*%  v
 }
此外,由于我打算依赖GenSA函数,我想在约束条件下使用类似的东西:

v <- c(0.25, 0.25, 0.25, 0.25)
dimension <- 4
lower <- rep(0, dimension)
upper <- rep(1, dimension)

out <- GenSA(v, lower = lower, upper = upper, fn = efficientFunction)

v因此函数本身似乎没有任何可以设置的约束。但是,可以重新参数化函数以强制约束。怎么样

efficientFunction <- function(v) {
    v <- v/sum(v)
    t(v) %*% Cov_Mat %*%  v
}

一种可能的方法是使用所谓的拉格朗日乘数(参见)。例如,设置

efficientFunction <- function(v) {
  lambda <- 100
  t(v)  %*% Cov_Mat   %*%  v + lambda * abs( sum(v) - 1 )
}
efficientFunction
out <- GenSA(v, lower = lower, upper = upper, fn = efficientFunction)
out$par/sum(out$par)
efficientFunction <- function(v) {
  lambda <- 100
  t(v)  %*% Cov_Mat   %*%  v + lambda * abs( sum(v) - 1 )
}