-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patha_star.py
More file actions
198 lines (148 loc) · 6.27 KB
/
a_star.py
File metadata and controls
198 lines (148 loc) · 6.27 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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
import math
from heapq import heappop, heappush
from termcolor import colored
class Node():
"""
Node class for A*
takes parent arg, positional arg,
g-, h-, f-values are assigned to a node
eq: self, otherNode -> returns True if both positional arguments are the same
dist2d: self, otherNode -> returns ecludian distance between both nodes
"""
def __init__(self, parent = None, position = None):
self.parent = parent
self.position = position
self.g = 0 #cost of path from start node to current node
self.h = 0 #actual cost of path from current node to end node
self.f = 0 #total cost of path from start to end using this node (f = g + h)
def __eq__(self, otherNode):
return self.position == otherNode.position
def dist2d(self, otherNode):
x1, y1 = self.position[0:2]
x2, y2 = otherNode.position[0:2]
dist = math.sqrt((x1-x2)**2 + (y1-y2)**2)
return dist
def a_star(start_pos, end_pos, map, occupancy_cost, movement):
"""
A Star pathfinding algorithm implemented for use on grid maps
takes: start position (x, y), end position (x, y), grid map (0=clear, 1=barrier), occupancy cost factor, movement type (4N=4directions, 8N=8directions)
returns: path as list of position-tuples from start position to end position
"""
startNode = Node(None, start_pos)
endNode = Node(None, end_pos)
startNode_cost = 0
startNode_estimated_cost_to_endNode = startNode.dist2d(endNode)
front = [(startNode_estimated_cost_to_endNode, startNode_cost, start_pos, None)]
came_from = {}
s2 = math.sqrt(2)
if movement == '8N':
movements = [(1, 0, 1.0),
(0, 1, 1.0),
(-1, 0, 1.0),
(0, -1, 1.0),
(1, 1, s2),
(-1, 1, s2),
(-1, -1, s2),
(1, -1, s2)]
elif movement == '4N':
movements = [(1, 0, 1.0),
(0, 1, 1.0),
(-1, 0, 1.0),
(0, -1, 1.0)]
else:
raise NameError
visited = []
while front:
element = heappop(front)
total_cost, cost, pos, previous = element
if pos in visited:
continue
visited.append(pos)
came_from[pos] = previous
if pos == end_pos:
break
for dx, dy, deltacost in movements:
new_x = pos[0] + dx
new_y = pos[1] + dy
new_pos = (new_x, new_y)
new_node = Node(previous, new_pos)
if new_node.position[0] > (len(map) - 1) or new_node.position[0] < 0 or new_node.position[1] > (len(map[len(map)-1]) -1) or new_node.position[1] < 0:
continue
if map[new_node.position[0]][new_node.position[1]] != 0:
continue
else:
potential_function_cost = map[new_x][new_y]*occupancy_cost
new_cost = cost + deltacost + potential_function_cost
new_total_cost_to_endNode = new_cost + Node.dist2d(new_node, endNode) + potential_function_cost
newVisStat = False
heappush(front, (new_total_cost_to_endNode, new_cost, new_pos, pos))
path = []
path_idx = []
if pos == endNode.position:
while pos:
path_idx.append(pos)
path.append((pos[0], pos[1]))
pos = came_from[pos]
path.reverse()
path_idx.reverse()
return path
def termMap(map, path):
"""
maps out a given path in the given map
takes: grid map (2D list, 0=clear, 1=barrier), path (list of tuples)
returns: altered map with path marked
"""
for pos in path:
map[pos[0]][pos[1]] = 'o '
start = path[0]
end = path[-1]
map[start[0]][start[1]] = 's '
map[end[0]][end[1]] = 'e '
print("\nMapped out path:", end = '')
print('\n')
print('+ ' + len(map[0])*'- ' + '+')
for y in range(len(map)):
print('| ', end = '')
for x in range(len(map[len(map)-1])):
val = map[y][x]
if val == 0:
print('• ', end = '')
if val == 1:
print("# ", end = '')
if val == 'o ':
print(colored(val, 'green'), end = '')
if val == 's ' or val == 'e ':
print(colored(val, 'red'), end = '')
print('| ', end = '')
print('\n', end = '')
print('+ ' + len(map[0])*'- ' + '+')
print('\n')
print('Path length:',len(path))
print('\n')
return map
if __name__ == "__main__":
startNode = (0,0)
endNode = (1, 15)
map = [ [0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 1, 1, 0, 0, 0],
[0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0],
[0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 1, 0, 0, 0, 1, 0, 1, 0],
[0, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 1, 1, 1, 1, 0, 1, 0],
[0, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0],
[0, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0],
[0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 1],
[0, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 0, 0, 1, 0, 0, 0, 1, 0, 0],
[0, 0, 1, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0],
[0, 1, 1, 1, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0],
[0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1, 0, 1, 0, 1],
[0, 1, 0, 1, 1, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 0],
[0, 1, 0, 1, 0, 0, 0, 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