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


Python Population.get_tour方法代码示例

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


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

示例1: __init__

# 需要导入模块: from population import Population [as 别名]
# 或者: from population.Population import get_tour [as 别名]
    def __init__(self, window, cities_list, show_window=True):
        if show_window:
            self.ini = window.figure.add_subplot(211)
            self.end = window.figure.add_subplot(212)

        self._cities_list = cities_list
        random.seed()

        pop = Population(1000, True, self._cities_list)
        if show_window:
            self.plt_reload(211, self._cities_list, 'Starting')
        print 'Initial distance: %.4f' % (pop.get_fittest().get_distance())
        self.x_graph.append(0)
        self.y_graph.append(pop.get_fittest().get_distance())

        for j in xrange(100):
            new_population = Population(pop.population_size(), False)

            elitism_offset = 0
            if self._elitism:
                new_population.save_tour(0, pop.get_fittest())
                elitism_offset = 1

            for i in xrange(elitism_offset, new_population.population_size()):
                parent1 = self.tournament_selection(pop)
                parent2 = self.tournament_selection(pop)

                # Crossover parents
                child = self.crossover(parent1, parent2)

                new_population.save_tour(i, child)

            # mutate
            for i in xrange(elitism_offset, new_population.population_size()):
                self.mutate(new_population.get_tour(i))
            pop = new_population
            if show_window:
                self.plt_reload(212, pop.get_fittest().get_tour(), 'Iteration: ' + str(j + 1))
            self.x_graph.append(self.x_graph[-1] + 1)
            self.y_graph.append(pop.get_fittest().get_distance())

        if show_window:
            self.plt_reload(212, pop.get_fittest().get_tour(), 'Iteration: ' + str(j + 1))
        self.x_graph.append(self.x_graph[-1] + 1)
        self.y_graph.append(pop.get_fittest().get_distance())

        print 'Final distance: %.4f' % (pop.get_fittest().get_distance())
        self.best_distance = pop.get_fittest().get_distance()
开发者ID:rodolphopivetta,项目名称:traveling-salesman,代码行数:50,代码来源:__init__.py

示例2: evolve_population

# 需要导入模块: from population import Population [as 别名]
# 或者: from population.Population import get_tour [as 别名]
def evolve_population(population):
    new_population = Population(population.size(), False, population.tour_manager)
    
    elitism_offset = 0
    
    # save the fittest tour
    if elitism:
        new_population.save_tour(0, population.get_fittest())
        elitism_offset = 1
    
    # crossover
    for i in range(elitism_offset, new_population.size()):
        parent_1 = tournament_selection(population)
        parent_2 = tournament_selection(population)
        
        child = crossover(parent_1, parent_2)
        new_population.save_tour(i, child)
    
    # mutate population
    for i in range(elitism_offset, new_population.size()):
        mutate(new_population.get_tour(i))
    
    return new_population
开发者ID:akaptur,项目名称:genetic-algorithm-experiments,代码行数:25,代码来源:algorithm.py


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