/
Controller.py
93 lines (71 loc) · 3.09 KB
/
Controller.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
from movement import maze_value
from specimen import Specimen
from maze_env import Maze
from genetic_algorithm import GeneticAlgorithm
from population import Population
class Controller:
def __init__(self):
pass
self.env = Maze(self)
self.env.mainloop()
def move_view_truck(self, action):
self.env.step(action)
self.env.render()
def main_show_maze(self, pop, cross, mut, max_generation, elitismo):
# vars
solution = False
generation = 0
mut = float(mut)
cross = float(cross)
elitismo = True if str(elitismo) == '1' else False
# Cria a população
##########################################3
population = Population(tamPop=int(pop))
population.create_random_population()
# Cria o algoritmo genetico
algoritmo = GeneticAlgorithm(crossover=cross, mutation=mut, elitismo=elitismo)
while solution == False and generation<=int(max_generation):
generation += 1
population = algoritmo.create_new_generation(current_population=population)
best_specimen = population.getSpeciemen(0)
# Reseta a tela e mostra o caminho do melhor cromossomo
######################################################################
if generation % 10 == 0:
self.env.reset()
self.env.after(100, self.show_path_on_maze(best_specimen=best_specimen))
solution = algoritmo.get_solution(best_specimen.genetic_code)
conteudo = '\nCódigo={} \nFitness={} \nGeração={}\nRota=[{}]'.format(best_specimen.genetic_code, best_specimen.fitness, generation,best_specimen.route)
self.env.delete()
self.env.escreve(conteudo)
print(best_specimen.genetic_code, best_specimen.fitness, best_specimen.route, generation)
if solution == True:
conteudo = 'Solução ótima!!' \
'\nCódigo={} ' \
'\nFitness={} ' \
'\nGeração={}'.format(best_specimen.genetic_code,
best_specimen.fitness, generation)
self.env.delete()
self.env.escreve(conteudo)
print(best_specimen.genetic_code, best_specimen.fitness, best_specimen.route, generation)
i = 0
while i <= 10:
i+=1
self.env.reset()
self.env.after(100, self.show_path_on_maze(best_specimen=best_specimen))
def show_path_on_maze(self, best_specimen):
specimen = best_specimen
start = 0
end = 2
for i in range(26):
direction = specimen.genetic_code[start:end]
start += 2
end += 2
action = maze_value[direction]
self.move_view_truck(action)
if __name__ == "__main__":
algorithm = GeneticAlgorithm(0.7, 0.2, 1)
population = Population(2000)
population.create_random_population()
generations_limit = 120
controler = Controller()
#controler.main_show_maze(5000, 8.6, 0.5, 1000, 1)