-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathmain.py
More file actions
65 lines (46 loc) · 2.06 KB
/
main.py
File metadata and controls
65 lines (46 loc) · 2.06 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
import numpy as np
import ga
def main():
# entradas da equação
equation_inputs = [4, -2, 3.5, 5, -11, -4.7]
# número de pesos a otimizar
num_weights = 6
sol_per_pop = 8
# população tem sol_per_pop cromossomos com num_weights gens
pop_size = (sol_per_pop, num_weights)
# População inicial
new_population = np.random.uniform(low=-4.0, high=4.0, size=pop_size)
# Algoritmo genético
num_generations = 100
num_parents_mating = 4
for generation in range(num_generations):
print(f"Geração: {generation}")
# medir o ‘fitness’ de cada cromossomo na população
fitness = ga.cal_pop_fitness(equation_inputs, new_population)
print("Valores de fitness:")
print(fitness)
# Selecionar os melhores pais na população para o cruzamento
parents = ga.select_mating_pool(new_population, fitness, num_parents_mating)
print("Genitores selecionados:")
print(parents)
# formar a próxima geração usando crossover
offspring_crossover = ga.crossover(parents, offspring_size=(
pop_size[0] - parents.shape[0], num_weights
))
print("Resultado do crossover:")
print(offspring_crossover)
# adicionar variações aos filhos usando mutação
offspring_mutation = ga.mutation(offspring_crossover)
print("Resultado da mutação:")
print(offspring_mutation)
# criar a nova população baseada nos pais e filhos
new_population[0:parents.shape[0], :] = parents
new_population[parents.shape[0]:, :] = offspring_mutation
best_result = np.max(np.sum(new_population*equation_inputs, axis=1))
print(f"Melhor resultado depois da geração {generation}: {best_result}")
fitness = ga.cal_pop_fitness(equation_inputs, new_population)
best_match_idx = np.where(fitness == np.max(fitness))
print("Melhor solução: ", new_population[best_match_idx, :])
print("Fitness da melhor solução: ", fitness[best_match_idx])
if __name__ == '__main__':
main()