-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathastar.py
66 lines (53 loc) · 2.28 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
65
66
from node import Node
from environment import *
def astar(grid, start, goal, poly):
'''
Args:
grid: grid
start: starting point
goal: goal point
Returns: path length between points
'''
path = []
node = []
for i, row in enumerate(grid):
col = []
for j, val in enumerate(row):
col.append(Node(i, j, val, 0))
node.append(col)
for i in range(len(node)):
for j in range(len(node[i])):
node[i][j].h = math.hypot(goal[0]-node[i][j].x, goal[1]-node[i][j].y) # h value for each node
seen = [[False for _ in range(len(node))]
for _ in range(len(grid))]
# initialize the start
node[start[0]][start[1]].g = 0
node[start[0]][start[1]].cost = node[start[0]][start[1]].g + node[start[0]][start[1]].h
current_node = not_seen_min_cost(node, seen)
seen[current_node.x][current_node.y] = True
path_length = 0
while current_node:
# goal condition
if current_node.x == goal[0] and current_node.y == goal[1]:
path.append(goal)
while path[-1] != start:
path_length += distance(current_node, current_node.parent)
path.append((current_node.parent.x, current_node.parent.y))
current_node = current_node.parent
path.reverse()
break
neighbor_locations = travel(current_node, grid, seen, poly)
for neighbor in neighbor_locations:
# check f value
if node[neighbor[0]][neighbor[1]].cost is None or \
node[neighbor[0]][neighbor[1]].cost > current_node.g + distance(current_node, node[neighbor[0]][neighbor[1]]) + node[neighbor[0]][neighbor[1]].h:
node[neighbor[0]][neighbor[1]].g = current_node.g + distance(current_node, node[neighbor[0]][neighbor[1]])
node[neighbor[0]][neighbor[1]].cost = node[neighbor[0]][neighbor[1]].g + node[neighbor[0]][
neighbor[1]].h
node[neighbor[0]][neighbor[1]].parent = current_node
current_node = not_seen_min_cost(node, seen)
# if no path exists
if current_node is None:
break
seen[current_node.x][current_node.y] = True # won't visit this node again
return path, path_length