我使用球体基准函数来测试进化策略代码,其中不相关的一步大小突变如下:
public class cromosome {
private double[] variable = new double[2];
private double[] stepSize = new double[2];
private double fitness=0;
}
//=========================method set fitness=========================================
public void setFitness() {
for (int i=0; i<variable.length; i++) {
fitness += variable[i]*variable[i];
}
System.out.println("fitness= " + fitness);
}
应用突变步骤后,我的值在范围内[-10;+10],似乎我的适合度超出了范围。这是我的突变方法
public static cromosome Mutation(cromosome cro) {
//Mutations with no correlation with one step size
Random r = new Random();
double a = r.nextGaussian();
double lr = 1 / (Math.sqrt(cro.getVariableLenght()));
double[] newMutationStep = new double[1];
newMutationStep[0] = cro.getMutationStep(0) * (Math.exp(lr * a));
double[] newVariable = new double[2];
for (int i = 0; i < cro.getVariableLenght(); i++) {
double b = r.nextGaussian();
newVariable[i] = cro.getVariable(i) + newMutationStep[0] * b;
}
cromosome newKromosom = new cromosome(newVariable[0], newVariable[1], newMutationStep[0]);
return newKromosom;
}
多次迭代后,适应度值超出范围,如何控制我的适应度?
您正在使用 r.nextGaussian((。此变量的点差大于 1。这意味着您有时会得到低于 -1 或高于 1 的值。这可能是问题的根源。要修复它,请添加验证,例如:
double a = r.nextGaussian();
if(a < -1){a = -1;}else if(a > 1){a = 1;}
在此之后,以与现在相同的方式处理它。