本文整理汇总了Python中pool.Pool.close方法的典型用法代码示例。如果您正苦于以下问题:Python Pool.close方法的具体用法?Python Pool.close怎么用?Python Pool.close使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类pool.Pool
的用法示例。
在下文中一共展示了Pool.close方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: myAsyncEA
# 需要导入模块: from pool import Pool [as 别名]
# 或者: from pool.Pool import close [as 别名]
#.........这里部分代码省略.........
def eval_individual(ind):
ind.fitness.values = toolbox.evaluate(ind)
return ind
def gen_new_offspring():
valid_offspring = True
offspring = None
while valid_offspring:
# select two individuals and clone them
offspring = map(toolbox.clone, toolbox.select(population, 2))
# apply crossover and mutation, take the first offspring
offspring = algorithms.varAnd(offspring, toolbox, cxpb, mutpb)[0]
valid_offspring = offspring.fitness.valid
# if the offspring is valid it is already in the population
return offspring
def log_stats():
record = stats.compile(population)
logbook.record(gen=start_gen + (num_evals // popsize), **record)
if verbose:
print(logbook.stream)
def save_checkpoint():
cp = dict(population=population, generation=start_gen + (num_evals // popsize), halloffame=halloffame,
logbook=logbook, rndstate=random.getstate())
if id is None:
cp_name = "checkpoint_ea.pkl"
else:
cp_name = "checkpoint_ea_{}.pkl".format(id)
pickle.dump(cp, open(cp_name, "wb"))
popsize = len(population)
MIN_POP_SIZE = popsize // 2
total_time = datetime.timedelta(seconds=0)
pool = Pool(processors=PROCESSORS, evalfunc=eval_individual)
db = Database()
if all([ind.fitness.valid for ind in population]):
print("All individuals have valid fitness")
# all inds are valid (loaded from checkpoint), generate some new to fill the pool
for _ in range(PROCESSORS):
offspring = gen_new_offspring()
pool.putQuerry(offspring)
else:
# put all individuals to the pool
for ind in population:
pool.putQuerry(ind)
population = [] # we get them back later
num_evals = 0
start_time = datetime.datetime.now()
while num_evals < (ngen-start_gen)*popsize:
# get finished individual and add him to population
ind = pool.getAnswer()
assert ind.fitness.valid
# add fitness and individual to the database
db.insert(ind, ind.fitness.values[0])
population.append(ind)
num_evals += 1
# update halloffame
halloffame.update([ind])
if len(population) < MIN_POP_SIZE:
continue
if num_evals % popsize == 0:
# record statistics and save checkpoint
log_stats()
save_checkpoint()
# check time
eval_time = datetime.datetime.now() - start_time
total_time = total_time + eval_time
#print("Time ", total_time)
if total_time > datetime.timedelta(hours=8*24):
print("Time limit exceeded.")
break
start_time = datetime.datetime.now()
if len(population) > popsize:
# delete the worst one
population = list(sorted(population, key=attrgetter('fitness')))[1:]
# generate new offspring and put him to the pool
offspring = gen_new_offspring()
pool.putQuerry(offspring)
pool.close()
# print(db.data)
db.save("database." + id + ".pkl")
return population, logbook