-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbuscas.py
215 lines (190 loc) · 8.13 KB
/
buscas.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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
import networkx as nx
import matplotlib
import matplotlib.pyplot as plt
import os
matplotlib.use('Agg')
class Buscas(object):
"""
Classe de buscas para utlizar buscas cegas
"""
def __init__(self):
self.initial_node = ''
self.finish_node = ''
self.nodes = {}
self.edges_cost = {}
self.__node_sons = {}
def __getitem__(self, item):
return {"largura": self.busca_largura(),
"profunda": self.busca_profundidade(),
"dijkstra": self.busca_dijkstra(),
"gulosa": self.busca_gulosa(),
"estrela": self.busca_a_estrela()
}[
item]
def reset_values(self):
self.initial_node = ''
self.finish_node = ''
self.nodes = {}
self.edges_cost = {}
self.__node_sons = {}
def __generate_node_sons(self):
for n1, n2 in list(self.edges_cost.keys()):
if n1 not in self.__node_sons:
self.__node_sons[n1] = {n2: self.edges_cost[(n1, n2)]}
else:
self.__node_sons[n1].update({n2: self.edges_cost[(n1, n2)]})
def __generate_next_node(self, node, jump_node):
node_name = list(node.keys())[0]
node_childrens = self.__node_sons[node_name]
if node_name not in jump_node:
node_value = node[node_name][0]
path = node[node_name][1]
insert_sons_formated = [
{key: (
node_childrens[key] + node_value,
path + " " + node_name)}
for key in node_childrens
if key not in jump_node
]
else:
insert_sons_formated = []
return insert_sons_formated
def busca_largura(self):
self.__generate_node_sons()
next_node = self.initial_node
dict_node = {}
borda = [{n: (self.__node_sons[next_node][n], self.initial_node)} for n in self.__node_sons[next_node]]
visiteds = [self.initial_node]
while next_node != self.finish_node:
node = borda.pop(0)
insert_sons_formated = self.__generate_next_node(node, visiteds)
borda += insert_sons_formated
dict_node = node
next_node = list(node.keys())[0]
visiteds.append(next_node)
path = (dict_node[self.finish_node][1] + " " + self.finish_node).split()
cost = dict_node[self.finish_node][0]
return path, cost
def busca_profundidade(self):
self.__generate_node_sons()
next_node = self.initial_node
dict_node = {}
borda = [{n: (self.__node_sons[next_node][n], self.initial_node)} for n in self.__node_sons[next_node]]
visiteds = [self.initial_node]
while next_node != self.finish_node:
node = borda.pop()
insert_sons_formated = self.__generate_next_node(node, visiteds)
borda += insert_sons_formated
dict_node = node
next_node = list(node.keys())[0]
visiteds.append(next_node)
path = (dict_node[self.finish_node][1] + " " + self.finish_node).split()
cost = dict_node[self.finish_node][0]
return path, cost
def busca_dijkstra(self):
self.__generate_node_sons()
next_node = self.initial_node
dict_node = {}
borda = [{n: (self.__node_sons[next_node][n], self.initial_node)} for n in self.__node_sons[next_node]]
borda = sorted(borda, key=self.__sort_dijkstra)
visiteds = [self.initial_node]
while next_node != self.finish_node:
node = borda.pop(0)
insert_sons_formated = self.__generate_next_node(node, visiteds)
borda += insert_sons_formated
borda = sorted(borda, key=self.__sort_dijkstra)
dict_node = node
next_node = list(node.keys())[0]
visiteds.append(next_node)
path = (dict_node[self.finish_node][1] + " " + self.finish_node).split()
cost = dict_node[self.finish_node][0]
return path, cost
def busca_a_estrela(self):
self.__generate_node_sons()
next_node = self.initial_node
dict_node = {}
borda = [{n: (self.__node_sons[next_node][n], self.initial_node)} for n in
self.__node_sons[next_node]]
borda = sorted(borda, key=self.__sort_a_estrela)
visiteds = [self.initial_node]
while next_node != self.finish_node:
node = borda.pop(0)
insert_sons_formated = self.__generate_next_node(node, visiteds)
borda += insert_sons_formated
borda = sorted(borda, key=self.__sort_a_estrela)
dict_node = node
next_node = list(node.keys())[0]
visiteds.append(next_node)
path = (dict_node[self.finish_node][1] + " " + self.finish_node).split()
cost = dict_node[self.finish_node][0]
return path, cost
def busca_gulosa(self):
self.__generate_node_sons()
next_node = self.initial_node
dict_node = {}
borda = [{n: (self.__node_sons[next_node][n], self.initial_node)} for n in
self.__node_sons[next_node]]
borda = sorted(borda, key=self.__sort_gulosa)
visiteds = [self.initial_node]
while next_node != self.finish_node:
node = borda.pop(0)
insert_sons_formated = self.__generate_next_node(node, visiteds)
borda += insert_sons_formated
borda = sorted(borda, key=self.__sort_gulosa)
dict_node = node
next_node = list(node.keys())[0]
visiteds.append(next_node)
path = (dict_node[self.finish_node][1] + " " + self.finish_node).split()
cost = dict_node[self.finish_node][0]
return path, cost
def __sort_dijkstra(self, node_aux):
key = list(node_aux.keys())[0]
return node_aux[key][0]
def __sort_a_estrela(self, node_aux):
key = list(node_aux.keys())[0]
return node_aux[key][0] + self.nodes[key]
def __sort_gulosa(self, node_aux):
key = list(node_aux.keys())[0]
return self.nodes[key]
def gerar_grafico(self, caminho, nome, use_digraph):
if use_digraph:
graph = nx.DiGraph()
else:
graph = nx.Graph()
# Inicia a lista com as duas primeiras edges
nos_resultados = [(caminho[0], caminho[1])]
# Continua a lista de edges a partir do terceiro elemento em diante de 2 em 2
for r in range(2, len(caminho), 2):
nos_resultados.append((caminho[r - 1], caminho[r]))
nos_resultados.append((caminho[r], caminho[r - 1]))
# Insere os nós no objeto
for node in list(self.nodes.keys()):
graph.add_node(node)
# lista para pular os nós de mesmo par já inseridos na lista de Edges
# Exemplo: (1, 2) ele irá pular o (2, 1)
pular = []
for n1, n2 in list(self.edges_cost.keys()):
# Faz a verificação de pulo descrito acima
if (n1, n2) not in pular:
# Verifica se o edge é um caminho até o nó final, se for colore a linha de vermelho, se não colore de
# azul
if n1 in caminho and n2 in caminho:
color = 'r'
else:
color = 'b'
graph.add_edge(n1, n2, color=color, weight=self.edges_cost[(n1, n2)]/100)
pular.append((n2, n1))
# Cria o layout em que o grafo será plotado
pos = nx.kamada_kawai_layout(graph)
# cria um array com as cores de cada edge
colors = [graph[u][v]['color'] for u, v in graph.edges]
# desenha o grafo com as configurações feitas acima
nx.draw(graph, pos, edge_color=colors, width=1, with_labels=True)
# realiza umas configurações adicionais nos edges
nx.draw_networkx_edge_labels(graph, pos, edge_labels=self.edges_cost)
# verifica se o arquivo já existe com o mesmo nome e se existir exclui e então salva o novo.
if not os.path.isdir("static/files"):
os.mkdir("static/files")
plt.savefig("static/files/" + nome)
plt.close()
self.reset_values()