Skip to content

Commit 8a66ed0

Browse files
committed
test: adding more tests to a star algorithm
1 parent d96029e commit 8a66ed0

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

Diff for: graphs/a_star.py

+28
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,34 @@ def search(
1616
cost: int,
1717
heuristic: list[list[int]],
1818
) -> tuple[list[list[int]], list[list[int]]]:
19+
"""
20+
Search for a path on a grid avoiding obstacles.
21+
>>> grid = [[0, 1, 0, 0, 0, 0],
22+
[0, 1, 0, 0, 0, 0],
23+
[0, 1, 0, 0, 0, 0],
24+
[0, 1, 0, 0, 1, 0],
25+
[0, 0, 0, 0, 1, 0]]
26+
>>> init = [0, 0]
27+
>>> goal = [len(grid)-1, len(grid[0])-1]
28+
>>> cost = 1
29+
>>> heuristic = [[0 for row in range(len(grid[0]))] for col in range(len(grid))]
30+
>>> for i in range(len(grid)):
31+
... for j in range(len(grid[0])):
32+
... heuristic[i][j] = abs(i - goal[0]) + abs(j - goal[1])
33+
... if grid[i][j] == 1:
34+
... heuristic[i][j] = 99
35+
>>> path, _ = search(grid, init, goal, cost, heuristic)
36+
>>> print(path)
37+
[[0, 0], [0, 1], [0, 2], [0, 3], [0, 4], [1, 4], [2, 4], [3, 4], [4, 4], [4, 5]]
38+
>>> print(action)
39+
[
40+
[0, 1, 0, 0, 0, 0],
41+
[0, 0, 0, 0, 0, 0],
42+
[0, 0, 0, 0, 0, 0],
43+
[0, 0, 0, 0, 0, 0],
44+
[0, 0, 0, 0, 0, 0]
45+
]
46+
"""
1947
closed = [
2048
[0 for col in range(len(grid[0]))] for row in range(len(grid))
2149
] # the reference grid

0 commit comments

Comments
 (0)