genetic_sort.py 文件源码

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

项目:ecs 作者: ecs-org 项目源码 文件源码
def next_generation(self):
        next_population = []
        wheel = []
        for permutation in self.population:
            value = self.evaluation[id(permutation)]
            fitness = value / self.avg_value
            fitness_fraction, copies = math.modf(fitness)
            if fitness_fraction:
                wheel.append(permutation)
                wheel.append(permutation)
            if copies == 1:
                next_population.append(permutation)
            elif copies > 1:
                next_population += [permutation] * int(copies)

        # XXX: fractional copies should be placed with a proportinal probability (FMD2)
        next_population += random.sample(wheel, len(self.population) - len(next_population))
        next_population_size = len(next_population)

        # crossover
        crossover_count = int(0.5 * self.crossover_p * next_population_size)
        crossover_indices = random.sample(range(next_population_size), 2 * crossover_count)
        for i in range(crossover_count):
            index_a, index_b = crossover_indices[2*i], crossover_indices[2*i+1]
            next_population[index_a], next_population[index_b] = self.crossover(next_population[index_a], next_population[index_b])

        # mutation
        for mutation, p in self.mutations:
            for index in random.sample(range(next_population_size), int(p * next_population_size)):
                next_population[index] = mutation(next_population[index])

        self.population = next_population
        self.generation_count += 1
        self.evaluate()
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号