本文整理汇总了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()
示例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