From 0f62ff089a4e24f78defea3b4f2a6588d186db42 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ab=C3=ADlio=20Azevedo?= Date: Fri, 13 Oct 2023 20:09:56 -0300 Subject: [PATCH 1/3] test: adding more tests to a star algorithm --- graphs/a_star.py | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/graphs/a_star.py b/graphs/a_star.py index e8735179eab9..ec6541ce0934 100644 --- a/graphs/a_star.py +++ b/graphs/a_star.py @@ -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 From 94db40d4449ed4693806fa5ea9beec209439b20e Mon Sep 17 00:00:00 2001 From: Tianyi Zheng Date: Sun, 15 Oct 2023 14:45:46 -0400 Subject: [PATCH 2/3] Apply suggestions from code review --- graphs/a_star.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/graphs/a_star.py b/graphs/a_star.py index ec6541ce0934..39aa3b69b913 100644 --- a/graphs/a_star.py +++ b/graphs/a_star.py @@ -35,11 +35,11 @@ def search( ... 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]] + [[0, 0], [1, 0], [2, 0], [3, 0], [4, 0], [4, 1], # doctest: +NORMALIZE_WHITESPACE + [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]] + [[0, 0, 0, 0, 0, 0], [2, 0, 0, 0, 0, 0], # doctest: +NORMALIZE_WHITESPACE + [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)) From 210b2595d2d544667211aa5c182b5ec9fca9172b Mon Sep 17 00:00:00 2001 From: Tianyi Zheng Date: Sun, 15 Oct 2023 14:52:40 -0400 Subject: [PATCH 3/3] Update a_star.py --- graphs/a_star.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/graphs/a_star.py b/graphs/a_star.py index 39aa3b69b913..06da3b5cd863 100644 --- a/graphs/a_star.py +++ b/graphs/a_star.py @@ -34,12 +34,12 @@ def search( ... 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], # doctest: +NORMALIZE_WHITESPACE - [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], # doctest: +NORMALIZE_WHITESPACE - [2, 0, 0, 0, 3, 3], [2, 0, 0, 0, 0, 2], [2, 3, 3, 3, 0, 2]] + >>> path # doctest: +NORMALIZE_WHITESPACE + [[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 # doctest: +NORMALIZE_WHITESPACE + [[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))