Reputation: 1287
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?
Upvotes: 8
Views: 14795
Reputation: 12022
Tournament Selection:
PseudoCode:
choose k (the tournament size) individuals from the population at random
choose the best individual from pool/tournament with probability p
choose the second best individual with probability p*(1-p)
choose the third best individual with probability p*((1-p)^2)
and so on...
Deterministic tournament selection selects the best individual (when p = 1) in any tournament. A 1-way tournament (k = 1) selection is equivalent to random selection. The chosen individual can be removed from the population that the selection is made from if desired, otherwise individuals can be selected more than once for the next generation. In comparison with the (stochastic) fitness proportionate selection method, tournament selection is often implemented in practice due to its lack of stochastic noise.
Tournament Selection in MatLab:
Matepool=randi(PopLength,PopLength,2);%%select two individuals randomly for tournament and chooose the one with best fitness value
%% number of tournament is equal to the number of population size
for i=1:PopLength
if Fitness(Matepool(i,1))>= Fitness(Matepool(i,2))
SelectedPop(i,1:IndLength)=CurrentPop(Matepool(i,1),1:IndLength);
else
SelectedPop(i,1:IndLength)=CurrentPop(Matepool(i,2),1:IndLength);
end
end
Upvotes: -1
Reputation: 2202
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);
}
}
Upvotes: 9
Reputation: 38345
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).
Upvotes: 1