In a genetic algorithm I have a list indis
(population) of candidate solutions represented by objects of type indi
. To make the selection straight forward, I want to sort these individuals by a fitness value.
Once sorted they should have a new index representing their fitness.
indi.getFitness() # Returns the fitness.
indi.setId(index) # sets the index
My initial approach is insufficient, as it doesn't include the sorting:
idx=0
for indi in self.indis:
indi.setId(idx)
idx += 1
Question is: how can I sort by fitness and re-index to make the best solution index 0?