0

Hello,

I'm trying to code a genetic algorithm in java but my code doesn't seem to be working as it should.

I think the problem lies within my roulette wheel selection method.

Can anyone spot it/point me in the right direction on how to solve it?

```
private static Individual[] rouletteWheelSelection2(Individual[] aPopulation) {
Random random = new Random();
Individual[] populationNew = new Individual[popSize];
int totalFitness = 0;
//sum the total fitness of the population
for (int i = 0; i < popSize; i++) {
Individual currentIndividual = aPopulation[i];
totalFitness += currentIndividual.fitnessVal;
}
/* sum the fitness of each individual in the population again
* until the running sum is >= to the randomly chosen number.
*/
for (int i = 0; i < popSize; i++) {
//pick a random number between 0 and that sum.
int randomNumber = random.nextInt(totalFitness + 1);
int runningSum = 0;
int index = 0;
int lastAddedIndex = 0;
while (runningSum < randomNumber) {
runningSum += aPopulation[index].fitnessVal;
lastAddedIndex = index;
index++;
}
populationNew[i] = aPopulation[lastAddedIndex];
runningSum = 0;
index = 0;
lastAddedIndex = 0;
}
return populationNew;
}
```