def select(self, population, fitness):
'''
Select a pair of parent using FPS algorithm.
'''
# Normalize fitness values for all individuals.
fit = population.all_fits(fitness)
min_fit = min(fit)
fit = [(i - min_fit) for i in fit]
# Create roulette wheel.
sum_fit = sum(fit)
wheel = list(accumulate([i/sum_fit for i in fit]))
# Select a father and a mother.
father_idx = bisect_right(wheel, random())
father = population[father_idx]
mother_idx = (father_idx + 1) % len(wheel)
mother = population[mother_idx]
return father, mother
评论列表
文章目录