使用遗传算法时,如何使权重总和(对于我投资组合中的股票)等于 1?

How can I make the sum of the weights (for the stocks in my portfolio) be equal to 1 while using a genetic algorithm?

我是 运行 R 中的遗传算法,对投资组合中股票的 select 权重,同时具有最高 return/risk 比率。问题是权重总和需要为 1,但到目前为止我尝试过的代码不起作用。任何帮助将不胜感激。

这是我的代码:

normalise=function(v){v/sum(v)}
f=function(weights){
  weights=normalise(weights)
  (weights%*%returns)/(weights%*%variances)

}
GA=ga(type="real",fitness=f,lower=rep(0,length(positive)),
      upper=rep(1,length(positive)),maxiter=20000,run=300)

那是因为您实际上 "repair" 没有解决方案:您只是在评估之前将其映射到可行的解决方案。这是你实际可以做的事情;但是你也必须映射 GA 的解决方案,即规范化 GA returns 的权重。 (例如参见 [​​=10=]。)