使用 pyevolve 恢复优化

发布于 11-26 19:16 字数 1176 浏览 8 评论 0原文

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

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

    # 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)

有人有这个想法吗?

I have done an optimization with Pyevolve and after a look at the results I wanted to add a few generation to have a better convergence. As an evaluation is quite long, I was wondering if I can resume my optimization to the last generation and add like 20 more generations. Everything must be set in the DB I hope so he can be possible.

Here is my GA properties (similar to the first example but with a more complicated evaluation function):

    # 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)

Anyone with the idea?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

帝王念2024-12-03 19:16:47

您好,在查看了 Pyevolve 的文档后,似乎没有任何方法可以根据您存储在数据库中的内容恢复进化(奇怪的行为)。

如果你想实现这种类型的机制,你可以偶尔对你的人口进行一次腌制,然后在 Pyevolve 中实现整个过程。

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

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

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

请注意,上述代码尚未经过测试。但它会满足您的所有要求。现在如何加载检查点并重新启动演化。

checkpoint = tools.Checkpoint()
checkpoint.load("my_checkpoint.ems")
population = checkpoint["population"]

# Continue the evolution has in before

此外,DEAP 的文档非常齐全,有超过 25 个多样化的示例,可以帮助新用户快速入门,我还听说开发人员回答问题的速度非常快。

Hi after reviewing the documentation of Pyevolve there doesn't seem to be any way to resume an evolution base on what you stored in the database (strange behaviour).

If you want to implement this type of mechanism, you could look at pickling your population once and a while and implementing the whole thing in Pyevolve.

Or, you could try DEAP a very open framework that let you see and manipulate every aspect of an evolutionary algorithm, transparently. And there is already some checkpointing mechanism implemented.

Here is what your code would look like in DEAP.

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

Note that the above code has not been tested. But it does everything you request for. Now how to load a checkpoint and restart an evolution.

checkpoint = tools.Checkpoint()
checkpoint.load("my_checkpoint.ems")
population = checkpoint["population"]

# Continue the evolution has in before

Moreover, DEAP is very well documented and has over 25 diversified examples that help new user to ramp up very quickly, I also heard that developers answer to question very quickly.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文