I am implementing a roulette wheel selection method for a genetic algorithm. My question, in essence, is pretty simple but I can't wrap my mind around it. In my fitness function, if an answer is extremely wrong it may return around -3000%. My problem is, when I attempt to assign probabilities for my results, they get skewed toward the wrong answers.
For example: If my percentages are in an array and are [92, 68, 5, -4, -3546] (from high to low) I need to give the numbers in the lower indices a greater chance of being selected than the numbers with higher indices.
Ignoring my fitness function, how do I create a probability based on this taking into account large negative numbers?
Some basic code I've tinkered with I found in another question:
public Individual rouletteWheelSelection() {
double randNum = m_rand.nextDouble() * this.totalFitness;
int idx;
for (idx=0; idx<POP_SIZE && randNum>0; ++idx) {
randNum -= m_population[idx].getFitnessValue();
}
return m_population[idx-1];
}
(original link here: GA written in Java)
I had my GA working for a different selection method, but now I'm trying to modify this one to work instead. Any help would be greatly appreciated.
***Edit
The following code is my rouletteWheelSelection I've modified:
private Chromosome rouletteWheelSelection(){
double randNum = Math.abs(rand_num.nextDouble() * totalFitness);
int idx;
for (idx=0;idx<NUM_CHROMOSOMES && randNum>0;++idx){
randNum -= Math.abs(population[idx].getFitness());
}
return population[NUM_CHROMOSOMES-idx];
}
Here is my fitness function:
public double getFitness()
{
String working = bitString;
int x1 = Integer.parseInt(working.substring(0,6),2);
int x2 = Integer.parseInt(working.substring(6),2);
double result = ScratchGA.functionTest(x1,x2);
double percentAccuracy = (1- Math.abs(((ScratchGA.getDesired() - result)/ScratchGA.getDesired())))*100;
if (percentAccuracy <= 100)
{
return percentAccuracy;
}
else
{
return -percentAccuracy;
}
}
The thought was that is a value was more than 100% different from what I needed, I made it negative to shove to the end of my sorted list.