2011-07-27 48 views
2

我已经使用Pyevolve进行了优化,并且在查看结果后,我想添加几代以获得更好的收敛性。由于评估时间很长,我想知道是否可以继续优化到最后一代,并增加20代。一切都必须在DB中设定,我希望他可以做到。使用pyevolve恢复优化

这里是我的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) 

任何人的想法?

回答

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多样化的例子,可以帮助新用户非常迅速提升,我还听说开发商回答非常迅速地质疑。

+0

谢谢你的回答。我将对DEAP的进化能力有一个清晰的认识。评估功能相当长时,恢复一些优化是非常有帮助的。 – TazgerO

+0

任何想法如何随着你一起去泡菜?这将是非常有帮助 – Anake

+0

泡菜和DEAP?如果您有关于DEAP的特定问题,请使用用户组。 – mitch