-
Notifications
You must be signed in to change notification settings - Fork 0
/
A_star.py
69 lines (52 loc) · 1.94 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
import heapq
# Define a grid (0 represents empty, 1 represents obstacles)
grid = [
[0, 0, 0, 0, 0],
[0, 1, 1, 0, 0],
[0, 0, 0, 0, 1],
[0, 1, 1, 0, 0],
[0, 0, 0, 0, 0],
]
# Define the start and goal positions
start = (0, 0)
goal = (4, 4)
# Define a heuristic function (Manhattan distance)
def heuristic(node, goal):
return abs(node[0] - goal[0]) + abs(node[1] - goal[1])
# A* algorithm
def astar(grid, start, goal):
open_list = [(0.0, start)] # Priority queue (f-score, node)
came_from = {} # Dictionary to store the parent node of each node
# Initialize g_score with all nodes set to infinity
g_score = {
(x, y): float("inf") for x in range(len(grid)) for y in range(len(grid[0]))
}
g_score[start] = 0
f_score = {node: float("inf") for node in g_score}
f_score[start] = heuristic(start, goal)
while open_list:
_, current = heapq.heappop(open_list)
if current == goal:
path = []
while current in came_from:
path.append(current)
current = came_from[current]
return path[::-1]
for neighbor in [(0, 1), (0, -1), (1, 0), (-1, 0)]:
x, y = current[0] + neighbor[0], current[1] + neighbor[1]
if 0 <= x < len(grid) and 0 <= y < len(grid[0]) and grid[x][y] == 0:
tentative_g_score = g_score[current] + 1
if tentative_g_score < g_score[(x, y)]:
came_from[(x, y)] = current
g_score[(x, y)] = tentative_g_score
f_score[(x, y)] = tentative_g_score + heuristic((x, y), goal)
heapq.heappush(open_list, (f_score[(x, y)], (x, y)))
return None # No path found
# Find the path
path = astar(grid, start, goal)
if path:
print("Path found:")
for node in path:
print(node)
else:
print("No path found.")