|
1 | 1 | from __future__ import annotations
|
2 | 2 |
|
3 |
| -grid = [ |
4 |
| - [0, 1, 0, 0, 0, 0], |
5 |
| - [0, 1, 0, 0, 0, 0], # 0 are free path whereas 1's are obstacles |
6 |
| - [0, 1, 0, 0, 0, 0], |
7 |
| - [0, 1, 0, 0, 1, 0], |
8 |
| - [0, 0, 0, 0, 1, 0], |
9 |
| -] |
10 |
| - |
11 |
| -""" |
12 |
| -heuristic = [[9, 8, 7, 6, 5, 4], |
13 |
| - [8, 7, 6, 5, 4, 3], |
14 |
| - [7, 6, 5, 4, 3, 2], |
15 |
| - [6, 5, 4, 3, 2, 1], |
16 |
| - [5, 4, 3, 2, 1, 0]]""" |
17 |
| - |
18 |
| -init = [0, 0] |
19 |
| -# all coordinates are given in format [y,x] |
20 |
| -goal = [len(grid) - 1, len(grid[0]) - 1] |
21 |
| -cost = 1 |
22 |
| - |
23 |
| -# the cost map which pushes the path closer to the goal |
24 |
| -heuristic = [[0 for row in range(len(grid[0]))] for col in range(len(grid))] |
25 |
| -for i in range(len(grid)): |
26 |
| - for j in range(len(grid[0])): |
27 |
| - heuristic[i][j] = abs(i - goal[0]) + abs(j - goal[1]) |
28 |
| - if grid[i][j] == 1: |
29 |
| - heuristic[i][j] = 99 # added extra penalty in the heuristic map |
30 |
| - |
31 |
| - |
32 | 3 | # the actions we can take
|
33 | 4 | # go up # go left # go down # go right
|
34 | 5 | DIRECTIONS = [[-1, 0], [0, -1], [1, 0], [0, 1]]
|
@@ -105,6 +76,36 @@ def search(
|
105 | 76 | return path
|
106 | 77 |
|
107 | 78 |
|
108 |
| -a = search(grid, init, goal, cost, heuristic) |
109 |
| -for i in range(len(a)): |
110 |
| - print(a[i]) |
| 79 | +if __name__ == "__main__": |
| 80 | + grid = [ |
| 81 | + [0, 1, 0, 0, 0, 0], |
| 82 | + [0, 1, 0, 0, 0, 0], # 0 are free path whereas 1's are obstacles |
| 83 | + [0, 1, 0, 0, 0, 0], |
| 84 | + [0, 1, 0, 0, 1, 0], |
| 85 | + [0, 0, 0, 0, 1, 0], |
| 86 | + ] |
| 87 | + |
| 88 | + """ |
| 89 | + heuristic = [[9, 8, 7, 6, 5, 4], |
| 90 | + [8, 7, 6, 5, 4, 3], |
| 91 | + [7, 6, 5, 4, 3, 2], |
| 92 | + [6, 5, 4, 3, 2, 1], |
| 93 | + [5, 4, 3, 2, 1, 0]]""" |
| 94 | + |
| 95 | + init = [0, 0] |
| 96 | + # all coordinates are given in format [y,x] |
| 97 | + goal = [len(grid) - 1, len(grid[0]) - 1] |
| 98 | + cost = 1 |
| 99 | + |
| 100 | + # the cost map which pushes the path closer to the goal |
| 101 | + heuristic = [[0 for row in range(len(grid[0]))] for col in range(len(grid))] |
| 102 | + for i in range(len(grid)): |
| 103 | + for j in range(len(grid[0])): |
| 104 | + heuristic[i][j] = abs(i - goal[0]) + abs(j - goal[1]) |
| 105 | + if grid[i][j] == 1: |
| 106 | + # added extra penalty in the heuristic map |
| 107 | + heuristic[i][j] = 99 |
| 108 | + |
| 109 | + a = search(grid, init, goal, cost, heuristic) |
| 110 | + for i in range(len(a)): |
| 111 | + print(a[i]) |
0 commit comments