Exemple #1
0
def reproduce(population):
    """Applies the evolutionary survival process to the population.

    A few select elites with the best reward (fitness) is automatically part of the next generation.
    From the population parents are selected to create offspring. The offspring will be part of
    the next generation. The parents will cease to exist unless they were part of the elite.
    Mutation is applied to the offspring, which has a chance of altering the chromosomes.

    Args:
        The population which is a list of the object Individual. The entire population just ran in
        the simulation and their reward (fitness) was stored.

    Returns:
        A new population which is a list of the object Individual. The new population is the next generation
        and consists of the elite from the previous population and offspring from the previous generation.
    """

    # Sort the population by its reward (fitness)
    population = sorted(population, key=lambda x: x.reward, reverse=True)
    total_remove = 94

    if judgement_day(population):
        new_population = [population[0]]

        for i in range(100 - len(new_population)):
            new_population.append(random_neural_network())

        return new_population

    # Elitism
    new_population = population[:(len(population) - total_remove)]

    # Selection, may be replaced by tournament_selection method
    parent_1, parent_2 = wheel_selection(total_remove, population)

    for i in range(int(total_remove / 2)):
        weight_1, weight_2 = uniform_crossover(parent_1[i].chromosome_1,
                                               parent_2[i].chromosome_1)
        bias_1, bias_2 = uniform_crossover(parent_1[i].chromosome_2,
                                           parent_2[i].chromosome_2)

        mutate(weight_1)
        mutate(weight_2)
        mutate(bias_1)
        mutate(bias_2)

        c1 = Individual()
        c2 = Individual()

        c1.chromosome_1 = weight_1
        c1.chromosome_2 = bias_1

        c2.chromosome_1 = weight_2
        c2.chromosome_2 = bias_2

        new_population.append(c1)
        new_population.append(c2)

    return new_population
def reproduce(population):
    """Applies the evolutionary survival process to the population.

    A few select elites with the best reward (fitness) is automatically part of the next generation.
    From the population parents are selected to create offspring. The offspring will be part of
    the next generation. The parents will cease to exist unless they were part of the elite.
    Mutation is applied to the offspring, which has a chance of altering the chromosomes.

    Args:
        The population which is a list of the object Individual. The entire population just ran in
        the simulation and their reward (fitness) was stored.

    Returns:
        A new population which is a list of the object Individual. The new population is the next generation
        and consists of the elite from the previous population and offspring from the previous generation.
    """

    # Sort the population to find out which had the best performance
    population = sorted(population, key=lambda x: x.reward, reverse=True)
    total_remove = 94

    # Elitism
    new_population = population[:(len(population) - total_remove)]

    parent_1, parent_2 = wheel_selection(total_remove, population)

    for i in range(int(total_remove / 2)):

        if random_offspring(parent_1[i], parent_2[i]):
            individual_1, individual_2 = create_random_offspring()
        else:
            rule_1, rule_2 = uniform_crossover_binary(parent_1[i].chromosome_1,
                                                      parent_2[i].chromosome_1)
            range_1, range_2 = uniform_crossover(parent_1[i].chromosome_2,
                                                 parent_2[i].chromosome_2)

            mutate(rule_1)
            mutate(rule_2)

            ca_mutate(range_1)
            ca_mutate(range_2)

            individual_1 = Individual()
            individual_2 = Individual()

            individual_1.chromosome_1 = rule_1
            individual_2.chromosome_1 = rule_2

            individual_1.chromosome_2 = range_1
            individual_2.chromosome_2 = range_2

        new_population.append(individual_1)
        new_population.append(individual_2)

    return new_population