return gene_constraints(new_g1), gene_constraints(new_g2)


def crossover(ind1, ind2):
    offspring_genes = crossover_blend(ind1.get_gene(), ind2.get_gene())
    return [
        create_individual(offspring_genes[0]),
        create_individual(offspring_genes[1])
    ]


if __name__ == '__main__':
    random.seed(30)

    p_1 = create_random_individual()
    p_2 = create_random_individual()

    offspring = crossover(p_1, p_2)

    c_1 = offspring[0]
    c_2 = offspring[1]

    x = np.linspace(MIN_BORDER, MAX_BORDER)
    plt.plot(x, fitness(x), '--', color='blue')
    plt.plot([p_1.get_gene(), p_2.get_gene()], [p_1.fitness, p_2.fitness],
             'o',
             markersize=15,
             color='orange')
    plt.plot([c_1.get_gene(), c_2.get_gene()], [c_1.fitness, c_2.fitness],
             's',
import random

from ch2.individual import create_random_individual


def select_tournament(population, tournament_size):
    new_offspring = []
    for _ in range(len(population)):
        candidates = [random.choice(population) for _ in range(tournament_size)]
        new_offspring.append(max(candidates, key = lambda ind: ind.fitness))
    return new_offspring


if __name__ == '__main__':

    random.seed(29)

    POPULATION_SIZE = 5

    generation_1 = [create_random_individual() for _ in range(POPULATION_SIZE)]
    generation_2 = select_tournament(generation_1, 3)

    print("Generation 1")
    [print(ind) for ind in generation_1]

    print("Generation 2")
    [print(ind) for ind in generation_2]
예제 #3
0
from ch2.crossover import crossover
from ch2.individual import create_random_individual
from ch2.mutate import mutate
from ch2.population import plot_population
from ch2.selection import select_tournament

if __name__ == '__main__':

    POPULATION_SIZE = 10
    CROSSOVER_PROBABILITY = .8
    MUTATION_PROBABILITY = .1
    MAX_GENERATIONS = 10

    random.seed(29)

    population = [create_random_individual() for _ in range(POPULATION_SIZE)]

    for generation_number in range(POPULATION_SIZE):
        # SELECTION
        selected = select_tournament(population, 3)
        # CROSSOVER
        crossed_offspring = []
        for ind1, ind2 in zip(selected[::2], selected[1::2]):
            if random.random() < CROSSOVER_PROBABILITY:
                children = crossover(ind1, ind2)
                crossed_offspring.append(children[0])
                crossed_offspring.append(children[1])
            else:
                crossed_offspring.append(ind1)
                crossed_offspring.append(ind2)
        # MUTATION
예제 #4
0

def mutate_gaussian(g, mu, sigma):
    mutated_gene = g + random.gauss(mu, sigma)
    return gene_constraints(mutated_gene)


def mutate(ind):
    return create_individual(mutate_gaussian(ind.get_gene(), 0, 1))


if __name__ == '__main__':

    random.seed(37)

    individual = create_random_individual()
    mutated = mutate(individual)

    x = np.linspace(MIN_BORDER, MAX_BORDER)
    plt.plot(x, fitness(x), '--', color = 'blue')
    plt.plot(
        [individual.get_gene()],
        [individual.fitness],
        'o', markersize = 20, color = 'orange'
    )
    plt.plot(
        [mutated.get_gene()],
        [mutated.fitness],
        's', markersize = 20, color = 'green'
    )
    plt.title("Circle : Before Mutation, Square: After Mutation")