-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
76 lines (59 loc) · 2.26 KB
/
main.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
'''This file is for calling the triangle classification'''
import pandas as pd
from fitness_evaluation import score_triangle_classification, classify_triangle
from genetic_algorithm import GeneticAlgorithm
def main():
'''Main function'''
ga = GeneticAlgorithm()
problem = 'triangle-classification'
expected_solution = 'equilateral'
if problem == 'triangle-classification':
get_fitness_score = score_triangle_classification
# 1) Initialize population
initial_pop = ga.init_pop()
generation = 1
# 2) Calculate fitness score for the current population
for chromo in range(ga.pop_size):
evaluated_chromo = get_fitness_score(initial_pop[chromo], expected_solution)
ga.current_pop.append(evaluated_chromo)
# 3) Loop until num_generations is reached
while generation <= ga.num_generations:
# 3.1) Select best chromosomes from the current population
selected = ga.selection(ga.current_pop)
print('Selected:', selected)
# 3.2) Makes new generation
crossovered = ga.crossover(selected, ga.current_pop)
print('Crossovered:', crossovered)
# 3.3) Diversification
mutated = ga.mutate(crossovered)
print('Mutated:', mutated)
# 3.3.1) Get fitness score for mutated chromosomes
new_pop = []
for mutated_chromo in mutated:
evaluated_chromo = get_fitness_score(mutated_chromo, expected_solution)
new_pop.append(evaluated_chromo)
# 3.4) Replace chromosome with new_pop chromosome if new_pop have better fitness score
ga.current_pop = ga.replace(new_pop, ga.current_pop)
show_generation(ga, generation)
generation+=1
def show_generation(ga, generation):
'''Prints the current gen'''
print('GENERATION '+ str(generation))
data = {
"Fitness score": [],
"Classification": [],
"Chromosome": [],
}
df = pd.DataFrame(data)
for element in ga.current_pop:
chromo = element[0]
triangle = [chromo[0],chromo[1],chromo[2]]
df.loc[len(df.index)] = [
str(element[1]),
classify_triangle(triangle),
str(chromo),
]
print(df.to_string())
print('-'*100)
if __name__ == '__main__':
main()