Simple Way to Reverse the Probability Order:
Say a original probability list [p1,p2,p3,...,pn]
is for individuals in population.
To reverse the order, for each pi
in this list, we get new_pi = (1-pi) / (n-1)
.
Explanation:
Since 0<=pi<=1
, the value of (1-pi)
makes a smaller probability get a larger value.
Since the sum of (1-pi)
for i
from 1
to n
become n-1
, after dividing by (n-1)
(normalization), we make sure 0<=new_pi<=1
.
Code Sample:
def rouletteWheelSelect(population):
fitnessSum = 0
for individual in population:
fitnessSum += individual.fitness
for individual in population:
individual.selectProb = individual.fitness / fitnessSum
probSum = 0
wheelProbList = []
if MAXIMIZATION_PROBLEM:
for individual in population:
probSum += individual.selectProb
wheelProbList.append(probSum)
elif MINIMIZATION_PROBLEM:
for individual in population:
probSum += (1-individual.selectProb) / (POPULATION_SIZE-1)
wheelProbList.append(probSum)
r = random.random() # 0<=r<1
for i, p in enumerate(wheelProbList):
if r < p:
return population[i]
return None