评价停留在遗传编程DEAP的局部最优中。如何防止GP收敛于局部最优值?

时间:2019-05-16 09:42:06

标签: genetic-programming deap fitness

我正在尝试对几何模型进行符号回归。而且它在大多数时候都因健身得分不接近0而卡住。因此,我进行了一些研究,发现这是局部极小值的问题。一些人试图将人口多样性优先于健康。但这不是我想要的。

所以我要做的是重新配置guard let selectedImage = info[.editedImage] as? UIImage else { } 并在其中添加一个块。因此,当最后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

在第59代中获得504.4395的最低适应度后,人口得到重置。

0 个答案:

没有答案