(start method body)
double totalfitness = 0;
double currentfitness;
for(int x = 0 ; x < m_PopulationSize ; x++) {
currentfitness = findFitness(m_CurrentPopulation[x]);
m_CurrentPopulation[x].setFitness(currentfitness);
totalfitness += currentfitness;
}
double fitness = 0;
for(int x = 0 ; x < m_PopulationSize ; x++) {
fitness += m_CurrentPopulation[x].getFitness();
}
(end method body)
[edited by - Argus on July 22, 2002 3:42:03 AM]
[edited by - Argus on July 22, 2002 4:46:21 AM]
[edited by - Argus on July 22, 2002 8:46:41 AM]
🔒 Locked
[java] coding problem
Advertisement
Original Post
Ok normally I keep at a problem until I crack it but I have a headache, I'm hungry, and this problem is very much unsolved. I have narrowed it down to the following, anyone who can tell me what's going wrong will have my eternal gratitude :
Now the problem is that 'fitness' after the test loop at the end is different to 'totalfitness', calculated in the first loop. 'm_PopulationSize' is 100 and the fitness function findFitness() returns between 0 and 100 inclusive. I've seen totalfitness greater than fitness by anywhere between 40 and 250 or so.
quote:Should be i, not x
Original post by Argus
double fitness = 0;
for(int i = 0 ; i < m_PopulationSize ; i++) {
fitness += m_CurrentPopulation[x].getFitness();
}
getFitness() and setFitness(.) are fine. This problem is unbelieveably bad. I used to tutor java at my university and I still can''t work out what the hell is going on.
From what I can see there are two loops. The first one goes through all the elements, obtaining a number for each one, storing that number in the element, and also adding the number to a total.
The second loop goes through all the elements and gets the number out of them and adds that to a separate total.
The two totals are different.
I mean wtf..
From what I can see there are two loops. The first one goes through all the elements, obtaining a number for each one, storing that number in the element, and also adding the number to a total.
The second loop goes through all the elements and gets the number out of them and adds that to a separate total.
The two totals are different.
I mean wtf..
Thats why i thought that either set- or getFitness() would be wrong. Or maybe it's a rounding error somewhere in them (so that 20.6 will be stored as 21 for example).
Edit: what happens, if you write
totalfitness += m_CurrentPopulation[x].getFitness();
instead of
totalfitness += currentfitness;
in the first loop?
[edited by - EgonOlsen on July 22, 2002 8:40:58 AM]
Edit: what happens, if you write
totalfitness += m_CurrentPopulation[x].getFitness();
instead of
totalfitness += currentfitness;
in the first loop?
[edited by - EgonOlsen on July 22, 2002 8:40:58 AM]
Nope they're fine. I store them as doubles and use them as doubles so they should be alright.
Some more information : I checked the info from the first loop against the second loop. After this method has been called a few times (and I have no idea why this makes a difference, since I'm not using any other threads), the numbers start looking more and more different.
In one instance I compared the second loop's numbers with the first loop's numbers, and the second loop had some numbers the same, but it had 59 of 100 numbers as either 70 or 80, while the first loop had only 9..
Also I have altered the code in my initial post to get rid of stuff that doesn't change the outcome.
EDIT : yup and I checked that "totalfitness += m_CurrentPopulation[x].getFitness()" pretty early - it makes no difference to the outcome.
[edited by - Argus on July 22, 2002 8:49:55 AM]
Some more information : I checked the info from the first loop against the second loop. After this method has been called a few times (and I have no idea why this makes a difference, since I'm not using any other threads), the numbers start looking more and more different.
In one instance I compared the second loop's numbers with the first loop's numbers, and the second loop had some numbers the same, but it had 59 of 100 numbers as either 70 or 80, while the first loop had only 9..
Also I have altered the code in my initial post to get rid of stuff that doesn't change the outcome.
EDIT : yup and I checked that "totalfitness += m_CurrentPopulation[x].getFitness()" pretty early - it makes no difference to the outcome.
[edited by - Argus on July 22, 2002 8:49:55 AM]
Maybe the objects in your array are not correct? Maybe some entry x holds the same object as entry y? I believe that either your object or your array is wrong.
Edit: But then again, the change i suggested above should show some effect, which it doesn't seem to...strange...
[edited by - EgonOlsen on July 22, 2002 9:00:05 AM]
Edit: But then again, the change i suggested above should show some effect, which it doesn't seem to...strange...
[edited by - EgonOlsen on July 22, 2002 9:00:05 AM]
Heh I finally figured it out at 3am. Pretty much a noob error - Egon you were right, some array positions were pointing to the same object. Thus when it went through the loop and set the fitness on say position 80, then it also set the previous positions'' fitness. But because it had already added the previous positions'' fitness to the total, it was only in the second loop that the altered fitnesses were added in place of the real ones..
Thanks for your help!
Thanks for your help!
Topic Locked
This topic has been locked by a moderator. New replies are not allowed.
Sign in to reply to this topic.
Advertisement