Skip to content

test: adding more tests to a star algorithm #10397

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Oct 15, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions graphs/a_star.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,31 @@ def search(
cost: int,
heuristic: list[list[int]],
) -> tuple[list[list[int]], list[list[int]]]:
"""
Search for a path on a grid avoiding obstacles.
>>> grid = [[0, 1, 0, 0, 0, 0],
... [0, 1, 0, 0, 0, 0],
... [0, 1, 0, 0, 0, 0],
... [0, 1, 0, 0, 1, 0],
... [0, 0, 0, 0, 1, 0]]
>>> init = [0, 0]
>>> goal = [len(grid) - 1, len(grid[0]) - 1]
>>> cost = 1
>>> heuristic = [[0] * len(grid[0]) for _ in range(len(grid))]
>>> heuristic = [[0 for row in range(len(grid[0]))] for col in range(len(grid))]
>>> for i in range(len(grid)):
... for j in range(len(grid[0])):
... heuristic[i][j] = abs(i - goal[0]) + abs(j - goal[1])
... if grid[i][j] == 1:
... heuristic[i][j] = 99
>>> path, action = search(grid, init, goal, cost, heuristic)
>>> path
[[0, 0], [1, 0], [2, 0], [3, 0], [4, 0], [4, 1], [4, 2],\
[4, 3], [3, 3], [2, 3], [2, 4], [2, 5], [3, 5], [4, 5]]
>>> action
[[0, 0, 0, 0, 0, 0], [2, 0, 0, 0, 0, 0], [2, 0, 0, 0, 3, 3],\
[2, 0, 0, 0, 0, 2], [2, 3, 3, 3, 0, 2]]
"""
closed = [
[0 for col in range(len(grid[0]))] for row in range(len(grid))
] # the reference grid
Expand Down