-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy patha_star.py
58 lines (51 loc) · 1.85 KB
/
a_star.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
import math
import pygame
from queue import PriorityQueue
from node import Node
HEIGHT, WIDTH = 900, 900
def reconstructPath(came_from, current, draw):
while current in came_from:
current = came_from[current]
current.makePath()
draw()
def huresticFunction(intermediate_node, end_node):
x1, y1 = intermediate_node
x2, y2 = end_node
return abs(x1 - x2) + abs(y1 - y2)
def aStar(draw, grid, start, end):
count = 0
priority_queue = PriorityQueue()
priority_queue.put((0, count, start))
came_from = {}
g_score = {node: math.inf for row in grid for node in row}
g_score[start] = 0
f_score = {node: math.inf for row in grid for node in row}
f_score[start] = huresticFunction(start.getPosition(), end.getPosition())
open_set = {start}
while not priority_queue.empty():
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
current = priority_queue.get()[2]
open_set.remove(current)
if current == end:
reconstructPath(came_from, end, draw)
return True
for neighbor in current.neighbors:
temp_g_score = g_score[current] + 1
if temp_g_score < g_score[neighbor]:
came_from[neighbor] = current
g_score[neighbor] = temp_g_score
f_score[neighbor] = temp_g_score + huresticFunction(
neighbor.getPosition(), end.getPosition()
)
if neighbor not in open_set:
count += 1
priority_queue.put((f_score[neighbor], count, neighbor))
open_set.add(neighbor)
if neighbor != end:
neighbor.makeVisiting()
draw()
if current != start:
current.makeVisited()
return False