0

I am having trouble getting the fitness function to work on my code. All that I end up with is the binary String "10101".

I would greatly appreciate it if somebody could assist me as I have spent an extremely long time on this and not gotten anywhere.

Here is the code:

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

    }
}

The fitness function is as follows:

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

When I run the program, the output is as follows:

10101

There is nothing more, no matter what I enter for values of the ArrayList...

I have been racking my brain for hours and am still nonethewiser - any help would be greatly appreciated!

Thank you so much.

Mick.

EDIT: The code has now been updated and the complete class is listed - apologies for any confusion.

Mus
  • 7,290
  • 24
  • 86
  • 130
  • 1
    I don't thin kthis is all the code as the only output is two empty println and the result of scalesFitness is not used – mmmmmm Mar 14 '11 at 11:39
  • This code won't compile. – PeterMmm Mar 14 '11 at 11:41
  • Yes, there are a few gaps at the code: i.e. the ScalesSolution constructor which accepts a String, the ScalesSolution.println() method... – Tomas Narros Mar 14 '11 at 11:43
  • I suggest you cut down your code to something you can compile and then debug with the debugger. Your RandomBinaryString appears to be randomly generated code, it doesn't do anything like what it suggests. ;) – Peter Lawrey Mar 14 '11 at 11:45
  • @Mick: I have to admit it was clearer at your prior "shortened" version. – Tomas Narros Mar 14 '11 at 11:48
  • Can you please describe what these methods are *supposed* to do and we can see what is wrong, if the printing still doesn't look right? – I82Much Mar 14 '11 at 11:52
  • Your code is wrong on every level. I brought your prelim version into IntelliJ and looked at it. It could never, ever compile, let alone run. The updates you've posted don't help the situation. I can see why you're having trouble understanding the issue; your code expresses your confusion. I don't see much hope if you can't summarize what you're trying to do without code. – duffymo Mar 14 '11 at 11:57

4 Answers4

2

I don't know exactly how this code is supposed to work, but one piece that looks suspicious to me is the following:

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

You never examine any of the values in scasol; should you be? i.e. something like

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

The way you have written now, you're comparing the iteration index rather than the value at that index.

The other problem as others mentioned is you don't print the result:

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

should be:

 double fitness = ScalesSolution.scalesFitness(weights);
System.out.println(fitness);
I82Much
  • 26,901
  • 13
  • 88
  • 119
  • Thank you so much; however, whenever I try this I receive the error message "The method get(int) is undefined for the type String". – Mus Mar 14 '11 at 11:41
  • @Mick updated, sorry thought it was an arraylist not a string. – I82Much Mar 14 '11 at 11:54
  • This is fantastic,thank you! Whenever I run the program I have found that the output calculates how many 1s and 0s there are and produces a result between -5 (where all values=1) and 0 (where all values=0). As you said, it depends whether the other algorithm is right. I have entered the algorithm from the following specs: "For each possible place in the string (n of them) you generate CS2004.UI(0,1). This will randomly generate a zero or one. If you get a zero add the character '0' to the end of the string, otherwise add a '1'. You will need to use a For loop." What are your thoughts? – Mus Mar 14 '11 at 12:06
1

I think your problem is at the empty System.out.println();. I would try the next:

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

You need to store your returned value at a variable, and then pass this variable to the methods where you want to use the value (in this case, printing it to the console statndard output via println method).

Tomas Narros
  • 13,390
  • 2
  • 40
  • 56
  • 1
    At least with respect to why he always gets same output. Whether the other algorithm is right is another issue – I82Much Mar 14 '11 at 11:54
0

is that code complete??

at the beginning you call

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

and I can't find the constructor or the method s.prinln();

but it seems like you print your Input String - and not any binary thing

k-deux
  • 493
  • 3
  • 13
0

You are nowhere using or even printing the result of your ScalesSolution.scalesFitness(weights); call, so no wonder it would not print anything.

Paŭlo Ebermann
  • 73,284
  • 20
  • 146
  • 210