J. *_*ang 5 r genetic-algorithm
我正在 R(库 mco)上开发 NSGA2 包。
我的 NSGA2 代码需要永远运行,所以我想知道:
1)有没有办法限制解值的精度(例如,最多3个小数位)而不是无限?
2)如何设置等式约束(网上的似乎都是>=或<=比=)?不确定我做得是否正确。
我的整个相关代码供参考,以便于跟踪:https://docs.google.com/document/d/1xj7OPng11EzLTTtWLdRWMm8zJ9f7q1wsx2nIHdh3RM4/edit ?usp=sharing
此处复制的相关示例代码部分:
VTR = get.hist.quote(instrument = 'VTR',
start="2010-01-01", end = "2015-12-31",
quote = c("AdjClose"),provider = "yahoo",
compress = "d")
ObjFun1 <- function (xh){
f1 <- sum(HSVaR_P(merge(VTR, CMI, SPLS, KSS, DVN, MAT, LOE, KEL, COH, AXP), xh, 0.05, 2))
tempt = merge(VTR, CMI, SPLS, KSS, DVN, MAT, LOE, KEL, COH, AXP)
tempt2 = tempt[(nrow(tempt)-(2*N)):nrow(tempt),]
for (i in 1:nrow(tempt2))
{
for (j in 1:ncol(tempt2))
{
if (is.na(tempt2[i,j]))
{
tempt2[i,j] = 0
}
}
}
f2 <- ((-1)*abs(sum((xh*t(tempt2)))))
c(f1=f1,f2=f2)
}
Constr <- function(xh){
totwt <- (1-sum(-xh))
totwt2 <- (sum(xh)-1)
c(totwt,totwt2)
}
Solution1 <- nsga2(ObjFun1, n.projects, 2,
lower.bounds=rep(0,n.projects), upper.bounds=rep(1,n.projects),
popsize=n.solutions, constraints = Constr, cdim=1,
generations=generations)
Run Code Online (Sandbox Code Playgroud)
该函数HSVaR_P返回矩阵(x,2*500,1)。
即使我设置 Generation = 1,代码似乎也无法运行。显然代码中应该有一些错误,但我不完全确定 NSGA2 算法的机制。
谢谢。
| 归档时间: |
|
| 查看次数: |
698 次 |
| 最近记录: |