YaTharThShaRma999
commited on
Update README.md
Browse files
README.md
CHANGED
@@ -1,48 +1,43 @@
|
|
1 |
-
from itertools import product, chain
|
2 |
-
import math
|
3 |
-
import matplotlib.pyplot as plt
|
4 |
import numpy as np
|
5 |
-
import
|
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 |
-
plt.figure(figsize=(10, 10))
|
48 |
-
plt.imshow(im, cmap='gray')
|
|
|
|
|
|
|
|
|
1 |
import numpy as np
|
2 |
+
import heapq
|
3 |
+
|
4 |
+
def heuristic(a, b):
|
5 |
+
return np.sqrt((b[0] - a[0]) ** 2 + (b[1] - a[1]) ** 2)
|
6 |
+
|
7 |
+
def astar(array, start, goal):
|
8 |
+
neighbors = [(0, 1), (0, -1), (1, 0), (-1, 0), (1, 1), (1, -1), (-1, 1), (-1, -1)]
|
9 |
+
close_set = set()
|
10 |
+
came_from = {}
|
11 |
+
gscore = {start: 0}
|
12 |
+
fscore = {start: heuristic(start, goal)}
|
13 |
+
open_heap = [(fscore[start], start)]
|
14 |
+
array_shape = array.shape
|
15 |
+
array_get = array.__getitem__
|
16 |
+
|
17 |
+
while open_heap:
|
18 |
+
current_fscore, current = heapq.heappop(open_heap)
|
19 |
+
|
20 |
+
if current == goal:
|
21 |
+
path = []
|
22 |
+
while current in came_from:
|
23 |
+
path.append(current)
|
24 |
+
current = came_from[current]
|
25 |
+
return path[::-1]
|
26 |
+
|
27 |
+
close_set.add(current)
|
28 |
+
for i, j in neighbors:
|
29 |
+
neighbor = current[0] + i, current[1] + j
|
30 |
+
|
31 |
+
if 0 <= neighbor[0] < array_shape[0] and 0 <= neighbor[1] < array_shape[1]:
|
32 |
+
if array_get(neighbor) == 1 or neighbor in close_set:
|
33 |
+
continue # Skip obstacles and already visited nodes
|
34 |
+
|
35 |
+
tentative_g_score = gscore[current] + heuristic(current, neighbor)
|
36 |
+
|
37 |
+
if tentative_g_score < gscore.get(neighbor, float('inf')):
|
38 |
+
came_from[neighbor] = current
|
39 |
+
gscore[neighbor] = tentative_g_score
|
40 |
+
fscore[neighbor] = tentative_g_score + heuristic(neighbor, goal)
|
41 |
+
heapq.heappush(open_heap, (fscore[neighbor], neighbor))
|
42 |
+
|
43 |
+
return None # No path found
|
|
|
|