当前位置: 首页>>代码示例>>Python>>正文


Python Track.approximate_positions方法代码示例

本文整理汇总了Python中track.Track.approximate_positions方法的典型用法代码示例。如果您正苦于以下问题:Python Track.approximate_positions方法的具体用法?Python Track.approximate_positions怎么用?Python Track.approximate_positions使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在track.Track的用法示例。


在下文中一共展示了Track.approximate_positions方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: evolve

# 需要导入模块: from track import Track [as 别名]
# 或者: from track.Track import approximate_positions [as 别名]
    def evolve(self):
        """Evolves the population."""
        graded = [ (self.fitness(x), x) for x in self.tracks ]
        grade = self.grade(graded)
        graded = [ x[1] for x in sorted(graded, key=itemgetter(0)) ]
        retain_length = int(len(graded)*self.retain_percentage)
        parents = graded[:retain_length]

        # randomly add other individuals to promote genetic diversity
        for individual in graded[retain_length:]:
            if self.random_select_chance > random():
                parents.append(individual)

        # mutate some individuals
        for i, _ in enumerate(parents):
            if self.mutate_chance > random():
                pos_to_mutate = randint(0, len(parents[i].positions) - 1)
                mutated_positions = parents[i].positions
                point_x = randint(0, self.map.size_x)
                point_y = randint(0, self.map.size_y)
                mutated_positions[pos_to_mutate] = Point(point_x, point_y)
                mutated_individual = Track(self.map)
                mutated_individual.approximate_positions(mutated_positions)
                parents[i] = mutated_individual


        # crossover parents to create children
        parents_length = len(parents)
        desired_length = len(self.tracks) - parents_length
        children = []
        for i in range(desired_length):
            male = 0
            female = 0
            while male == female:
                male = randint(0, parents_length - 1)
                female = randint(0, parents_length - 1)
            male = parents[male]
            female = parents[female]
            half_male = int(len(male.positions) / 2)
            half_female = int(len(female.positions) / 2)
            child_positions = (male.positions[:half_male] +
                female.positions[half_female:] )
            child = Track(self.map)
            child.approximate_positions(child_positions)
            children.append(child)

        parents.extend(children)
        self.tracks = parents
        return grade
开发者ID:Faerbit,项目名称:evol-racer,代码行数:51,代码来源:population.py


注:本文中的track.Track.approximate_positions方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。