Java健身函数

标签 java function fitness

我在让适应度函数在我的代码上运行时遇到问题。我最终得到的是二进制字符串“10101”。

如果有人可以帮助我,我将不胜感激,因为我在这方面花了很长时间但没有取得任何进展。

这是代码:

public class Fitness{

public static void main(String args[]){

    ScalesSolution s = new ScalesSolution("10101");
    s.println();

    ArrayList<Double> weights = new ArrayList<Double>();

        weights.add(1.0);
        weights.add(2.0);
        weights.add(3.0);
        weights.add(4.0);
        weights.add(5.0);
        weights.add(6.0);
        weights.add(7.0);
        weights.add(17.0);
        weights.add(117.0);
        weights.add(3427.0);
        weights.add(5437.0);
        weights.add(567.0);
        weights.add(7567.0);

    ScalesSolution.scalesFitness(weights);
    System.out.println();

    }
}

适应度函数如下:

public class ScalesSolution{

    private static String scasol;
//Creates a new scales solution based on a string parameter
//The string parameter is checked to see if it contains all zeros and ones
//Otherwise the random binary string generator is used (n = length of parameter)
public ScalesSolution(String s)
{
    boolean ok = true;
    int n = s.length();
    for(int i=0;i<n;++i)
    {
        char si = s.charAt(i);
        if (si != '0' && si != '1') ok = false;
    }
    if (ok)
    {
        scasol = s;
    }
    else
    {
        scasol = RandomBinaryString(n);
    }
}
private static String RandomBinaryString(int n)
{
    String s = new String();

    for(int i = 0; i < s.length(); i++){
        CS2004.UI(0,1);
            if(i == 0){
                System.out.println(s + "0");
            }
            else if(i == 1){
                System.out.println(s + "1");
            }
    }

    return(s);
}
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



public static double scalesFitness(ArrayList<Double> weights)
{   
    if (scasol.length() > weights.size()) return(-1);
    double lhs = 0.0,rhs = 0.0;

    double L = 0.0;
    double R = 0.0;

    for(int i = 0; i < scasol.length(); i++){
        if(i == 0){
            L = L += i;
        }
        if(i == 1){
            R = R += i;
        }
    }//end for

    int n = scasol.length();

    return(L-R);

    //return(Math.abs(lhs-rhs));

}
//Display the string without a new line
public void print()
{
    System.out.print(scasol);
}
//Display the string with a new line
public void println()
{
    print();
    System.out.println();
}
}

当我运行程序时,输出如下:

10101

无论我为 ArrayList 的值输入什么,都没有更多内容...

我已经绞尽脑汁几个小时了,但仍然不明白 - 任何帮助将不胜感激!

非常感谢。

米克。

编辑:代码现已更新,并列出了完整的类 - 对于任何混淆表示歉意。

最佳答案

我不知道这段代码到底是如何工作的,但我觉得可疑的部分如下:

for(int i = 0; i < scasol.length(); i++){
        if(i == 0){
        L = L += i;
    }
    if(i == 1){
        R = R += i;
    }
    }//end for

您从不检查 scasol 中的任何值;你应该是吗?即类似于

for(int i = 0; i < scasol.length(); i++){
            if(scasol.getCharAt(i) == '0'){
            L = L += 0;
        }
        else if(scasol.getCharAt(i) == '1'){
            R = R += 1;
        }
        }//end for

按照您现在编写的方式,您正在比较迭代索引而不是该索引处的值。

其他人提到的另一个问题是您不打印结果:

ScalesSolution.scalesFitness(weights);
System.out.println();

应该是:

 double fitness = ScalesSolution.scalesFitness(weights);
System.out.println(fitness);

关于Java健身函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5298001/

相关文章:

java - 如何通过 Hapi 获取 HL7 v2x 消息的段、组和重复次数列表

java - Install4j: 安装程序:sys.installationDir 不一致

c - 指向结构和函数的指针

javascript - 空白函数产生 "object expected"错误

javascript - onClick ="javascript: function(' 值')'"和onClick ="function(' 值');”有什么区别?

Java Stream_Don't apply filter if my filter string is null

java正则表达式,从字符串中获取时间

android - 在没有 Samsung Cloud 帐户的情况下迁移 Samsung Health 数据

algorithm - 某些适应度为0时的适应度比例选择