How to put mathematical constraints with GenSA function in R

前端 未结 2 979
遥遥无期
遥遥无期 2020-12-21 08:34

I am currently trying to use Simulated Annealing package GenSA in order to minimize the function below :

efficientFunction <- function(v) {
  t(v)  %*% Co         


        
相关标签:
2条回答
  • 2020-12-21 09:11

    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)
    
    0 讨论(0)
  • 2020-12-21 09:32

    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.

    0 讨论(0)
提交回复
热议问题