-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpathfinding.py
More file actions
58 lines (47 loc) · 1.44 KB
/
Copy pathpathfinding.py
File metadata and controls
58 lines (47 loc) · 1.44 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
import heapq
class PriorityQueue:
def __init__(self):
self.elements = []
def empty(self):
return len(self.elements) == 0
def put(self, item, priority):
heapq.heappush(self.elements, (priority, item))
def get(self):
return heapq.heappop(self.elements)[1]
def heuristic(a, b):
(x1, y1) = a
(x2, y2) = b
return 0#abs(x1 - x2) + abs(y1 - y2) - 1
def reconstruct_path(came_from, start, goal):
current = goal
path = []
while current != start:
path.append(current)
try:
current = came_from[current]
except KeyError as e:
return False
path.append(start) # optional
path.reverse() # optional
return path
def a_star_search(grid, start, goal):
frontier = PriorityQueue()
frontier.put(start, 0)
came_from = {}
cost_so_far = {}
came_from[start] = None
cost_so_far[start] = 0
while not frontier.empty():
current = frontier.get()
if current == goal:
break
allowed_next = [n for n in grid.get_neighbouring_points(current) if grid.is_route_allowed(n)]
for next in allowed_next:
new_cost = cost_so_far[current] + grid.get_route_cost(next)
if next not in cost_so_far or new_cost < cost_so_far[next]:
#print(grid.get_route_cost(next))
cost_so_far[next] = new_cost
priority = new_cost + heuristic(goal, next)
frontier.put(next, priority)
came_from[next] = current
return reconstruct_path(came_from, start, goal), cost_so_far.get(goal, 123456)