roulette_wheel_selection.py 文件源码

python
阅读 28 收藏 0 点赞 0 评论 0

项目:gaft 作者: PytLab 项目源码 文件源码
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
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号