From 1fe65d551be30b640e1f770f203e4e81983980ad Mon Sep 17 00:00:00 2001 From: Minoli Gamlath Date: Mon, 18 Oct 2021 20:25:52 +0530 Subject: [PATCH 1/4] Prevent starting/finishing at an obstacle --- graphs/a_star.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/graphs/a_star.py b/graphs/a_star.py index d3657cb19540..fd62fc30e6a8 100644 --- a/graphs/a_star.py +++ b/graphs/a_star.py @@ -33,6 +33,14 @@ # function to search the path def search(grid, init, goal, cost, heuristic): + if (grid[goal[0]][goal[1]]==1): + print("Error: Goal cannot be an obstacle") + return + + if (grid[init[0]][init[1]]==1): + print("Error: Cannot start at an obstacle") + return + closed = [ [0 for col in range(len(grid[0]))] for row in range(len(grid)) ] # the reference grid From 05f4dd1a1255b5ef12c878d0651d725c93bf97c3 Mon Sep 17 00:00:00 2001 From: Minoli Gamlath Date: Mon, 18 Oct 2021 20:29:02 +0530 Subject: [PATCH 2/4] Allowed user to call function without heuristic map --- graphs/a_star.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/graphs/a_star.py b/graphs/a_star.py index fd62fc30e6a8..7f1203e557f9 100644 --- a/graphs/a_star.py +++ b/graphs/a_star.py @@ -31,7 +31,7 @@ # function to search the path -def search(grid, init, goal, cost, heuristic): +def search(grid, init, goal, cost, heuristic=heuristic): if (grid[goal[0]][goal[1]]==1): print("Error: Goal cannot be an obstacle") From ac619ea3c5b8a4159ac75c75d8ad8e7b991d70e8 Mon Sep 17 00:00:00 2001 From: Minoli Gamlath Date: Mon, 18 Oct 2021 20:37:23 +0530 Subject: [PATCH 3/4] Replaced default heuristic with cost sensitive function --- graphs/a_star.py | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/graphs/a_star.py b/graphs/a_star.py index 7f1203e557f9..456bfe8a17ff 100644 --- a/graphs/a_star.py +++ b/graphs/a_star.py @@ -17,21 +17,25 @@ goal = [len(grid) - 1, len(grid[0]) - 1] # all coordinates are given in format [y,x] cost = 1 -# the cost map which pushes the path closer to the goal -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 # added extra penalty in the heuristic map +def getHeuristicMap(cost): + + 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] = cost * (abs(i - goal[0]) + abs(j - goal[1])) + if grid[i][j] == 1: + heuristic[i][j] = 99 # added extra penalty in the heuristic map + + return heuristic + # the actions we can take delta = [[-1, 0], [0, -1], [1, 0], [0, 1]] # go up # go left # go down # go right # function to search the path -def search(grid, init, goal, cost, heuristic=heuristic): +def search(grid, init, goal, cost, heuristic=getHeuristicMap(cost)): if (grid[goal[0]][goal[1]]==1): print("Error: Goal cannot be an obstacle") @@ -103,6 +107,6 @@ def search(grid, init, goal, cost, heuristic=heuristic): return path -a = search(grid, init, goal, cost, heuristic) +a = search(grid, init, goal, cost) for i in range(len(a)): print(a[i]) From 51822a450a60031e29db58a2805055d752bfb4f2 Mon Sep 17 00:00:00 2001 From: Minoli Gamlath Date: Tue, 19 Oct 2021 18:17:46 +0530 Subject: [PATCH 4/4] Fixed function name according to convention --- graphs/a_star.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/graphs/a_star.py b/graphs/a_star.py index 456bfe8a17ff..b612e61dc4b2 100644 --- a/graphs/a_star.py +++ b/graphs/a_star.py @@ -18,7 +18,7 @@ cost = 1 -def getHeuristicMap(cost): +def get_heuristic_map(cost): heuristic = [[0 for row in range(len(grid[0]))] for col in range(len(grid))] for i in range(len(grid)):