This repository was archived by the owner on Jul 18, 2023. It is now read-only.
forked from mauriciopereira74/Vector-Race
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGraph.py
More file actions
635 lines (538 loc) · 21.7 KB
/
Graph.py
File metadata and controls
635 lines (538 loc) · 21.7 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
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
# Classe Graph para representação de Grafos
import math
from queue import Queue
import networkx as nx # biblioteca de tratamento de grafos necessária para desnhar graficamente o grafo
import matplotlib.pyplot as plt # idem
from Node import Node
class Grafo:
def __init__(self, directed=False):
self.m_nodes = []
self.m_directed = directed
self.m_graph = {} # dicionario para armazenar os nodos e arestas
self.m_h = {} # dicionario para posteriormente armazenar as heuristicas para cada nodo -> pesquisa informada
self.nestedCircuito = None
# Escrever o grafo como String
def __str__(self):
out = ""
for key in self.m_graph.keys():
out = out + "node" + str(key) + ": " + str(self.m_graph[key]) + "\n"
return out
# Encontrar nodo pelo nome
def get_node_by_name(self, name):
search_node = Node(name)
for node in self.m_nodes:
if node == search_node:
return node
else:
return None
# Imprimir arestas
def imprime_aresta(self):
listaA = ""
lista = self.m_graph.keys()
for nodo in lista:
for (nodo2, custo) in self.m_graph[nodo]:
listaA = listaA + nodo + " ->" + nodo2 + "\n" # + " custo:" + str(custo) + "\n"
return listaA
# Adicionar aresta no grafo
def add_edge(self, node1, node2, weight):
n1 = Node(node1)
n2 = Node(node2)
if (n1 not in self.m_nodes):
self.m_nodes.append(n1)
self.m_graph[node1] = list()
else:
n1 = self.get_node_by_name(node1)
if (n2 not in self.m_nodes):
self.m_nodes.append(n2)
self.m_graph[node2] = list()
else:
n2 = self.get_node_by_name(node2)
self.m_graph[node1].append((node2, weight))
if not self.m_directed:
self.m_graph[node2].append((node1, weight))
# Devolver nodos do Grafo
def getNodes(self):
return self.m_nodes
# Devolver o custo de uma aresta
def get_arc_cost(self, node1, node2):
custoT = math.inf
a = self.m_graph[node1] # lista de arestas para aquele nodo
nodeInA = [item for item in a if item[0] == node2]
if len(nodeInA) == 0:
custoT = 1
else:
for (nodo, custo) in a:
if nodo == node2:
custoT = custo
return custoT
# Algoritmo de Procura DFS
def procura_DFS(self, start, end, path=None, visited=None):
if path == None:
path = []
if visited == None:
visited = set()
path.append(start)
visited.add(start)
if start == end:
# calcular o custo do caminho funçao calcula custo.
custoT = self.calcula_custo(path)
return (path, custoT)
for (adjacente, peso) in self.m_graph[start]:
# print(f"adjacentes : {adjacente}\visited : {visited}")
if peso!=25 and adjacente not in visited:
resultado = self.procura_DFS(adjacente, end, path, visited)
if resultado is not None:
return resultado
path.pop() # se nao encontra remover o que está no caminho......
return None
# Algoritmo de Procura BFS
def procura_BFS(self, start, end):
# definir nodos visitados para evitar ciclos
visited = set()
fila = Queue()
# adicionar o nodo inicial à fila e aos visitados
fila.put(start)
visited.add(start)
# garantir que o start node nao tem pais...
parent = dict()
parent[start] = None
path_found = False
while not fila.empty() and path_found == False:
nodo_atual = fila.get()
if nodo_atual == end:
path_found = True
else:
for (adjacente, peso) in self.m_graph[nodo_atual]:
if peso!=25 and adjacente not in visited:
fila.put(adjacente)
parent[adjacente] = nodo_atual
visited.add(adjacente)
# Reconstruir o caminho
path = []
if path_found:
path.append(end)
while parent[end] is not None:
path.append(parent[end])
end = parent[end]
path.reverse()
# funçao calcula custo caminho
custo = self.calcula_custo(path)
return (path, custo)
# Desenha grafo em modo grafico
def desenha(self):
##criar lista de vertices
lista_v = self.m_nodes
lista_a = []
g = nx.Graph()
for nodo in lista_v:
n = nodo.getName()
g.add_node(n)
for (adjacente, peso) in self.m_graph[n]:
lista = (n, adjacente)
# lista_a.append(lista)
g.add_edge(n, adjacente, weight=peso)
pos = nx.spring_layout(g)
nx.draw_networkx(g, pos, with_labels=True, font_weight='bold')
labels = nx.get_edge_attributes(g, 'weight')
nx.draw_networkx_edge_labels(g, pos, edge_labels=labels)
plt.draw()
plt.show()
# Define heuristica para cada nodo
def add_heuristica(self, n, estima):
n1 = Node(n)
if n1 in self.m_nodes:
self.m_h[n] = estima
# Heuristica -> define heuristica para cada nodo 1 por defeito
# Nota -> apenas para teste de pesquisa informada
def heuristica(self):
nodos = self.m_graph.keys()
for s in nodos:
print(s)
# self.m_h[n] = 1
return (True)
def getDistance(self, i, f):
x, y = i
xf, yf = f
dx = abs(x - xf)
dy = abs(y - yf)
return 1 * (dx + dy) - min(dx, dy) # D * (dx + dy) + (D2 - 2 * D) * min(dx, dy)
##########################################
def PStrTuple(self, pontoString):
res = pontoString[1:-1]
res = res.split(',')
resint = list(map(int, res))
tup = (resint[0], resint[1])
return tup
def heuristica_aStar(self, nFinal):
nodos = self.m_graph.keys()
for n in nodos:
if n == nFinal:
self.m_h[n] = 0
else: self.m_h[n] = self.getDistance(self.PStrTuple(n), self.PStrTuple(nFinal)) + 1
return (True)
def calcula_custo(self, caminho):
# caminho é uma lista de nodos
teste = caminho
custo = 0
i = 0
while i + 1 < len(teste):
custo = custo + self.get_arc_cost(teste[i], teste[i + 1])
#print(teste[i])
i = i + 1
return custo
def calcula_est(self, estima):
l = list(estima.keys())
min_estima = estima[l[0]]
node = l[0]
for k, v in estima.items():
if v < min_estima:
min_estima = v
node = k
# elif v == min_estima:
# if self.m_h[node] > self.m_h[k]:
# min_estima = v
# node = k
return node
# Devolve heuristica do nodo
def getH(self, nodo):
if nodo not in self.m_h.keys():
return 1000
else:
return (self.m_h[nodo])
def add_circuito(self, nestedCircuitoooo):
self.nestedCircuito = nestedCircuitoooo
def PStringtoArr(self, pontoString):
res = pontoString[1:-1]
res = res.split(',')
resint = list(map(int, res))
return resint
def barreirasBetween(self, p1x, p1y, p2x, p2y):
tabuleiro = self.nestedCircuito
# Check for trivial cases
if p1x == p2x and p1y == p2y:
return True
if p1x == p2x:
miny = min(p1y, p2y)
maxy = max(p1y, p2y)
for y in range(miny, maxy+1):
if tabuleiro[p1x][y] == 'X':
return False
return True
if p1y == p2y:
minx = min(p1x, p2x)
maxx = max(p1x, p2x)
for x in range(minx, maxx+1):
if tabuleiro[x][p1y] == 'X':
return False
return True
# Use Bresenham's line algorithm to draw a line between the two points
dx = abs(p2x - p1x)
dy = abs(p2y - p1y)
sx = 1 if p1x < p2x else -1
sy = 1 if p1y < p2y else -1
err = dx - dy
while True:
if tabuleiro[p1x][p1y] == 'X':
return False
if p1x == p2x and p1y == p2y:
break
e2 = err * 2
if e2 > -dy:
err -= dy
p1x += sx
if e2 < dx:
err += dx
p1y += sy
return True
def ArrToVString(self, velArr):
res = "(" + str(velArr[0]) + "," + str(velArr[1]) + ")"
return res
# Função que devolve vizinhos de um nó
# def getNeighboursVel(self, nodo, vel):
# lista = []
# x = self.PStringtoArr(nodo)
# xV, yV = vel
# velocidades = []
#
# if xV < 0 and yV < 0:
# for x in range(xV-1, 0):
# for y in range(yV-1, 0):
# velocidades.append([x,y])
# elif xV > 0 and yV > 0:
# for x in range(0, xV+2):
# for y in range(0, yV+2):
# velocidades.append([x,y])
# elif xV < 0 and yV > 0:
# for x in range(xV-1, 0):
# for y in range(0, yV+2):
# velocidades.append([x,y])
# elif xV > 0 and yV < 0:
# for x in range(0, xV+2):
# for y in range(yV-1, 0):
# velocidades.append([x,y])
# elif xV == 0 and yV == 0:
# for x in range(-1, 2):
# for y in range(-1, 2):
# velocidades.append([x,y])
# elif xV == 0 and yV < 0:
# for x in range(-1, 2):
# for y in range(yV-1, 0):
# velocidades.append([x,y])
# elif xV == 0 and yV > 0:
# for x in range(-1, 2):
# for y in range(0, yV+2):
# velocidades.append([x,y])
# elif xV < 0 and yV == 0:
# for x in range(xV-1, 0):
# for y in range(-1, 1):
# velocidades.append([x,y])
# elif xV > 0 and yV == 0:
# for x in range(0, xV+2):
# for y in range(-1, 1):
# velocidades.append([x,y])
#
# for idx, x in enumerate(velocidades):
# if x == [0,0]:
# velocidades.pop(idx)
#
# listaPsArr = []
# for v in velocidades:
# listaPsArr.append([int(x[0])+v[0],int(x[1])+v[1]])
#
# listaPs = []
# for p in listaPsArr:
# listaPs.append(self.ArrToVString(p))
#
# for p in listaPs:
# ret = False
# if p in self.m_graph:
# golo = self.PStringtoArr(p)
# if self.barreirasBetween(x[0], x[1], golo[0], golo[1]):
# for adjs in self.m_graph[nodo]:
# adj, peso = adjs
# if adj == p:
# lista.append((adj, peso))
# ret = True
# break
# if not ret:
# lista.append((p, 1))
#
# return lista
# Função que devolve vizinhos de um nó
def getNeighboursVel(self, nodo, vel):
lista = []
x = self.PStringtoArr(nodo)
for xx in range(x[0], x[0]+vel):
for yy in range(x[1], x[1]+vel):
if f'({xx},{yy})' in self.m_graph:
for (adjacente, peso) in self.m_graph[f'({xx},{yy})']:
# if peso!=25:
if self.barreirasBetween(x[0], x[1], xx, yy):
lista.append((adjacente, peso))
return lista
# Algoritmo A*
def procura_aStar_wVelocity(self, start, end):
# open_list is a list of nodes which have been visited, but who's neighbors
# haven't all been inspected, starts off with the start node
# closed_list is a list of nodes which have been visited
# and who's neighbors have been inspected
closed_list_a = set([])
open_list = {start}
# velocidade = (0,0)
velocidade = 1
# g contains current distances from start_node to all other nodes
# the default value (if it's not found in the map) is +infinity
g = {}
g[start] = 0
# parents contains an adjacency map of all nodes
parents = {}
parents[start] = start
n = None
while len(open_list) > 0:
# find a node with the lowest value of f() - evaluation function
calc_heurist = {}
flag = 0
for v in open_list:
if n == None:
n = v
else:
flag = 1
calc_heurist[v] = (g[v] + self.getH(v))#/velocidade
if flag == 1:
min_estima = self.calcula_est(calc_heurist)
n = min_estima
if n == None:
print('Path does not exist!')
return None
# if the current node is the stop_node
# then we begin reconstructin the path from it to the start_node
if n == end:
reconst_path = []
while parents[n] != n:
reconst_path.append(n)
n = parents[n]
reconst_path.append(start)
reconst_path.reverse()
#print('Path found: {}'.format(reconst_path))
return (reconst_path, self.calcula_custo(reconst_path))
# for all neighbors of the current node do
for (m, weight) in self.getNeighboursVel(n, velocidade): # definir função getneighbours tem de ter um par nodo peso
# if the current node isn't in both open_list and closed_list
# add it to open_list and note n as it's parent
if m not in open_list and m not in closed_list_a:
open_list.add(m)
parents[m] = n
g[m] = g[n] + weight
# otherwise, check if it's quicker to first visit n, then m
# and if it is, update parent data and g data
# and if the node was in the closed_list, move it to open_list
else:
if g[m] > g[n] + weight:
g[m] = g[n] + weight
parents[m] = n
if m in closed_list_a:
closed_list_a.remove(m)
open_list.add(m)
# velocidade = tuple(map(lambda i, j: i + j, velocidade, (1,1)))
velocidade += 1
# remove n from the open_list, and add it to closed_list
# because all of his neighbors were inspected
open_list.remove(n)
closed_list_a.add(n)
print('Path does not exist!')
return None
# Função que devolve vizinhos de um nó
def getNeighbours(self, nodo):
lista = []
for (adjacente, peso) in self.m_graph[nodo]:
lista.append((adjacente, peso))
return lista
# Algoritmo A*
def procura_aStar(self, start, end):
# open_list is a list of nodes which have been visited, but who's neighbors
# haven't all been inspected, starts off with the start node
# closed_list is a list of nodes which have been visited
# and who's neighbors have been inspected
closed_list_a = set([])
open_list = {start}
# g contains current distances from start_node to all other nodes
# the default value (if it's not found in the map) is +infinity
g = {}
g[start] = 0
# parents contains an adjacency map of all nodes
parents = {}
parents[start] = start
n = None
while len(open_list) > 0:
# find a node with the lowest value of f() - evaluation function
calc_heurist = {}
flag = 0
for v in open_list:
if n == None:
n = v
else:
flag = 1
calc_heurist[v] = g[v] + self.getH(v) * (1 + 1/1000)
if flag == 1:
min_estima = self.calcula_est(calc_heurist)
n = min_estima
if n == None:
print('Path does not exist!')
return None
# if the current node is the stop_node
# then we begin reconstructin the path from it to the start_node
if n == end:
reconst_path = []
while parents[n] != n:
reconst_path.append(n)
n = parents[n]
reconst_path.append(start)
reconst_path.reverse()
#print('Path found: {}'.format(reconst_path))
return (reconst_path, self.calcula_custo(reconst_path))
# for all neighbors of the current node do
for (m, weight) in self.getNeighbours(n): # definir função getneighbours tem de ter um par nodo peso
# if the current node isn't in both open_list and closed_list
# add it to open_list and note n as it's parent
if m not in open_list and m not in closed_list_a:
open_list.add(m)
parents[m] = n
g[m] = g[n] + weight
# otherwise, check if it's quicker to first visit n, then m
# and if it is, update parent data and g data
# and if the node was in the closed_list, move it to open_list
else:
if g[m] > g[n] + weight:
g[m] = g[n] + weight
parents[m] = n
if m in closed_list_a:
closed_list_a.remove(m)
open_list.add(m)
# remove n from the open_list, and add it to closed_list
# because all of his neighbors were inspected
open_list.remove(n)
closed_list_a.add(n)
print('Path does not exist!')
return None
def heuristica_greedy(self, nFinal):
nodos = self.m_graph.keys()
for n in nodos:
if n == nFinal:
self.m_h[n] = 0
else: self.m_h[n] = self.getDistance(self.PStrTuple(n), self.PStrTuple(nFinal))
return (True)
# def shortenClosedListToCollision_a(self, collisionPoint):
# global closed_list_a
# tempList = list(closed_list_a)
# pointIndex = tempList.index(collisionPoint)
# closed_list_a = set(tempList[:pointIndex])
# def shortenClosedListToCollision_greedy(self, collisionPoint):
# global closed_list_greedy
# tempList = list(closed_list_greedy)
# pointIndex = tempList.index(collisionPoint)
# closed_list_greedy = set(tempList[:pointIndex])
# Algoritmo Greedy
def greedy(self, start, end):
# open_list é uma lista de nodos visitados, mas com vizinhos
# que ainda não foram todos visitados, começa com o start
# closed_list é uma lista de nodos visitados
# e todos os seus vizinhos também já o foram
closed_list_greedy = set([])
open_list = set([start])
# parents é um dicionário que mantém o antecessor de um nodo
# começa com start
parents = {}
parents[start] = start
while len(open_list) > 0:
n = None
# encontraf nodo com a menor heuristica
for v in open_list:
if n == None or self.m_h[v] < self.m_h[n]:
n = v
if n == None:
print('Path does not exist!')
return None
# se o nodo corrente é o destino
# reconstruir o caminho a partir desse nodo até ao start
# seguindo o antecessor
if n == end:
reconst_path = []
while parents[n] != n:
reconst_path.append(n)
n = parents[n]
reconst_path.append(start)
reconst_path.reverse()
return (reconst_path, self.calcula_custo(reconst_path))
# para todos os vizinhos do nodo corrente
for (m, weight) in self.getNeighbours(n):
# Se o nodo corrente nao esta na open nem na closed list
# adiciona-lo à open_list e marcar o antecessor
if weight!=25 and m not in open_list and m not in closed_list_greedy:
open_list.add(m)
parents[m] = n
# remover n da open_list e adiciona-lo à closed_list
# porque todos os seus vizinhos foram inspecionados
open_list.remove(n)
closed_list_greedy.add(n)
print('Path does not exist!')
return None