Exemplo n.º 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
Exemplo n.º 3
0
def generate_individual():
    """Creates a sequence of rule sets and pack it into the object Individual.

    An Individual consists of two chromosomes. The first chromosome consists of
    CA rules. The second chromosome consists of the ranges used within the CA.

    Returns:
        The object Individual.
    """
    size = random.randint(2, 5)
    individual = Individual()

    for i in range(size):
        gene = random.randint(0, 255)
        individual.chromosome_1.append(gene)

    individual.chromosome_2 = generate_ca_cut()

    return individual
Exemplo n.º 4
0
def initial_population():
    """Initiates a population of individuals with random chromosomes.

    Returns:
        A list of the object Individual.
    """
    individuals = []

    for j in range(100):

        sequence_size = random.randint(2, 5)
        individual = Individual()

        for i in range(sequence_size):

            gene = random.randint(0, 255)
            individual.chromosome_1.append(gene)

        individual.chromosome_2 = generate_ca_cut()
        individuals.append(individual)

    return individuals