-
Notifications
You must be signed in to change notification settings - Fork 1
/
lib.py
76 lines (56 loc) · 3.1 KB
/
lib.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import random
def initialize_population(size, alphabet, chromosome_length):
population = []
for _ in range(size):
chromosome = random.choices(alphabet, k=chromosome_length)
population += [chromosome]
return population
def tournament_selection(population, tournament_size, fitness_function):
selected_parents = []
for _ in range(2):
participants = random.sample(population, tournament_size)
best_chromosome = min(participants, key=fitness_function)
selected_parents += [best_chromosome]
return selected_parents[0], selected_parents[1]
def single_point_crossover(parent0, parent1):
chromosome_length = len(parent0)
crossover_point = random.randint(1, chromosome_length - 1)
child0 = parent0[:crossover_point] + parent1[crossover_point:]
child1 = parent1[:crossover_point] + parent0[crossover_point:]
return child0, child1
def mutation(chromosome, alphabet, rate):
for i in range(len(chromosome)):
if random.random() < rate:
chromosome[i] = random.choice(alphabet)
return chromosome
def genetics_algorithm(fitness_function, generations, mutation_rate, population_size, chromosome_length, alphabet,
tournament_size, chromosome_representation):
population = initialize_population(population_size, alphabet, chromosome_length)
for generation in range(generations):
new_population = []
for _ in range(population_size):
parent0, parent1 = tournament_selection(population, tournament_size, fitness_function)
child0, child1 = single_point_crossover(parent0, parent1)
child0, child1 = mutation(child0, alphabet, mutation_rate), mutation(child1, alphabet, mutation_rate)
new_population += [child0] + [child1]
population = new_population
best_chromosome = min(population, key=fitness_function)
best_fitness = fitness_function(best_chromosome)
print(f"Generation {generation + 1}:")
print(f"\tBest Fitness = {best_fitness}")
print(f"\tBest Chromosome = {chromosome_representation(best_chromosome)}")
def genetics_algorithm_with_callback(fitness_function, generations, mutation_rate, population_size, chromosome_length,
alphabet,
tournament_size, chromosome_representation, callback):
population = initialize_population(population_size, alphabet, chromosome_length)
for generation in range(generations):
new_population = []
for _ in range(int(population_size / 2)):
parent0, parent1 = tournament_selection(population, tournament_size, fitness_function)
child0, child1 = single_point_crossover(parent0, parent1)
child0, child1 = mutation(child0, alphabet, mutation_rate), mutation(child1, alphabet, mutation_rate)
new_population += [child0] + [child1]
population = new_population
best_chromosome = min(population, key=fitness_function)
best_fitness = fitness_function(best_chromosome)
callback(generation, best_fitness, best_chromosome)