-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
115 lines (85 loc) · 3.49 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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
from view.spot import Spot
from busca.search import a_star
from view.view import draw_map, draw_path, draw_hobbits
from settings import PASSOS_if, dict_etapas, hobbits_index
import pygame
from genetica.cromossomo import *
from statistics import mean
hobbits_por_etapa = dict()
(width, height) = (1000, 500)
def make_grid():
map_ = []
with open('./dados/mapa.txt', 'r') as f:
for i, linha in enumerate(f):
map_.append([])
for j,x in enumerate(linha):
spot = Spot(i, j, x, width, height)
map_[i].append(spot)
return map_
def get_checkpoint(grid, state):
for row in grid:
for spot in row:
if spot.state == state:
return spot
def main():
# IA
genetico = gera_populacao(50, 500)
print('Fitness medio: {}'.format(mean([c.tempo for c in genetico])))
melhor_cromo = genetico[0]
print('Tempo do melhor cromossomo: {}'.format(melhor_cromo.tempo))
print('Hobbits selecionados para cada etapa:')
nova_lista = []
for hobbits in melhor_cromo.lista:
novo_item = []
for idx, elemento in enumerate(hobbits):
if elemento == 1:
novo_item.append(hobbits_index[idx])
nova_lista.append(novo_item)
print("Dicionario de etapas {}".format(dict_etapas))
hobbits_por_etapa['1'] = ['Frodo', 'Merry', 'Pippin', 'Sam']
for idx, etapa in enumerate(dict_etapas.keys()):
print('{nome_etapa}: {lista}'.format(nome_etapa=etapa, lista=nova_lista[idx]))
hobbits_por_etapa[etapa] = nova_lista[idx]
#draw_hobbits(etapa, hobbits)
print("Lista de hobits por etapa")
print(hobbits_por_etapa)
# A*
idx = 0
grid = make_grid()
for row in grid:
for spot in row:
spot.update_neighbors(grid)
screen = pygame.display.set_mode((width, height))
screen.fill((255, 255, 255))
start = get_checkpoint(grid, PASSOS_if[idx][0])
end = get_checkpoint(grid, PASSOS_if[idx][1])
running = True
finalizado = False
while running:
draw_map(screen, grid)
draw_hobbits(screen, start.state, hobbits_por_etapa)
pygame.display.update()
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_RIGHT:
came_from = {}
if not finalizado and (start.state != '1' or (start.state == '1' and end.state == '2')):
draw_map(screen, grid)
pygame.display.update()
came_from = a_star(grid, start, end, screen)
draw_hobbits(screen, start.state, hobbits_por_etapa)
if idx < len(PASSOS_if) - 1:
idx+=1
else:
finalizado = True
draw_path(came_from, grid, end)
draw_map(screen, grid)
pygame.display.update()
if (came_from and not finalizado):
start = get_checkpoint(grid, PASSOS_if[idx][0])
end = get_checkpoint(grid, PASSOS_if[idx][1])
draw_path(came_from, grid, start)
if __name__ == '__main__':
main()