如何在R中使用GenSA函数设置数学约束
我目前正在尝试使用模拟退火软件包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)
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 )
}