首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用pyevolve恢复优化

使用pyevolve恢复优化
EN

Stack Overflow用户
提问于 2011-07-27 16:38:54
回答 1查看 1.5K关注 0票数 2

我已经使用Pyevolve进行了优化,在查看了结果后,我想添加几代以获得更好的收敛性。由于评估相当长,我想知道我是否可以恢复我的优化到上一代,并增加大约20代。一切都必须在数据库中设置,我希望他可以成为可能。

以下是我的GA属性(类似于第一个示例,但具有更复杂的评估函数):

代码语言:javascript
复制
    # Genome instance, 1D List of 6 elements
genome = G1DList.G1DList(6)

# Sets the range max and min of the 1D List
genome.setParams(rangemin=1, rangemax=15)

# The evaluator function (evaluation function)
genome.evaluator.set(eval_func)

# Genetic Algorithm Instance
ga=GSimpleGA.GSimpleGA(genome)

# Set the Roulette Wheel selector method, the number of generations and
# the termination criteria
ga.selector.set(Selectors.GRouletteWheel)
ga.setGenerations(50)
ga.setPopulationSize(10)
ga.terminationCriteria.set(GSimpleGA.ConvergenceCriteria)

# Sets the DB Adapter, the resetDB flag will make the Adapter recreate
# the database and erase all data every run, you should use this flag
# just in the first time, after the pyevolve.db was created, you can
# omit it.
sqlite_adapter = DBAdapters.DBSQLite(identify="F-Beam-Optimization", resetDB=True)
ga.setDBAdapter(sqlite_adapter)

# Do the evolution, with stats dump
# frequency of 5 generations
ga.evolve(freq_stats=2)

有谁有这个想法吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2011-08-05 11:46:39

嗨,在回顾了Pyevolve的文档之后,似乎没有任何方法可以根据您在数据库中存储的内容来恢复演化(奇怪的行为)。

如果你想实现这种类型的机制,你可以时不时地关注一下你的群体,然后在Pyevolve中实现整个过程。

或者,您可以尝试DEAP,这是一个非常开放的框架,它允许您透明地查看和操作进化算法的各个方面。而且已经实现了一些检查点机制。

下面是你的代码在DEAP中的样子。

代码语言:javascript
复制
import random    
from deap import algorithms, base, creator, tools

# Create the needed types
creator.create("FitnessMax", base.Fitness, weights=(1.0,))
creator.create("Individual", list, fitness=creator.FitnessMax)

# Container for the evolutionary tools
toolbox = base.Toolbox()
toolbox.register("attr", random.random, 1, 15)
toolbox.register("individual", tools.initRepeat, creator.Individual, toolbox.attr, 6)
toolbox.register("population", tools.initRepeat, list, toolbox.individual)

# Operator registering
toolbox.register("evaluate", eval_func)
toolbox.register("mate", tools.cxTwoPoints)
toolbox.register("mutate", tools.mutGaussian, mu=0, sigma=1, indpb=0.05)
toolbox.register("select", tools.selTournament, tournsize=3)

population = toolbox.population(n=10)
stats = tools.Statistics(key=lambda ind: ind.fitness.values)
stats.register("Max", max)
checkpoint = tools.Checkpoint(population=population)

GEN, CXPB, MUTPB = 0, 0.5, 0.1
while stats.Max() < CONDITION:
    # Apply standard variation (crossover followed by mutation)
    offspring = algorithms.varSimple(toolbox, population, cxpb=CXPB, mutpb=MUTPB)

    # Evaluate the individuals
    fits = toolbox.map(toolbox.evaluate, offspring)
    for fit, ind in zip(fits, offspring):
        ind.fitness.values = fit

    # Select the fittest individuals
    offspring = [toolbox.clone(ind) for ind in toolbox.select(offspring, len(offspring)]
    # The "[:]" is important to not replace the label but what it contains
    population[:] = offspring

    stats.update(population)
    if GEN % 20 == 0:
        checkpoint.dump("my_checkpoint")
    GEN += 1

请注意,上面的代码尚未经过测试。但是它能做你想要的一切。现在,如何加载检查点并重新启动进化。

代码语言:javascript
复制
checkpoint = tools.Checkpoint()
checkpoint.load("my_checkpoint.ems")
population = checkpoint["population"]

# Continue the evolution has in before

此外,DEAP有很好的文档记录,有超过25个不同的例子,帮助新用户非常快地升级,我也听说开发人员对问题的回答非常快。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/6841573

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档