I'm writing a genetic algorithm and I plan to move from roulette wheel selection to tournament selection, but I suspect my understanding may be flawed.
If I'm only selecting the n/2 best solutions in the population, surely I run out of population quite quickly?
My understanding of the algorithm is:
for(Member m in currentPopulation){
Member randomMember1 = random member of currentPopulation which is then removed from currentPopulation
Member randomMember2 = as above;
//Mutate and crossover
if(randomMember1.getScore() > randomMember2.getScore()){
nextGeneration.add(randomMember1);
} else {
nextGeneration.add(randomMember2);
}
}
Am I understanding this correctly?
In tournament selection the selected individuals are not removed from the population. You may select the same individuals to take part in multiple tournaments.
Having looked at your code a little closer, I see you do have another misunderstanding. You would not typically mutate/crossover all members of the tournament. Instead, you perform a tournament, with the winner of that tournament being select as an individual to undergo mutation/crossover. This means that for mutation your tournament size must be at least 2, and for crossover the size must be at least 3 with the best 2 winning (or you can perform 2 separate tournaments to choose each of the parents to crossover).
Some pseudo-code might help:
while (nextPopulation too small) {
Members tournament = randomly choose x members from currentPopulation
if(crossover){
Member parents = select best two members from tournament
Member children = crossover(parents)
nextPopulation.add(children);
} else {
Member parent = select best one member from tournament
Member child = mutate(parent)
nextPopulation.add(child);
}
}
If you're selecting n/2 individuals from your population in every generation, you will eventually reach a point where you have a population of 1. What you want to do in addition to selection is create new members for your next generation using mutation or crossover, generally on those that were victors in the tournament.
So, for each generation, you have a population of size n - you reduce this to n/2 through your selection, and then those n/2 members reproduce and/or mutate to produce roughly n/2 more members for your next generation (which, on average, will be 'fitter' than those that didn't progress from the previous generation).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With