Repository navigation
Expand file tree
/
Copy patha_star_algorithm.py
More file actions
99 lines (64 loc) · 2.61 KB
/
Copy patha_star_algorithm.py
File metadata and controls
99 lines (64 loc) · 2.61 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
import pygame
import heapq
PURPLE=(119,107,219)
ORANGE=(255,152,0)
def getNeighbors(grid,node,rows,cols):
neighbors=[]
row,col=node.row,node.col
if row >0 and not grid[row-1][col].is_wall:
neighbors.append(grid[row-1][col])
if row < rows-1 and not grid[row+1][col].is_wall:
neighbors.append(grid[row+1][col])
if col >0 and not grid[row][col-1].is_wall:
neighbors.append(grid[row][col-1])
if col < cols-1 and not grid[row][col+1].is_wall:
neighbors.append(grid[row][col+1])
return neighbors
def reconstructPath(cameFrom,current,drawFunc):
while current in cameFrom:
current=cameFrom[current]
if not current.is_start:
current.color=ORANGE
drawFunc()
pygame.time.delay(20)
def heuristic(nodeA,nodeB):
x1,y1=nodeA.row,nodeA.col
x2,y2=nodeB.row,nodeB.col
return abs(x1-x2)+abs(y1-y2)
def aStar(drawFunc,grid,start,goal,rows,cols):
count=0
explore_nodes=0
openSet=[]
heapq.heappush(openSet,(0,count,start))
cameFrom={}
gScore={node:float("inf") for row in grid for node in row}
gScore[start]=0
fScore={node:float("inf") for row in grid for node in row}
fScore[start]=heuristic(start,goal)
openSetHash={start}
while openSet:
for event in pygame.event.get():
if event.type== pygame.QUIT:
pygame.quit()
return False
current=heapq.heappop(openSet)[2]
openSetHash.remove(current)
explore_nodes +=1
if current==goal:
reconstructPath(cameFrom,current,drawFunc)
return True,explore_nodes
for neighbor in getNeighbors(grid,current,rows,cols):
tempGScore=gScore[current]+1
if tempGScore < gScore[neighbor]:
cameFrom[neighbor]=current
gScore[neighbor]=tempGScore
fScore[neighbor]=tempGScore+heuristic(neighbor,goal)
if neighbor not in openSetHash:
count+=1
heapq.heappush(openSet,(fScore[neighbor],count,neighbor))
openSetHash.add(neighbor)
if not neighbor.is_goal:
neighbor.color=PURPLE
drawFunc()
pygame.time.delay(40)
return False,explore_nodes