Java 爬山码-随机变异

Java 爬山码-随机变异,java,hill-climbing,Java,Hill Climbing,这段代码应该比较两个适合度,使用最好的一个来找到解决方案,然后在下一次迭代中使用最好的一个。然而,我遇到的问题是,它只是使用最新的健身,不管它是大还是小。如果我的代码有任何错误,谁能帮我发现,谢谢 这是一个有点棘手的解释,所以如果有人需要更多的澄清,请询问,我将发布我的整个项目,尽管我相信错误与这一小部分代码有关: public static ScalesSolution RMHC(ArrayList<Double> weights, int n, int iter) { S

这段代码应该比较两个适合度,使用最好的一个来找到解决方案,然后在下一次迭代中使用最好的一个。然而,我遇到的问题是,它只是使用最新的健身,不管它是大还是小。如果我的代码有任何错误,谁能帮我发现,谢谢

这是一个有点棘手的解释,所以如果有人需要更多的澄清,请询问,我将发布我的整个项目,尽管我相信错误与这一小部分代码有关:

public static ScalesSolution RMHC(ArrayList<Double> weights, int n, int iter) {
    ScalesSolution sol = new ScalesSolution(n);
    ScalesSolution oldSol = new ScalesSolution(sol.GetSol());
    for (int i = 0; i < iter; i++) {
        System.out.println("Iteration number: " + i);
        System.out.println("Old Solution : ");
        oldSol.println();
        double f = oldSol.ScalesFitness(weights);
        System.out.println("Old Fitness: ");
        System.out.println(f);
        // the new solution after copying the string from scalesolution
        sol.SmallChange();
        System.out.println("New Solution : ");
        sol.println();
        double f1 = sol.ScalesFitness(weights);
        System.out.println("New Fitness: ");
        System.out.println(f1);
        if (oldSol.ScalesFitness(weights) > sol.ScalesFitness(weights)) {               
            oldSol = new ScalesSolution(sol.GetSol());
        }
    }
    return (oldSol);
}
以下是ScaleFitness和ScaleSolution:

public ScalesSolution(int n) {
    scasol = RandomBinaryString(n);
}

// This is the fitness function for the Scales problem
// This function returns -1 if the number of weights is less than the size of the current solution 
// Exercise 3

public static double ScalesFitness(ArrayList<Double> weights) {
    int n = scasol.length();                // Assigns the length of scasol to n
    double lhs = 0.0;                       //  Initialises lhs to 0.0, type double
    double rhs = 0.0;                       //  Initialises rhs to 0.0, type double
    if (n > weights.size())                 // If statement, compares n and weight size
        return (-1);                        // Returns -1 when the if statement is true

    // Code goes here
    for (int i = 0; i < n; i++) {           // For loop which goes from i=0 to n
        if (scasol.charAt(i) == '0') {      // If statement which checks if the character at position i is equal to a 0
            lhs += weights.get(i);          // Adds weight at position i to lhs
        } else {                            // If the character in position i is not a 0 do the following
            rhs += weights.get(i);          // Adds the weight at position i to rhs
        }
    }
    return (Math.abs(lhs - rhs));           // Calculates the absolute value of lhs-rhs and returns the value
}
公共规模解决方案(int n){
scasol=RandomBinaryString(n);
}
//这是尺度问题的适应度函数
//如果权重数小于当前解决方案的大小,则此函数返回-1
//练习3
公共静态双尺度适应度(ArrayList权重){
int n=scasol.length();//将scasol的长度指定给n
double lhs=0.0;//将lhs初始化为0.0,键入double
double rhs=0.0;//将rhs初始化为0.0,键入double
if(n>weights.size())//if语句,比较n和weight size
return(-1);//当if语句为true时返回-1
//代码在这里
for(int i=0;i
但这完全取决于sol.SmallChange()对吗?这是一个单调递增的函数吗?@sundar
SmalChange
甚至不是一个函数——返回值被忽略。@MarkoTopolnik但这可能会改变
sol
对象的状态,这可能会影响
ScalesFitness
结果对吗?对,因此所讨论的函数是
ScalesFitness
,但是“单调递增”只是一元函数的一个属性,因此你的问题在适应度最大化的世界中毫无意义。@JimmyK你能分享ScalesFitness方法和相应的ScalesSolution构造函数吗?
public ScalesSolution(int n) {
    scasol = RandomBinaryString(n);
}

// This is the fitness function for the Scales problem
// This function returns -1 if the number of weights is less than the size of the current solution 
// Exercise 3

public static double ScalesFitness(ArrayList<Double> weights) {
    int n = scasol.length();                // Assigns the length of scasol to n
    double lhs = 0.0;                       //  Initialises lhs to 0.0, type double
    double rhs = 0.0;                       //  Initialises rhs to 0.0, type double
    if (n > weights.size())                 // If statement, compares n and weight size
        return (-1);                        // Returns -1 when the if statement is true

    // Code goes here
    for (int i = 0; i < n; i++) {           // For loop which goes from i=0 to n
        if (scasol.charAt(i) == '0') {      // If statement which checks if the character at position i is equal to a 0
            lhs += weights.get(i);          // Adds weight at position i to lhs
        } else {                            // If the character in position i is not a 0 do the following
            rhs += weights.get(i);          // Adds the weight at position i to rhs
        }
    }
    return (Math.abs(lhs - rhs));           // Calculates the absolute value of lhs-rhs and returns the value
}