0

I have a fitness function as part of a lab and wish to apply it to a set of 'weights' (ArrayList weights). I have created the array and stored some values in it. I have created random binary strings (which have an 'x' at the end in order to generate random values) which I wish to also apply the fitness function to; however, the problem I am having is that the fitness function always returns a value of 0. Am I missing something here?

The fitness function is as follows:

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(scasol.charAt(i) == '0'){
        L = L += 0;
    }
    else if(scasol.charAt(i) == '1'){
        R = R += 1;
    }
    }//end for

    int n = scasol.length();

    return(L-R);

}

Random binary string method:

private static String RandomBinaryString(int n){
    String s = new String();

    for(int i = 0; i <= n; i++){
        int y = CS2004.UI(0,1);
            if(y == 0){
                System.out.print(s + '0');
            }
            else if(y == 1){
                System.out.print(s + '1');
            }
    }

    return(s);
}

Main method (in separate class):

public static void main(String args[]){

    for(int i = 0; i < 10; i++){
        ScalesSolution s = new ScalesSolution("10101x");
        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(10.0);
        System.out.println();

    System.out.print("Fitness: ");
    System.out.print(ScalesSolution.scalesFitness(weights));
}

Once run, the random binary strings work perfectly well, yet the fitness function fails to change from 0. Here is a sample output:

1101100
1100111
0001111
1001010
1110000
0011111
1100111
1011001
0000110
1000000

Fitness: 0.0

If you wish to code for the whole class(es), then please let me know.

Thank you all so much for your time.

Mick.

Mus
  • 7,290
  • 24
  • 86
  • 130

3 Answers3

2

Looks to me like you're always returning a blank String from RandomBinaryString() - you print out some digits but never actually append them. Use s = s+'0', or s += '0', or s.concat("0") ,or use a StringBuilder , etc...

I'm assuming scasol is your binary string, so that's empty, then nothing in your for loop gets called once, so L and R both stay at 0.0, and you wind up returning 0.0.

DHall
  • 393
  • 2
  • 13
  • Thank you, I tried this but it doesn't seem to work. Yes, scasol is my binary String. I have noticed that if I change the following to < from >, it produces -1.0 every time instead of 0.0. `public static double scalesFitness(ArrayList weights) { if (scasol.length() > weights.size()) return(-1);` Could this be affecting it somehow? – Mus Mar 14 '11 at 20:11
  • @Mick - getting -1 in this case makes sense if scasol is a blank String. Can you post the code for the ScalesSolution constructor? – DHall Mar 14 '11 at 20:15
  • I just realized `s.concat("0")` by itself won't do anything. Make sure you're assigning the value to s with either `s=s+'0'` or `s+='0'` or `s=s.concat("0")`. The rest of the code should work, also make sure you have the same number of numbers in the Weights list as characters in the seed String. – DHall Mar 14 '11 at 20:43
  • I tried all of your suggestions but unfortunately they did not seem to work - there must be something else responsible for the lack of function...thank you once again for your effort, I appreciate it; unfortunately, however, I am still nonethewiser as to what it could be...do you happen to have any other suggestions? – Mus Mar 14 '11 at 22:35
0

Your random string RandomBinaryString only ever prints 's' it never alters it so the sum of the function is equivalent to returning 'new String()'.

Another issue, 'L = L += 0' is redundant. It is the same as L = 0. Always.

'R = R+=1' is also redundant, it is the same as R += 1.

Matthew Sowders
  • 1,640
  • 1
  • 19
  • 32
0

@DHall Code for scasol constructor:

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);
    }
}

If this is not of help I can post the code for the class up.

Thanks.

Mick.

Mus
  • 7,290
  • 24
  • 86
  • 130