Question about fitness in genetic algorithms
in
Programming Questions
•
1 year ago
Hi all, I'm working through Daniel Shiffman's fantastic GA examples (
www.shiffman.net/teaching/nature/ga) and am trying to do some modifications to his 'GAShakespeare' example. I understand the basics of how the GA model works, but am confused about some specific details of the code when it comes to measuring the 'fitness' of the population.
In the Population class, a 'perfectscore' (the stopping point) is defined as:
In the Population class, a 'perfectscore' (the stopping point) is defined as:
- perfectScore = pow(2, target.length());
And the 'fitness' in the DNA class at a particular moment is similarly defined as:
- fitness = pow(2, score);
Does anyone know why it would be 2^nth? When using a longer string however (>20 characters), this results in numbers way too high and I had to change to a long instead. I tried changing the code to be just:
- perfectScore = target.length();
- fitness = score;
This works, but is
much slower. I have no idea why, since it isn't changing the underlying crossover procedures. Thoughts appreciated!
1