|
6 | 6 |
|
7 | 7 | Path = list[tuple[int, int]]
|
8 | 8 |
|
9 |
| -grid = [ |
10 |
| - [0, 0, 0, 0, 0, 0, 0], |
11 |
| - [0, 1, 0, 0, 0, 0, 0], # 0 are free path whereas 1's are obstacles |
12 |
| - [0, 0, 0, 0, 0, 0, 0], |
13 |
| - [0, 0, 1, 0, 0, 0, 0], |
14 |
| - [1, 0, 1, 0, 0, 0, 0], |
15 |
| - [0, 0, 0, 0, 0, 0, 0], |
16 |
| - [0, 0, 0, 0, 1, 0, 0], |
17 |
| -] |
| 9 | +# 0 are free path whereas 1's are obstacles |
| 10 | +TEST_GRIDS = [ |
| 11 | + [ |
| 12 | + [0, 0, 0, 0, 0, 0, 0], |
| 13 | + [0, 1, 0, 0, 0, 0, 0], |
| 14 | + [0, 0, 0, 0, 0, 0, 0], |
| 15 | + [0, 0, 1, 0, 0, 0, 0], |
| 16 | + [1, 0, 1, 0, 0, 0, 0], |
| 17 | + [0, 0, 0, 0, 0, 0, 0], |
| 18 | + [0, 0, 0, 0, 1, 0, 0], |
| 19 | + ], |
| 20 | + [ |
| 21 | + [0, 0, 0, 1, 1, 0, 0], |
| 22 | + [0, 0, 0, 0, 1, 0, 1], |
| 23 | + [0, 0, 0, 1, 1, 0, 0], |
| 24 | + [0, 1, 0, 0, 1, 0, 0], |
| 25 | + [1, 0, 0, 1, 1, 0, 1], |
| 26 | + [0, 0, 0, 0, 0, 0, 0] |
| 27 | + ], |
| 28 | + [ |
| 29 | + [0, 0, 1, 0, 0], |
| 30 | + [0, 1, 0, 0, 0], |
| 31 | + [0, 0, 1, 0, 1], |
| 32 | + [1, 0, 0, 1, 1], |
| 33 | + [0, 0, 0, 0, 0] |
| 34 | + ] |
| 35 | +] |
18 | 36 |
|
19 | 37 | delta = ([-1, 0], [0, -1], [1, 0], [0, 1]) # up, left, down, right
|
20 | 38 |
|
@@ -82,7 +100,8 @@ class GreedyBestFirst:
|
82 | 100 | (6, 2), (6, 3), (5, 3), (5, 4), (5, 5), (6, 5), (6, 6)]
|
83 | 101 | """
|
84 | 102 |
|
85 |
| - def __init__(self, start: tuple[int, int], goal: tuple[int, int]): |
| 103 | + def __init__(self, grid: list[list[int]], start: tuple[int, int], goal: tuple[int, int]): |
| 104 | + self.grid = grid |
86 | 105 | self.start = Node(start[1], start[0], goal[1], goal[0], 0, None)
|
87 | 106 | self.target = Node(goal[1], goal[0], goal[1], goal[0], 99999, None)
|
88 | 107 |
|
@@ -136,9 +155,9 @@ def get_successors(self, parent: Node) -> list[Node]:
|
136 | 155 | pos_x = parent.pos_x + action[1]
|
137 | 156 | pos_y = parent.pos_y + action[0]
|
138 | 157 |
|
139 |
| - if (0 <= pos_x <= len(grid[0]) - 1 |
140 |
| - and 0 <= pos_y <= len(grid) - 1 |
141 |
| - and grid[pos_y][pos_x] == 0): |
| 158 | + if (0 <= pos_x <= len(self.grid[0]) - 1 |
| 159 | + and 0 <= pos_y <= len(self.grid) - 1 |
| 160 | + and self.grid[pos_y][pos_x] == 0): |
142 | 161 |
|
143 | 162 | successors.append(
|
144 | 163 | Node(
|
@@ -166,18 +185,21 @@ def retrace_path(self, node: Node | None) -> Path:
|
166 | 185 |
|
167 | 186 |
|
168 | 187 | if __name__ == "__main__":
|
169 |
| - init = (0, 0) |
170 |
| - goal = (len(grid) - 1, len(grid[0]) - 1) |
171 |
| - for elem in grid: |
172 |
| - print(elem) |
173 |
| - |
174 |
| - print("------") |
175 |
| - |
176 |
| - greedy_bf = GreedyBestFirst(init, goal) |
177 |
| - path = greedy_bf.search() |
178 |
| - if path: |
179 |
| - for pos_x, pos_y in path: |
180 |
| - grid[pos_x][pos_y] = 2 |
| 188 | + for idx, grid in enumerate(TEST_GRIDS): |
| 189 | + print(f"==grid-{idx + 1}==") |
181 | 190 |
|
| 191 | + init = (0, 0) |
| 192 | + goal = (len(grid) - 1, len(grid[0]) - 1) |
182 | 193 | for elem in grid:
|
183 | 194 | print(elem)
|
| 195 | + |
| 196 | + print("------") |
| 197 | + |
| 198 | + greedy_bf = GreedyBestFirst(grid, init, goal) |
| 199 | + path = greedy_bf.search() |
| 200 | + if path: |
| 201 | + for pos_x, pos_y in path: |
| 202 | + grid[pos_x][pos_y] = 2 |
| 203 | + |
| 204 | + for elem in grid: |
| 205 | + print(elem) |
0 commit comments