本文整理汇总了Python中population.Population.get_fittest方法的典型用法代码示例。如果您正苦于以下问题:Python Population.get_fittest方法的具体用法?Python Population.get_fittest怎么用?Python Population.get_fittest使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类population.Population
的用法示例。
在下文中一共展示了Population.get_fittest方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: __init__
# 需要导入模块: from population import Population [as 别名]
# 或者: from population.Population import get_fittest [as 别名]
def __init__(self, sol, use_random_crossover=True):
generation_count = 0
fitness.set_solution(sol)
population = Population(50, initialize=True)
while population.get_fittest().get_fitness() < fitness.get_max_fitness():
print "Generation: %d" % (generation_count, )
print "Fittest Scores: %d" % (population.get_fittest().get_fitness(),)
print "Fittest: %s\n" % (population.get_fittest().genes.bin,)
generation_count += 1
population = Algorithm(use_random_crossover).evolvePopulation(population)
msg = "===== SOLUTION FOUND! =====\nGENERATION: %d\nGENES: %s\n"
print msg % (generation_count, population.get_fittest().genes.bin)
示例2: tournament_selection
# 需要导入模块: from population import Population [as 别名]
# 或者: from population.Population import get_fittest [as 别名]
def tournament_selection(self, pop):
tournament = Population(self._tournament_size, False)
for i in xrange(self._tournament_size):
random_id = int(random.random() * pop.population_size())
tournament.save_tour(i, pop.get_tour(random_id))
return tournament.get_fittest()
示例3: tournament_selection
# 需要导入模块: from population import Population [as 别名]
# 或者: from population.Population import get_fittest [as 别名]
def tournament_selection(self, population):
population_size = len(population.individuals)
tournament = Population(self.tournament_size, False)
for i in range(0, self.tournament_size):
randomId = int(random() * population_size)
tournament.individuals.append(population.individuals[randomId])
return tournament.get_fittest()
示例4: __init__
# 需要导入模块: from population import Population [as 别名]
# 或者: from population.Population import get_fittest [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()
示例5: evolve
# 需要导入模块: from population import Population [as 别名]
# 或者: from population.Population import get_fittest [as 别名]
def evolve(pop: population.Population) -> population.Population:
new_population = population.Population()
if elitism:
new_population.add(pop.get_fittest())
for _ in range((len(pop.indivs) - 1) if elitism else len(pop.indivs)):
indiv1 = tournament(pop)
indiv2 = tournament(pop)
new_population.add(crossover(indiv1, indiv2))
for indiv in new_population.indivs:
mutate(indiv)
return new_population
示例6: main
# 需要导入模块: from population import Population [as 别名]
# 或者: from population.Population import get_fittest [as 别名]
def main():
generations, num_cities, population_size = parse_args()
tour_manager = TourManager()
tour_manager.create_tour_cities(num_cities)
population = Population(population_size, True, tour_manager)
print "Initial distance = %d" % (population.get_fittest().get_distance(),)
for i in range(0, generations):
population = algorithm.evolve_population(population)
print "generation=%d shortest_distance=%d" % (i, population.get_fittest().get_distance())
fittest = population.get_fittest()
print """
Finished
Final distance=%d
Solution
--------
%s
""" % (fittest.get_distance(), fittest)