使用pyevolve恢复优化

2 投票
1 回答
1505 浏览
提问于 2025-04-16 22:21

我用Pyevolve做了一些优化,查看结果后,我想再增加几代,以便更好地收敛。因为评估过程比较长,我在想是否可以从最后一代继续我的优化,然后再增加大约20代。我希望所有的设置都在数据库里,这样应该是可行的。

这是我的遗传算法属性(和第一个例子类似,但评估函数更复杂):

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

有没有人有这个想法?

1 个回答

3

你好,经过查看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个多样化的示例,可以帮助新用户快速上手。我还听说开发者们对问题的回答也很迅速。

撰写回答