# Java – Genetic Algorithm Tournament Selection

evolutionary-algorithmgenetic-algorithmjava

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()){
} else {
}
}
``````

Am I understanding this correctly?

#### Best Solution

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)