-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAStar.py
More file actions
44 lines (34 loc) · 1.18 KB
/
Copy pathAStar.py
File metadata and controls
44 lines (34 loc) · 1.18 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
import pygame
import heapq
vec = pygame.Vector2
def vec2tuple(node):
return (int(node.x), int(node.y))
def heursitic(node1, node2):
return 10*(abs(node1.x - node2.x) + abs(node1.y - node2.y))
class PriorityQueue(object):
def __init__(self):
self.node = []
def push(self, node, cost):
heapq.heappush(self.node, (cost, node))
def pop(self):
return heapq.heappop(self.node)[1]
def isEmpty(self):
return len(self.node) == 0
def AStar(graph, start, end):
frontier = PriorityQueue()
frontier.push(vec2tuple(end), 0)
cost = {}
path = {}
cost[vec2tuple(end)] = 0
path[vec2tuple(end)] = vec(0, 0)
while not frontier.isEmpty():
current = frontier.pop()
if current == start:
break
for next in graph.findNeigbors(vec(current)):
next_cost = cost[current] + graph.w(current, vec2tuple(next))
if vec2tuple(next) not in cost or next_cost < cost[vec2tuple(next)]:
cost[vec2tuple(next)] = next_cost
frontier.push(vec2tuple(next), next_cost + heursitic(start, next))
path[vec2tuple(next)] = vec(current) - next
return path