2011-07-27 51 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