-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenetic_algorithm.py
More file actions
94 lines (71 loc) · 3.39 KB
/
genetic_algorithm.py
File metadata and controls
94 lines (71 loc) · 3.39 KB
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
import random
from utils.functions_utils import DIMENSIONS
from evolutionary_algorithm import EA
'''
a. Representação das soluções (indivíduos):
Lista de números reais de tamanho igual ao número de dimensões
b. Função de Fitness: 1 / (1 + f(x))
c. População (tamanho, inicialização):
2000, inicialização aleatória
d. Processo de seleção de pais:
Seleção dos 2 melhores indivíduos de 6 aleatórios
e. Operadores Genéticos (Recombinação e Mutação):
Recombinação aritmética e mutação gaussiana
f. Processo de seleção por sobrevivência:
Seleção dos 200 melhores indivíduos
g. Condições de término do Algoritmo Evolucionário :
100 iterações sem melhora ou 5000 iterações
'''
class GA(EA):
def __init__(self, function_name, mutation_prob = 0.05, max_iterations = 5000, crossover_prob = 0.9, population_size = 250,
selected_parents = 2):
super().__init__(function_name, max_iterations, population_size, selected_parents, crossover_prob, mutation_prob, population_size//2, DIMENSIONS)
def generate_initial_population(self):
population = []
boundary = self.get_boundaries()
for i in range(self.population_size):
individual = [random.uniform(-boundary, boundary) for _ in range(self.genoma_size)]
population.append(individual)
return population
def parent_selection(self, population):
selected_individuals = []
for _ in range(self.selected_parents*3):
selected_individuals.append(random.choice(population))
selected_individuals.sort(key=lambda individual : self.fitness(individual), reverse=True)
return selected_individuals[:self.selected_parents]
def crossover(self, parents):
[parent1, parent2] = parents
child1 = parent1.copy()
child2 = parent2.copy()
idx = random.randint(0, self.genoma_size - 1)
alpha = random.uniform(0,1)
child1[idx] = (alpha * parent1[idx] + (1 - alpha) * parent2[idx])
child2[idx] = (alpha * parent2[idx] + (1 - alpha) * parent1[idx])
return [child1, child2]
def mutation(self, population):
boundary = self.get_boundaries()
for individual in population:
for i in range(self.genoma_size):
if random.random() < self.mutation_prob:
individual[i] = random.gauss(0,1)
return population
def find_solution(self):
self.best_fitness = 0
self.best_fit_count = 0
self.it_best_fitness_list = []
self.it_fitness_mean_list = []
population = self.generate_initial_population()
for i in range(self.max_iterations):
for _ in range(self.number_of_crossovers):
parents = self.parent_selection(population)
children = parents
if random.random() < self.crossover_prob:
children = self.crossover(parents)
children = self.mutation(children)
population.extend(children)
population = self.survival_selection(population)
print(f"Iteration : {i}, Fitness : {self.fitness(population[0])}" )
self.update_data(population)
if self.check_stopping_criteria():
break
return population, i