How to put mathematical constraints with GenSA function in R

夙愿已清 提交于 2019-11-29 15:41:38

A possible approach would be to make use of so-called Lagrange multipliers (cf., http://en.wikipedia.org/wiki/Lagrange_multiplier). For example, set

efficientFunction <- function(v) {
  lambda <- 100
  t(v)  %*% Cov_Mat   %*%  v + lambda * abs( sum(v) - 1 )
}

, so that in order to minimize the objective function efficientFunction the resulting parameter also minimize the penalty term lambda * abs( sum(v) - 1 ). The Lagrange multiplier lambda is set to an arbitrary but sufficiently high level.

So the function itself doesn't appear to have any constraints that you can set. However, you can reparameterize your function to force the constraint. How about

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

Here we normalize the values of v so that they will sum to 1. Then, when we get the output parameters, we need to perform the same transformation

out <- GenSA(v, lower = lower, upper = upper, fn = efficientFunction)
out$par/sum(out$par)
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!