0

我正在尝试对几何模型进行符号回归。而且它在大多数情况下都会卡在健身分数不接近 0 的情况下。所以我做了一些研究,发现这是局部最小值的问题。有些人试图将人口多样性置于健康之上。但这不是我想要的。

所以我所做的是重新配置algorithms.eaSimple并在其中添加了一个块。因此,当最后n=50一代具有相同的适应度时,它会重置种群。除此之外,我没有任何想法,因为我对它很陌生。 有没有更好的方法来做到这一点?

我正在使用最小健身。creator.create("FitnessMin", base.Fitness, weights=(-1.0,))

def my_eaSimple(population, toolbox, cxpb, mutpb, ngen, stats=None, halloffame: tools.HallOfFame = None,
                verbose=True):
    logbook = tools.Logbook()
    logbook.header = ['gen', 'nevals'] + (stats.fields if stats else [])

    # Evaluate the individuals with an invalid fitness
    invalid_ind = [ind for ind in population if not ind.fitness.valid]
    fitnesses = toolbox.map(toolbox.evaluate, invalid_ind)
    for ind, fit in zip(invalid_ind, fitnesses):
        ind.fitness.values = fit

    if halloffame is not None:
        halloffame.update(population)

    record = stats.compile(population) if stats else {}
    logbook.record(gen=0, nevals=len(invalid_ind), **record)
    if verbose:
        print(logbook.stream)

    # Begin the generational process
    gen = 1
    last_few_pop_to_consider = 50
    starting_condition = last_few_pop_to_consider
    is_last_few_fitness_same = lambda stats_array: abs(numpy.mean(stats_array) - stats_array[0]) < 0.1
    while gen < ngen + 1:
        # Select the next generation individuals
        offspring = toolbox.select(population, len(population))

        # Vary the pool of individuals
        offspring = algorithms.varAnd(offspring, toolbox, cxpb, mutpb)

        # Evaluate the individuals with an invalid fitness
        invalid_ind = [ind for ind in offspring if not ind.fitness.valid]
        fitnesses = toolbox.map(toolbox.evaluate, invalid_ind)
        for ind, fit in zip(invalid_ind, fitnesses):
            ind.fitness.values = fit

        # Update the hall of fame with the generated individuals
        if halloffame is not None:
            halloffame.update(offspring)

        # Replace the current population by the offspring
        population[:] = offspring

        # Append the current generation statistics to the logbook
        record = stats.compile(population) if stats else {}
        logbook.record(gen=gen, nevals=len(invalid_ind), **record)
        if verbose:
            print(logbook.stream)

        gen += 1

        # stopping criteria
        min_fitness = record['fitness']['min\t']
        # max_fitness = record['fitness']['max\t']

        if min_fitness < 0.1:
            print('Reached desired fitness')
            break

        if gen > starting_condition:
            min_stats = logbook.chapters['fitness'].select('min\t')[-last_few_pop_to_consider:]
            if is_last_few_fitness_same(min_stats):
                print('Defining new population')
                population = toolbox.population(n=500)
                starting_condition = gen + last_few_pop_to_consider

    return population, logbook

输出

gen nevals  avg         max         min     std         avg     max     min     std    
0   500     2.86566e+23 1.41421e+26 112.825 6.31856e+24 10.898  38      3       9.50282
1   451     2.82914e+18 1.41421e+21 90.113  6.31822e+19 6.226   38      1       5.63231
2   458     2.84849e+18 1.41421e+21 89.1206 6.3183e+19  5.602   36      1       5.18417
3   459     4.24902e+14 2.01509e+17 75.1408 9.01321e+15 5.456   35      1       4.05167
4   463     4.23166e+14 2.03904e+17 74.3624 9.11548e+15 6.604   36      1       3.61762
5   462     2.8693e+11  1.25158e+14 65.9366 5.60408e+12 7.464   34      1       3.00478
6   467     2.82843e+18 1.41421e+21 65.9366 6.31823e+19 8.144   37      1       3.51216
7   463     5.40289e+13 2.65992e+16 65.9366 1.1884e+15  8.322   22      1       2.88276
8   450     6.59849e+14 3.29754e+17 59.1286 1.47323e+16 8.744   34      1       3.03685
9   458     1.8128e+11  8.17261e+13 54.4395 3.65075e+12 9.148   23      1       2.69557
10  459     6.59851e+14 3.29754e+17 54.4395 1.47323e+16 9.724   35      1       3.02255
11  458     2.34825e+10 1.41421e+11 54.4395 5.26173e+10 9.842   18      1       2.32057
12  459     3.52996e+11 1.60442e+14 54.4395 7.1693e+12  10.56   33      1       2.63788
13  457     3.81044e+11 1.60442e+14 54.4395 7.18851e+12 11.306  35      1       2.84611
14  457     2.30681e+13 1.15217e+16 54.4395 5.14751e+14 11.724  24      1       2.6495 
15  463     2.65947e+10 1.41421e+11 54.4395 5.52515e+10 12.072  29      1       2.63036
16  469     4.54286e+10 9.13693e+12 54.4395 4.10784e+11 12.104  34      1       3.00752
17  461     6.58255e+11 1.74848e+14 54.4395 9.76474e+12 12.738  36      4       3.10956
18  450     2.03669e+10 1.41421e+11 54.4395 4.96374e+10 13.062  30      4       3.01963
19  465     1.75385e+10 2.82843e+11 54.4395 4.74595e+10 13.356  24      1       2.82157
20  458     1.83887e+10 1.41421e+11 54.4395 4.7559e+10  13.282  23      1       3.03949
21  455     3.67899e+10 8.36173e+12 54.4395 4.04044e+11 13.284  34      4       3.03106
22  461     1.36372e+10 1.41422e+11 54.4395 4.16569e+10 13.06   35      3       3.01005
23  471     2.00634e+26 1.00317e+29 54.3658 4.48181e+27 12.798  36      1       3.17698
24  466     2.82843e+18 1.41421e+21 54.3658 6.31823e+19 12.706  36      3       3.07043
25  464     3.00384e+10 8.36174e+12 54.3658 3.75254e+11 12.612  34      5       2.89231
26  474     2.00925e+10 1.41421e+11 54.3658 4.93588e+10 12.594  34      3       2.60253
27  452     2.9528e+11  1.41626e+14 54.3658 6.32694e+12 12.43   25      1       2.49822
28  453     1.23899e+10 1.41421e+11 54.3658 3.98511e+10 12.41   20      5       2.45721
29  456     5.98529e+14 2.99256e+17 54.3658 1.33697e+16 12.57   37      1       2.6346 
30  474     1.35672e+13 6.69898e+15 54.3658 2.99297e+14 12.526  35      1       2.94029
31  446     6.92755e+22 3.46377e+25 54.3658 1.5475e+24  12.55   36      1       2.62517
32  462     4.02525e+10 8.16482e+12 54.3658 3.92769e+11 12.764  34      5       2.77061
33  449     1.53268e+13 7.65519e+15 54.3658 3.42007e+14 12.628  35      1       2.76218
34  466     3.13214e+16 1.54388e+19 54.3658 6.89799e+17 12.626  35      1       2.97626
35  464     2.82845e+18 1.41421e+21 54.3658 6.31823e+19 12.806  36      5       2.74597
36  460     2.93493e+11 1.32308e+14 54.3658 5.91505e+12 12.734  35      5       2.88084
37  456     2.93491e+10 8.29826e+12 54.3658 3.72372e+11 12.614  37      1       2.80517
38  449     3.44519e+10 8.16482e+12 54.3658 3.67344e+11 12.742  34      3       2.91881
39  466     1.53217e+13 7.65519e+15 54.3658 3.42008e+14 12.502  35      3       2.70296
40  454     2.82843e+18 1.41421e+21 54.3658 6.31823e+19 12.51   36      1       2.81103
41  453     9.66059e+24 4.68888e+27 54.3658 2.09566e+26 12.554  33      1       2.47691
42  448     2.2287e+10  3.38289e+12 54.3658 1.58629e+11 12.576  26      1       2.50763
43  460     5.47399e+12 2.73042e+15 54.3658 1.21985e+14 12.584  34      1       2.80053
44  460     2.82843e+18 1.41421e+21 54.3658 6.31823e+19 12.692  27      1       2.86516
45  464     2.829e+18   1.41421e+21 54.3658 6.31823e+19 12.57   34      1       3.15549
46  460     2.92607e+11 1.31556e+14 54.3658 5.88776e+12 12.61   37      3       2.78817
47  465     2.82843e+18 1.41421e+21 54.3658 6.31823e+19 12.622  36      1       3.04616
48  461     1.64306e+10 2.97245e+12 54.3658 1.37408e+11 12.468  26      1       2.57856
49  463     1.54834e+10 1.41421e+11 54.3658 4.4029e+10  12.464  20      1       2.4529 
50  451     1.59239e+10 1.41421e+11 54.3658 4.44609e+10 12.63   33      1       2.76281
51  455     5.40036e+19 2.70018e+22 54.3658 1.20635e+21 12.78   37      1       2.84668
52  478     2.82843e+18 1.41421e+21 54.3658 6.31823e+19 12.712  36      3       2.84694
53  461     2.78669e+21 1.39193e+24 54.3658 6.21866e+22 12.714  36      1       3.23546
54  471     7.41272e+12 3.70045e+15 54.3658 1.65323e+14 12.336  34      3       2.848  
55  465     2.83036e+18 1.41421e+21 54.3658 6.31822e+19 12.74   36      1       3.62662
56  459     2.82843e+18 1.41421e+21 54.3658 6.31823e+19 12.606  29      1       2.60437
57  453     5.98308e+24 2.99154e+27 54.3658 1.33652e+26 12.722  34      1       2.62311
58  460     3.62463e+21 1.8109e+24  54.3658 8.09047e+22 12.65   37      1       2.92361
Defining new population
59  500     5.83025e+48 2.91513e+51 109.953 1.30238e+50 10.846  38      1       8.89889
60  464     2.93632e+15 8.87105e+17 165.988 4.38882e+16 5.778   36      1       4.79173
61  444     5.54852e+19 2.70018e+22 93.5182 1.20674e+21 4.992   37      1       4.648  
62  463     4.28647e+14 2.14148e+17 82.0774 9.56741e+15 5.468   34      1       4.34891
63  464     2.82843e+18 1.41421e+21 78.8184 6.31823e+19 6.624   35      1       4.25989
64  453     3.40035e+11 1.60954e+14 68.7629 7.19022e+12 7.356   36      1       3.77694
65  456     5.65762e+18 2.82851e+21 68.7629 1.26368e+20 7.606   35      1       4.15966
66  461     2.82843e+18 1.41421e+21 68.7629 6.31823e+19 7.906   35      1       3.81171
67  447     1.63302e+10 1.41421e+11 68.7629 4.51102e+10 7.802   33      1       3.47258
68  463     6.59552e+14 3.29754e+17 68.7629 1.47323e+16 8.37    34      3       3.80698
69  460     1.53579e+13 7.65512e+15 68.7629 3.42003e+14 8.646   35      1       3.64042
70  461     2.80014e+10 1.41421e+11 68.7629 5.63553e+10 9.212   38      1       3.69582
71  453     1.97446e+11 7.80484e+13 68.7629 3.50764e+12 9.84    34      1       3.74785
72  459     9.98853e+11 1.75397e+14 68.7629 1.25317e+13 10.284  35      3       3.61764
73  453     5.6863e+16  2.84218e+19 68.7629 1.26979e+18 10.796  36      1       3.86864
74  466     2.57445e+10 1.41434e+11 68.7629 5.4564e+10  10.806  35      1       3.2949 
75  453     2.82849e+18 1.41421e+21 68.7629 6.31823e+19 10.876  34      1       3.27301
76  433     1.67235e+20 8.36174e+22 68.7629 3.73574e+21 10.868  35      1       2.94051
77  457     3.6663e+21  1.83315e+24 68.7629 8.1899e+22  10.964  37      3       3.21476
78  461     1.80829e+14 9.04015e+16 68.7629 4.03883e+15 10.992  35      3       3.26985
79  450     3.21984e+11 1.41626e+14 68.7629 6.32593e+12 11.17   28      1       2.77941
80  460     2.82843e+18 1.41421e+21 68.7629 6.31823e+19 11.044  35      1       3.25362
81  455     6.46751e+14 2.99308e+17 68.7629 1.34123e+16 11.06   34      1       3.51061
82  463     3.21908e+21 1.60954e+24 68.7629 7.19088e+22 11.112  34      1       3.58433
83  473     2.82843e+18 1.41421e+21 68.7629 6.31823e+19 10.946  38      3       3.70663
84  460     3.14081e+11 1.41626e+14 68.7629 6.32625e+12 10.896  35      1       3.4976 
85  456     1.53419e+13 7.65526e+15 68.7629 3.4201e+14  11.156  36      1       3.23661

在第 59 代获得 54.4395 最低适应度 50 次后,人口将重置。

4

0 回答 0