首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Java Fitness函数

Java Fitness函数
EN

Stack Overflow用户
提问于 2011-03-14 19:34:45
回答 4查看 2.7K关注 0票数 0

我在使用我的代码时遇到了问题。我最终得到的只是二进制字符串"10101“。

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

代码如下:

代码语言:javascript
复制
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();

    }
}

适应度函数如下:

代码语言:javascript
复制
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();
}
}

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

代码语言:javascript
复制
10101

无论我在ArrayList的值中输入什么,都没有更多的东西...

我已经绞尽脑汁思考了几个小时了,但我仍然不知道--任何帮助都将不胜感激!

非常感谢。

米克。

编辑:代码现已更新,并列出了完整的类-如有任何混淆,敬请谅解。

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2011-03-14 19:39:22

我不知道这段代码到底是如何工作的,但我觉得有一点值得怀疑:

代码语言:javascript
复制
for(int i = 0; i < scasol.length(); i++){
        if(i == 0){
        L = L += i;
    }
    if(i == 1){
        R = R += i;
    }
    }//end for

您从未检查过scasol中的任何值;您应该这样做吗?也就是说,类似于

代码语言:javascript
复制
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

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

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

代码语言:javascript
复制
ScalesSolution.scalesFitness(weights);
System.out.println();

应该是:

代码语言:javascript
复制
 double fitness = ScalesSolution.scalesFitness(weights);
System.out.println(fitness);
票数 2
EN

Stack Overflow用户

发布于 2011-03-14 19:42:13

我想你的问题出在空的System.out.println();上。我会尝试下一个:

代码语言:javascript
复制
double returnedValue= ScalesSolution.scalesFitness(weights);
System.out.println(returnedValue);

您需要将返回值存储在一个变量中,然后将此变量传递给您希望使用该值的方法(在本例中,通过println方法将其打印到控制台状态输出)。

票数 1
EN

Stack Overflow用户

发布于 2011-03-14 19:39:47

代码是否完整??

开始时,您可以调用

代码语言:javascript
复制
ScalesSolution s = new ScalesSolution("10101");
s.println();

而且我找不到构造函数或s.prinln()方法;

但它看起来像是打印您的输入字符串-而不是任何二进制内容

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/5298001

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档