-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathastar.py
64 lines (59 loc) · 2.37 KB
/
astar.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
import time
import math
from queue import PriorityQueue
class A_STAR:
def __init__(self, maze=None, isManhattan=True):
self.maze = maze
self.goal = (1,1)
self.directions = 'ESWN'
self.isManhattan = isManhattan
def calculateEstimatedDistance(self, cur):
if self.isManhattan:
# MANHATTAN
return abs(cur[0] - self.goal[0]) + abs(cur[1] - self.goal[1])
# EUCLIDEAN
return math.sqrt((cur[0] - self.goal[0]) ** 2 + (cur[1] - self.goal[1]) ** 2)
def solve(self):
start_time = time.time()
start = (self.maze.rows, self.maze.cols)
# The cost path from the start node to the current node
g_score = {}
# The cost path from the current node to goal node
f_score = {}
for cur in self.maze.grid:
g_score[cur] = float('inf')
for cur in self.maze.grid:
f_score[cur] = float('inf')
g_score[start] = 0
f_score[start] = self.calculateEstimatedDistance(start)
pq = PriorityQueue()
pq.put((self.calculateEstimatedDistance(start), self.calculateEstimatedDistance(start), start))
path = {}
while not pq.empty():
cur = pq.get()[2]
if cur == self.goal:
break
for d in self.directions:
if self.maze.maze_map[cur][d]:
if d == 'E':
Next = (cur[0], cur[1] + 1)
elif d == 'S':
Next = (cur[0] + 1, cur[1])
elif d == 'W':
Next = (cur[0], cur[1] - 1)
elif d == 'N':
Next = (cur[0] - 1, cur[1])
next_g = g_score[cur] + 1
next_f = next_g + self.calculateEstimatedDistance(Next)
if next_f < f_score[Next]:
g_score[Next] = next_g
f_score[Next] = next_f
pq.put((next_f, self.calculateEstimatedDistance(Next), Next))
path[Next] = cur
astar_path = {}
cur = self.goal
while cur != start:
astar_path[path[cur]] = cur
cur = path[cur]
end_time = time.time()
return astar_path, path, end_time - start_time