-
Notifications
You must be signed in to change notification settings - Fork 0
/
a_star.py
282 lines (228 loc) · 9.49 KB
/
a_star.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
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
# Sample code from https://www.redblobgames.com/pathfinding/a-star/
# Copyright 2014 Red Blob Games <[email protected]>
#
# Feel free to use this code in your own projects, including commercial projects
# License: Apache v2.0 <http://www.apache.org/licenses/LICENSE-2.0.html>
from __future__ import annotations
# some of these types are deprecated: https://www.python.org/dev/peps/pep-0585/
from typing import Protocol, Iterator, Tuple, TypeVar, Optional
T = TypeVar('T')
Location = TypeVar('Location')
class Graph(Protocol):
def neighbors(self, id: Location) -> list[Location]: pass
class SimpleGraph:
def __init__(self):
self.edges: dict[Location, list[Location]] = {}
def neighbors(self, id: Location) -> list[Location]:
return self.edges[id]
example_graph = SimpleGraph()
example_graph.edges = {
'A': ['B'],
'B': ['C'],
'C': ['B', 'D', 'F'],
'D': ['C', 'E'],
'E': ['F'],
'F': [],
}
import collections
class Queue:
def __init__(self):
self.elements = collections.deque()
def empty(self) -> bool:
return not self.elements
def put(self, x: T):
self.elements.append(x)
def get(self) -> T:
return self.elements.popleft()
# utility functions for dealing with square grids
def from_id_width(id, width):
return (id % width, id // width)
def draw_tile(graph, id, style):
r = " . "
if 'number' in style and id in style['number']: r = " %-2d" % style['number'][id]
if 'point_to' in style and style['point_to'].get(id, None) is not None:
(x1, y1) = id
(x2, y2) = style['point_to'][id]
if x2 == x1 + 1: r = " > "
if x2 == x1 - 1: r = " < "
if y2 == y1 + 1: r = " v "
if y2 == y1 - 1: r = " ^ "
if 'path' in style and id in style['path']: r = " @ "
if 'start' in style and id == style['start']: r = " A "
if 'goal' in style and id == style['goal']: r = " Z "
if id in graph.walls: r = "###"
return r
def draw_grid(graph, **style):
print("___" * graph.width)
for y in range(graph.height):
for x in range(graph.width):
print("%s" % draw_tile(graph, (x, y), style), end="")
print()
print("~~~" * graph.width)
# data from main article
DIAGRAM1_WALLS = [from_id_width(id, width=30) for id in [21,22,51,52,81,82,93,94,111,112,123,124,133,134,141,142,153,154,163,164,171,172,173,174,175,183,184,193,194,201,202,203,204,205,213,214,223,224,243,244,253,254,273,274,283,284,303,304,313,314,333,334,343,344,373,374,403,404,433,434]]
GridLocation = Tuple[int, int]
class SquareGrid:
def __init__(self, start_pos: Tuple[int, int], width: int, height: int):
self.width = width
self.height = height
self.walls: list[GridLocation] = []
self.start_position = start_pos
def in_bounds(self, id: GridLocation) -> bool:
(x, y) = id
return self.start_position[0] <= x < self.start_position[0]+self.width and self.start_position[1] <= y < self.start_position[1]+self.height
def passable(self, id: GridLocation) -> bool:
return id not in self.walls
def neighbors(self, id: GridLocation) -> Iterator[GridLocation]:
(x, y) = id
neighbors = [(x+1, y), (x-1, y), (x, y-1), (x, y+1)] # E W N S
# see "Ugly paths" section for an explanation:
if (x + y) % 2 == 0: neighbors.reverse() # S N W E
results = filter(self.in_bounds, neighbors)
results = filter(self.passable, results)
return results
class WeightedGraph(Graph):
def cost(self, from_id: Location, to_id: Location) -> float: pass
class GridWithWeights(SquareGrid):
def __init__(self, start_pos: Tuple[int, int], width: int, height: int):
super().__init__(start_pos, width, height)
self.weights: dict[GridLocation, float] = {}
def cost(self, from_node: GridLocation, to_node: GridLocation) -> float:
return self.weights.get(to_node, 1)
start_position = Tuple[int, int]
start_position = (0,0)
diagram4 = GridWithWeights(start_position, 10, 10)
diagram4.walls = [[5, 5],
[5, 6]
]
diagram4.weights = {loc: 5 for loc in [(3, 4), (3, 5), (4, 1), (4, 2),
(4, 3), (4, 4), (4, 5), (4, 6),
(4, 7), (4, 8), (5, 1), (5, 2),
(5, 3), (5, 4), (5, 5), (5, 6),
(5, 7), (5, 8), (6, 2), (6, 3),
(6, 4), (6, 5), (6, 6), (6, 7),
(7, 3), (7, 4), (7, 5)]}
import heapq
class PriorityQueue:
def __init__(self):
self.elements: list[tuple[float, T]] = []
def empty(self) -> bool:
return not self.elements
def put(self, item: T, priority: float):
heapq.heappush(self.elements, (priority, item))
def get(self) -> T:
return heapq.heappop(self.elements)[1]
def dijkstra_search(graph: WeightedGraph, start: Location, goal: Location):
frontier = PriorityQueue()
frontier.put(start, 0)
came_from: dict[Location, Optional[Location]] = {}
cost_so_far: dict[Location, float] = {}
came_from[start] = None
cost_so_far[start] = 0
while not frontier.empty():
current: Location = frontier.get()
if current == goal:
break
for next in graph.neighbors(current):
new_cost = cost_so_far[current] + graph.cost(current, next)
if next not in cost_so_far or new_cost < cost_so_far[next]:
cost_so_far[next] = new_cost
priority = new_cost
frontier.put(next, priority)
came_from[next] = current
return came_from, cost_so_far
# thanks to @m1sp <Jaiden Mispy> for this simpler version of
# reconstruct_path that doesn't have duplicate entries
def reconstruct_path(came_from: dict[Location, Location],
start: Location, goal: Location) -> list[Location]:
current: Location = goal
path: list[Location] = []
if goal not in came_from: # no path was found
print('test')
return []
while current != start:
path.append(current)
current = came_from[current]
path.append(start) # optional
path.reverse() # optional
return path
# diagram_nopath = GridWithWeights(10, 10)
# diagram_nopath.walls = [(5, row) for row in range(10)]
def heuristic(a: GridLocation, b: GridLocation) -> float:
(x1, y1) = a
(x2, y2) = b
return abs(x1 - x2) + abs(y1 - y2)
import numpy as np
def euclidean(a: GridLocation, b: GridLocation) -> float:
(x1, y1) = a
(x2, y2) = b
return np.sqrt((x1 - x2)**2 + (y1 - y2)**2)
def a_star_search(graph: WeightedGraph, start: Location, goal: Location, heuristic):
frontier = PriorityQueue()
frontier.put(start, 0)
came_from: dict[Location, Optional[Location]] = {}
cost_so_far: dict[Location, float] = {}
came_from[start] = None
cost_so_far[start] = 0
while not frontier.empty():
current: Location = frontier.get()
if current == goal:
break
for next in graph.neighbors(current):
new_cost = cost_so_far[current] + graph.cost(current, next)
if next not in cost_so_far or new_cost < cost_so_far[next]:
cost_so_far[next] = new_cost
priority = new_cost + heuristic(next, goal)
frontier.put(next, priority)
came_from[next] = current
return came_from, cost_so_far
def breadth_first_search(graph: Graph, start: Location, goal: Location):
frontier = Queue()
frontier.put(start)
came_from: dict[Location, Optional[Location]] = {}
came_from[start] = None
while not frontier.empty():
current: Location = frontier.get()
if current == goal:
break
for next in graph.neighbors(current):
if next not in came_from:
frontier.put(next)
came_from[next] = current
return came_from
class SquareGridNeighborOrder(SquareGrid):
def neighbors(self, id):
(x, y) = id
neighbors = [(x + dx, y + dy) for (dx, dy) in self.NEIGHBOR_ORDER]
results = filter(self.in_bounds, neighbors)
results = filter(self.passable, results)
return list(results)
def test_with_custom_order(neighbor_order):
if neighbor_order:
g = SquareGridNeighborOrder(30, 15)
g.NEIGHBOR_ORDER = neighbor_order
else:
g = SquareGrid(30, 15)
g.walls = DIAGRAM1_WALLS
start, goal = (8, 7), (27, 2)
came_from = breadth_first_search(g, start, goal)
draw_grid(g, path=reconstruct_path(came_from, start=start, goal=goal),
point_to=came_from, start=start, goal=goal)
class GridWithAdjustedWeights(GridWithWeights):
def cost(self, from_node, to_node):
prev_cost = super().cost(from_node, to_node)
nudge = 0
(x1, y1) = from_node
(x2, y2) = to_node
if (x1 + y1) % 2 == 0 and x2 != x1: nudge = 1
if (x1 + y1) % 2 == 1 and y2 != y1: nudge = 1
return prev_cost + 0.001 * nudge
if __name__ == "__main__":
start, goal = (1, 4), (8, 3)
came_from, cost_so_far = a_star_search(diagram4, start, goal)
print(cost_so_far)
draw_grid(diagram4, point_to=came_from, start=start, goal=goal)
print()
draw_grid(diagram4, path=reconstruct_path(came_from, start=start, goal=goal))
cnt = 0
print(reconstruct_path(came_from, start=start, goal=goal))