def classical_genetic_algorithm():
    POPULATION_SIZE = 140
    CROSSOVER_PROBABILITY = .3
    MUTATION_PROBABILITY = .9
    MAX_GENERATIONS = 140

    Individual.count = 0
    first_population = [
        generate_random(len(points)) for _ in range(POPULATION_SIZE)
    ]
    best_ind = random.choice(first_population)
    fit_avg = []
    fit_best = []
    generation_num = 0
    population = first_population.copy()

    while generation_num < MAX_GENERATIONS:
        generation_num += 1
        offspring = selection_rank_with_elite(population, elite_size=2)
        crossed_offspring = crossover_operation(offspring, crossover,
                                                CROSSOVER_PROBABILITY)
        mutated_offspring = mutation_operation(crossed_offspring, mutate,
                                               MUTATION_PROBABILITY)
        population = mutated_offspring.copy()
        best_ind, fit_avg, fit_best = stats(population, best_ind, fit_avg,
                                            fit_best)

    return best_ind.fitness, Individual.count
def adaptive_genetic_algorithm():
    POPULATION_SIZE = 200
    MIN_POPULATION_SIZE = 50
    MAX_POPULATION_SIZE = 300

    CROSSOVER_PROBABILITY = .5
    MIN_CROSSOVER_PROBABILITY = .1

    MUTATION_PROBABILITY = .5
    MIN_MUTATION_PROBABILITY = .1

    MAX_GENERATIONS = 10_000
    MIN_GENERATIONS = 100

    ELITE_SIZE = 1

    Individual.count = 0
    first_population = [
        generate_random(len(points)) for _ in range(POPULATION_SIZE)
    ]
    best_ind = random.choice(first_population)
    fit_avg = []
    fit_best = []
    learn_trend = []
    ev_avg = []
    population_size = []
    mutation_prob = []
    crossover_prob = []
    generation_num = 0
    population = first_population.copy()

    while generation_num < MIN_GENERATIONS or\
            (generation_num < MAX_GENERATIONS and is_learning_positive(fit_best, 50, .001)):
        generation_num += 1
        offspring = selection_rank_with_elite(population,
                                              elite_size=ELITE_SIZE)
        crossed_offspring = crossover_operation(offspring, crossover,
                                                CROSSOVER_PROBABILITY)
        mutated_offspring = mutation_operation(crossed_offspring, mutate,
                                               MUTATION_PROBABILITY)
        population = mutated_offspring.copy()
        best_ind, fit_avg, fit_best = stats(population, best_ind, fit_avg,
                                            fit_best)
        ev_avg.append(average(fit_avg, 10))
        learn_rate = is_learning_positive(fit_avg, 10, .001)
        learn_trend.append(learn_rate)
        if not learn_rate:
            MUTATION_PROBABILITY = min(MUTATION_PROBABILITY * 1.1, 1)
            CROSSOVER_PROBABILITY = min(CROSSOVER_PROBABILITY * 1.1, 1)
            if len(population) < MAX_POPULATION_SIZE:
                population = population + [
                    generate_random(len(points)) for _ in range(2)
                ]
        else:
            MUTATION_PROBABILITY = max(MUTATION_PROBABILITY * .99,
                                       MIN_MUTATION_PROBABILITY)
            CROSSOVER_PROBABILITY = max(CROSSOVER_PROBABILITY * .99,
                                        MIN_CROSSOVER_PROBABILITY)
            if len(population) > MIN_POPULATION_SIZE:
                worst_ind = min(population, key=lambda ind: ind.fitness)
                population.remove(worst_ind)

        population_size.append(len(population))
        crossover_prob.append(CROSSOVER_PROBABILITY)
        mutation_prob.append(MUTATION_PROBABILITY)

    return best_ind.fitness, Individual.count
Exemplo n.º 3
0
first_population = [
    generate_random(len(points)) for _ in range(POPULATION_SIZE)
]
best_ind = random.choice(first_population)
fit_avg = []
fit_best = []
impr_list = []
ev_avg = []
generation_num = 0
population = first_population.copy()

while generation_num < MAX_GENERATIONS:
    generation_num += 1
    offspring = selection_rank_with_elite(population, elite_size=ELITE_SIZE)
    crossed_offspring = crossover_operation(offspring, crossover,
                                            CROSSOVER_PROBABILITY)
    mutated_offspring = mutation_operation(crossed_offspring, mutate,
                                           MUTATION_PROBABILITY)
    population = mutated_offspring.copy()
    best_ind, fit_avg, fit_best = stats(population, best_ind, fit_avg,
                                        fit_best)
    ev_avg.append(average(fit_avg, 10))
    impr_list.append(is_improvement_positive(fit_avg, 10, .001))


def add_degradation_areas(learn_trend):
    for i in range(len(learn_trend)):
        if not learn_trend[i]:
            plt.axvspan(i, i + 1, color='red', alpha=0.1)