【问题标题】:Python DEAP, how to stop the evolution when the fitness doesn't increase after X generations?Python DEAP,当X代后适应度没有增加时如何停止进化?
【发布时间】:2017-08-01 03:39:11
【问题描述】:

我想在适应度没有增加时停止遗传算法。

我在 python 中使用 DEAP 库。

通常,我有以下日志文​​件:

    gen nevals  mean        max     
    0   100 0.352431    0.578592
    1   83  -0.533964   0.719633
    2   82  -0.567494   0.719633
    3   81  -0.396759   0.751318
    4   74  -0.340427   0.87888
    5   80  -0.29756    0.888443
    6   86  -0.509486   0.907789
    7   85  -0.335586   1.06199
    8   69  -0.23967    1.12339
    9   73  -0.10727    1.20622
    10  88  -0.181696   1.20622
    11  77  -0.188449   1.20622
    12  72  0.135398    1.25254
    13  67  0.0304611   1.26931
    14  74  -0.0436463  1.3181
    15  70  0.289306    1.37582
    16  79  -0.0441134  1.37151
    17  73  0.339611    1.37204
    18  68  -0.137938   1.37204
    19  76  0.000527522 1.40034
    20  84  0.198005    1.40078
    21  69  0.243705    1.4306
    22  74  0.11812 1.4306
    23  83  0.16235 1.4306
    24  82  0.270455    1.43492
    25  76  -0.200259   1.43492
    26  77  0.157181    1.43492
    27  74  0.210868    1.43492

我最初设置 ngen = 200,但如您所见,适应度函数在第 22 代时达到了局部最大值。所以我想在发生这种情况时停止遗传算法。

【问题讨论】:

  • 您可以使用简单的中断条件来调整您的算法。所有统计数据均在logbook 中存储。

标签: python genetic-algorithm deap


【解决方案1】:
def main():
    random.seed(64)
    pop = toolbox.population(n=100)
    CXPB, MUTPB = 0.5, 0.2
    print "Start of evolution"
    fitnesses = list(map(toolbox.evaluate, pop))
    for ind, fit in zip(pop, fitnesses):
        ind.fitness.values = fit
    print "  Evaluated %i individuals" % len(pop)
    fits = [ind.fitness.values[0] for ind in pop]
    g = 0
    while max(fits) < 0.67 and g < 1000000:
        g = g + 1
        print "-- Generation %i --" % g
        offspring = toolbox.select(pop, len(pop))
        offspring = list(map(toolbox.clone, offspring))
        for child1, child2 in zip(offspring[::2], offspring[1::2]):
            if random.random() < CXPB:
                toolbox.mate(child1, child2)
                del child1.fitness.values
                del child2.fitness.values
        for mutant in offspring:
            if random.random() < MUTPB:
                toolbox.mutate(mutant)
                del mutant.fitness.values
        invalid_ind = [ind for ind in offspring if not ind.fitness.valid]
        fitnesses = map(toolbox.evaluate, invalid_ind)
        for ind, fit in zip(invalid_ind, fitnesses):
            ind.fitness.values = fit
        pop[:] = offspring
        fits = [ind.fitness.values[0] for ind in pop]
        print "fitness-- ",max(fits)
    print "-- End of (successful) evolution --"
    best_ind = tools.selBest(pop, 1)[0]
    triangle_to_image(best_ind).save('best.jpg')

这将在达到所需的适应度值时停止代码或特定代数结束

你可以设置成当适应度在一段时间内没有改变时停止,即当它达到局部最大值并卡在那里

第 12 行 此示例在适应度超过 0.67 时停止 然后保存结果

当你不使用名人堂之类的东西时,这是这样做的方法 不知道怎么做,如果你找到了也告诉我

【讨论】:

    【解决方案2】:

    老实说,我最近也在研究这个问题。 根据我最近所做的研究,我发现:

    1. 有一个实现 CMA-ES 算法的 DEAP 示例。它包含停止标准 (Python DEAP, how to stop the evolution when the fitness doesn't increase after X generations?)
    2. 有一篇论文值得一读:https://heal.heuristiclab.com/system/files/diss%20gkr2.pdf
    3. 上述解决方案实现了问题中提到的内容:https://github.com/DEAP/deap/issues/271

    我还没有尝试过以上任何方法,但我非常确定它会起作用。

    【讨论】:

    • 在您自己的答案中链接到不同的答案似乎有点奇怪......
    猜你喜欢
    • 1970-01-01
    • 2018-08-02
    • 1970-01-01
    • 2017-10-06
    • 1970-01-01
    • 1970-01-01
    • 2012-11-13
    • 1970-01-01
    • 2021-08-04
    相关资源
    最近更新 更多