java - 保存随机变异爬山算法的最佳解决方案 - Java
问题描述
我正在尝试为我的算法保存最佳解决方案,该算法打印出二进制问题的解决方案。我的代码保存了最好的 Fitness 结果(最小的数字),但它没有保存最好的 Scales 解决方案(二进制代码),它应该是最好的 Fitness 的 Scales 解决方案。
public static ScalesSolution RMHC(ArrayList<Double> weights, int iter) {
if(weights == null || weights.size() == 0 || iter < 1) {
return null;
}
ScalesSolution sol = new ScalesSolution(weights.size());
ScalesSolution newSol = new ScalesSolution(sol.GetSol());
double oldSolFitness = sol.ScalesFitness(weights);
double newSolFitness = 0;
for(int i = 1; i <= iter; i++) {
newSol.SmallChange();
newSolFitness = newSol.ScalesFitness(weights);
if(newSolFitness < oldSolFitness) {
oldSolFitness = newSolFitness;
sol = new ScalesSolution(newSol.GetSol());
}
else if(newSolFitness > oldSolFitness) {
newSolFitness = oldSolFitness;
sol = new ScalesSolution(newSol.GetSol());
}
oldSolFitness = newSolFitness;
System.out.println(newSol.GetSol() + "; " + newSolFitness);
}
return sol;
}
总之,我想保存找到的最佳健身的解决方案(二进制),而不是保存找到的最后一个解决方案。
如果需要更多信息,请给我留言,提前谢谢!
解决方案
推荐阅读
- xcode - Swift 4 隐藏视图并填补空白
- django - 如何在字典中保存元素倍数?
- node.js - 运行 `npm install xx`, `npm uninstall xx` 后,npm 如何知道如何更新 `package.json`
- ruby-on-rails - 将默认时间格式设为 ISO8601
- ios - 尝试读取 txt 文件时出现 TIC SSL 信任错误
- javascript - gridstack.js 根据内部内容设置 grid-stack-item 高度自动
- javascript - 如何在散点图中添加滚动条?
- java - Maven 将项目部署为 Jar - 缺少类定义
- javascript - 这个 Javascrip/节点变量/常量分配符号/语法叫什么?
- haskell - http-simple setRequestBodyLBS 背后的哲学