Beispiel #1
0
def crossover_fitness_driven_one_point(p1, p2):
    point = random.randint(1, len(p1.gene_list) - 1)
    c1, c2 = copy.deepcopy(p1.gene_list), copy.deepcopy(p2.gene_list)
    c1[point:], c2[point:] = p2.gene_list[point:], p1.gene_list[point:]
    child1 = Individual(c1)
    child2 = Individual(c2)
    candidates = [child1, child2, p1, p2]

    best = sorted(candidates, key=lambda ind: ind.fitness, reverse=True)

    return best[0:2]
Beispiel #2
0
def mutation_fitness_driven_bit_flip(ind, max_tries=3):
    for t in range(0, max_tries):
        mut = copy.deepcopy(ind.gene_list)
        pos = random.randint(0, len(ind.gene_list) - 1)
        g1 = mut[pos]
        mut[pos] = (g1 + 1) % 2
        mutated = Individual(mut)
        if mutated.fitness > ind.fitness:
            return mutated
    return ind
import random

from ch9.knapsack.individual import Individual
from ch9.knapsack.random_set_generator import random_set_generator

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

    items = random_set_generator(1, 100, 0.1, 7, 200)
    Individual.set_items(items)
    Individual.set_max_weight(10)

    population = [Individual.create_random() for _ in range(1000)]
    average_weight = sum([ind.total_weight()
                          for ind in population]) / len(population)
    print(f'Average weight of population: {average_weight}')
Beispiel #4
0
def mutate(ind):
    mutated_gene = mutation_bit_flip(ind.gene_list)
    return Individual(mutated_gene)
Beispiel #5
0
from ch9.knapsack.random_set_generator import random_set_generator
from ch9.knapsack.toolbox import mutation_bit_flip


def mutate(ind):
    mutated_gene = mutation_bit_flip(ind.gene_list)
    return Individual(mutated_gene)


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

    random.seed(63)

    items = random_set_generator(1, 100, 0.1, 7, 200)
    Individual.set_items(items)
    Individual.set_max_weight(10)

    gene_set = [0] * len(items)
    inclusions = [2, 30, 34, 42, 48, 64, 85, 104, 113, 119, 157, 174]
    for i in inclusions:
        gene_set[i] = 1
    ind = Individual(gene_set)

    alive = 0
    killed = 0

    for _ in range(1000):
        mutated = mutate(ind)
        if mutated.fitness == 0:
            killed += 1
def create_random_individual(gene_len, zeros=1, ones=1):
    s = ([0] * zeros) + ([1] * ones)
    return Individual([random.choice(s) for _ in range(gene_len)])
import random

from ch9.knapsack.individual import Individual
from ch9.knapsack.random_set_generator import random_set_generator


def create_random_individual(gene_len, zeros=1, ones=1):
    s = ([0] * zeros) + ([1] * ones)
    return Individual([random.choice(s) for _ in range(gene_len)])


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

    items = random_set_generator(1, 100, 0.1, 7, 200)
    Individual.set_items(items)
    Individual.set_max_weight(10)

    population = [
        create_random_individual(len(items), 50, 1) for _ in range(1000)
    ]
    average_weight = sum([ind.total_weight()
                          for ind in population]) / len(population)
    print(f'Average weight of population: {average_weight}')
def crossover(parent1, parent2):
    child1_genes, child2_genes = crossover_one_point(parent1.gene_list, parent2.gene_list)
    return Individual(child1_genes), Individual(child2_genes)
    stats,
    plot_stats,
)


def crossover(parent1, parent2):
    child1_genes, child2_genes = crossover_one_point(parent1.gene_list, parent2.gene_list)
    return Individual(child1_genes), Individual(child2_genes)


def mutate(ind):
    mutated_gene = mutation_bit_flip(ind.gene_list)
    return Individual(mutated_gene)


Individual.set_items(get_items_from_my_room())
Individual.set_max_weight(10)

random.seed(63)
POPULATION_SIZE = 8
CROSSOVER_PROBABILITY = .7
MUTATION_PROBABILITY = .2
MAX_GENERATIONS = 20

first_population = [Individual.create_random() for _ in range(POPULATION_SIZE)]
population = first_population.copy()
fitness_list = [ind.fitness for ind in population]
fit_avg = [sum(fitness_list) / len(population)]
fit_best = [max(fitness_list)]
fit_best_ever = [max(fitness_list + fit_best)]
best_ind = random.choice(first_population)