本文整理汇总了Python中deap.tools.selBest方法的典型用法代码示例。如果您正苦于以下问题:Python tools.selBest方法的具体用法?Python tools.selBest怎么用?Python tools.selBest使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类deap.tools
的用法示例。
在下文中一共展示了tools.selBest方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: main
# 需要导入模块: from deap import tools [as 别名]
# 或者: from deap.tools import selBest [as 别名]
def main():
random.seed(64)
NISLES = 5
islands = [toolbox.population(n=300) for i in range(NISLES)]
# Unregister unpicklable methods before sending the toolbox.
toolbox.unregister("attr_bool")
toolbox.unregister("individual")
toolbox.unregister("population")
NGEN, FREQ = 40, 5
toolbox.register("algorithm", algorithms.eaSimple, toolbox=toolbox,
cxpb=0.5, mutpb=0.2, ngen=FREQ, verbose=False)
for i in range(0, NGEN, FREQ):
results = toolbox.map(toolbox.algorithm, islands)
islands = [pop for pop, logbook in results]
tools.migRing(islands, 15, tools.selBest)
return islands
示例2: filterAwayWorst
# 需要导入模块: from deap import tools [as 别名]
# 或者: from deap.tools import selBest [as 别名]
def filterAwayWorst(population, N=5):
aliveSize = len(population) - 5
population = tools.selBest(population, aliveSize)
return population
示例3: selBest
# 需要导入模块: from deap import tools [as 别名]
# 或者: from deap.tools import selBest [as 别名]
def selBest(individuals, number):
chosen = sorted(individuals, key=selectCriteria, reverse=True)
return chosen[:number]
示例4: test_cma
# 需要导入模块: from deap import tools [as 别名]
# 或者: from deap.tools import selBest [as 别名]
def test_cma():
NDIM = 5
strategy = cma.Strategy(centroid=[0.0]*NDIM, sigma=1.0)
toolbox = base.Toolbox()
toolbox.register("evaluate", benchmarks.sphere)
toolbox.register("generate", strategy.generate, creator.__dict__[INDCLSNAME])
toolbox.register("update", strategy.update)
pop, _ = algorithms.eaGenerateUpdate(toolbox, ngen=100)
best, = tools.selBest(pop, k=1)
assert best.fitness.values < (1e-8,), "CMA algorithm did not converged properly."
示例5: end
# 需要导入模块: from deap import tools [as 别名]
# 或者: from deap.tools import selBest [as 别名]
def end(self):
"""
See :meth:`~l2l.optimizers.optimizer.Optimizer.end`
"""
# ------------ Finished all runs and print result --------------- #
logger.info("-- End of (successful) evolution --")
best_inds = tools.selBest(self.pop, 10)
for best_ind in best_inds:
logger.info("Best individual is %s, %s" % (best_ind, best_ind.fitness.values))
logger.info("-- Hall of fame --")
for hof_ind in self.hall_of_fame:
logger.info("HOF individual is %s, %s" % (hof_ind, hof_ind.fitness.values))
示例6: main
# 需要导入模块: from deap import tools [as 别名]
# 或者: from deap.tools import selBest [as 别名]
def main(procid, pipein, pipeout, sync, seed=None):
random.seed(seed)
toolbox.register("migrate", migPipe, k=5, pipein=pipein, pipeout=pipeout,
selection=tools.selBest, replacement=random.sample)
MU = 300
NGEN = 40
CXPB = 0.5
MUTPB = 0.2
MIG_RATE = 5
deme = toolbox.population(n=MU)
hof = tools.HallOfFame(1)
stats = tools.Statistics(lambda ind: ind.fitness.values)
stats.register("avg", numpy.mean)
stats.register("std", numpy.std)
stats.register("min", numpy.min)
stats.register("max", numpy.max)
logbook = tools.Logbook()
logbook.header = "gen", "deme", "evals", "std", "min", "avg", "max"
for ind in deme:
ind.fitness.values = toolbox.evaluate(ind)
record = stats.compile(deme)
logbook.record(gen=0, deme=procid, evals=len(deme), **record)
hof.update(deme)
if procid == 0:
# Synchronization needed to log header on top and only once
print(logbook.stream)
sync.set()
else:
logbook.log_header = False # Never output the header
sync.wait()
print(logbook.stream)
for gen in range(1, NGEN):
deme = toolbox.select(deme, len(deme))
deme = algorithms.varAnd(deme, toolbox, cxpb=CXPB, mutpb=MUTPB)
invalid_ind = [ind for ind in deme if not ind.fitness.valid]
for ind in invalid_ind:
ind.fitness.values = toolbox.evaluate(ind)
hof.update(deme)
record = stats.compile(deme)
logbook.record(gen=gen, deme=procid, evals=len(deme), **record)
print(logbook.stream)
if gen % MIG_RATE == 0 and gen > 0:
toolbox.migrate(deme)