首页 > 解决方案 > 保存随机变异爬山算法的最佳解决方案 - 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;                                                     

}

总之,我想保存找到的最佳健身的解决方案(二进制),而不是保存找到的最后一个解决方案。

如果需要更多信息,请给我留言,提前谢谢!

标签: javaalgorithm

解决方案


推荐阅读