Skip to content

Commit b7fe4a2

Browse files
committed
add test grids
1 parent f79c596 commit b7fe4a2

File tree

1 file changed

+47
-25
lines changed

1 file changed

+47
-25
lines changed

Diff for: graphs/greedy_best_first.py

+47-25
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,33 @@
66

77
Path = list[tuple[int, int]]
88

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+
]
1836

1937
delta = ([-1, 0], [0, -1], [1, 0], [0, 1]) # up, left, down, right
2038

@@ -82,7 +100,8 @@ class GreedyBestFirst:
82100
(6, 2), (6, 3), (5, 3), (5, 4), (5, 5), (6, 5), (6, 6)]
83101
"""
84102

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
86105
self.start = Node(start[1], start[0], goal[1], goal[0], 0, None)
87106
self.target = Node(goal[1], goal[0], goal[1], goal[0], 99999, None)
88107

@@ -136,9 +155,9 @@ def get_successors(self, parent: Node) -> list[Node]:
136155
pos_x = parent.pos_x + action[1]
137156
pos_y = parent.pos_y + action[0]
138157

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):
142161

143162
successors.append(
144163
Node(
@@ -166,18 +185,21 @@ def retrace_path(self, node: Node | None) -> Path:
166185

167186

168187
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}==")
181190

191+
init = (0, 0)
192+
goal = (len(grid) - 1, len(grid[0]) - 1)
182193
for elem in grid:
183194
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

Comments
 (0)