本文整理汇总了Python中population.Population.mutate方法的典型用法代码示例。如果您正苦于以下问题:Python Population.mutate方法的具体用法?Python Population.mutate怎么用?Python Population.mutate使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类population.Population
的用法示例。
在下文中一共展示了Population.mutate方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: run
# 需要导入模块: from population import Population [as 别名]
# 或者: from population.Population import mutate [as 别名]
def run():
cities = preprocessData("berlin.tsp")
pop = Population(cities, GENERATION_SIZE)
history = []
i = 0
best = pop.getBestMember().length()
for j in range(SIMULATED_POPULATION_COUNT):
# get next generation
pop = pop.copyPopulation()
pop.crossover(CROSSOVER_COUNT, CROSSOVER_SIZE_MIN, CROSSOVER_SIZE_MAX)
pop.mutate(GENERATION_SIZE-CROSSOVER_COUNT, MUTATE_COUNT)
best_member = pop.getBestMember()
new_fitness = best_member.length()
history.append(new_fitness)
if(best > new_fitness):
best = new_fitness
if(PLOT_PATH):
gra = int(122)
plt.clf()
for c in best_member._cities:
plt.plot([c.x], [c.y], color="red", aa=True, marker="o")
plt.annotate(int(c.name), (c.x, c.y), color="red")
plt.title('Fitness = %.2fkm' % new_fitness)
best_member.draw(plt, "#%02x%02x%02x" % (gra, gra, gra))
plt.savefig("stats/fig%s.png" % i)
i += 1
return history
示例2: print
# 需要导入模块: from population import Population [as 别名]
# 或者: from population.Population import mutate [as 别名]
print("Gen: {}\tPopulation: {}\tFitnesses: {}\tAverage Fitness: {}"
.format(current_generation, ga.population, ga.fitnesses, avg))
# Selection at random for mating
lowest_fitness_index = ga.fitnesses.index(min(ga.fitnesses))
del ga.population[lowest_fitness_index]
del ga.fitnesses[lowest_fitness_index]
# Crossover/Repr
parents = ga.random_mate_select(ga.population)
children = ga.reproduce(parents[0], parents[1])
parents2 = ga.random_mate_select(ga.population)
children = children + ga.reproduce(parents2[0], parents2[1])
# Mutation with small independent probability
for child in children:
if random.random() < ga.p_mutation:
children[children.index(child)] = ga.mutate(child)
ga.population = children
ga.fitnesses.clear()
current_generation += 1
if current_generation == ga.max_generations:
print("\nGlobal Optimum: ")
print("Individual: {}\tFitness: {}\tGeneration: {}\n"
.format(ga.global_optimum[0], ga.global_optimum[1], ga.global_optimum[2]))
num_gens = input("How many more generations would you like to run? (0 to stop)\n")
ga.max_generations += int(num_gens)