So I've written a roulette selection function for my genetic algorithm which follows:
public String tournament(float fitness, Chromosome pop[], int selection)
{
// roulette
if (selection == 1)
{
Random random = new Random();
float slice = random.nextFloat() * fitness;
float curFitness = 0.0f;
for (int i = 0; i < initialPopulation; i++)
{
curFitness += pop[i].fitness;
if (curFitness >= slice)
return pop[i].bits;
}
}
return "";
}
The problem is that it is sometimes returning the blank string, which has only been placed their to satisfy the return condition. This is generally not a problem, but during some runs it causes the GA to terminate since the next step involves the crossover stage. Any ideas?