Skip to content

Commit a2421f2

Browse files
committed
add test grids
1 parent f7e202d commit a2421f2

File tree

1 file changed

+49
-26
lines changed

1 file changed

+49
-26
lines changed

graphs/greedy_best_first.py

Lines changed: 49 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,35 @@
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-
]
18-
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+
]
36+
37+
1938
delta = ([-1, 0], [0, -1], [1, 0], [0, 1]) # up, left, down, right
2039

2140

@@ -82,7 +101,8 @@ class GreedyBestFirst:
82101
(6, 2), (6, 3), (5, 3), (5, 4), (5, 5), (6, 5), (6, 6)]
83102
"""
84103

85-
def __init__(self, start: tuple[int, int], goal: tuple[int, int]):
104+
def __init__(self, grid: list[list[int]], start: tuple[int, int], goal: tuple[int, int]):
105+
self.grid = grid
86106
self.start = Node(start[1], start[0], goal[1], goal[0], 0, None)
87107
self.target = Node(goal[1], goal[0], goal[1], goal[0], 99999, None)
88108

@@ -136,9 +156,9 @@ def get_successors(self, parent: Node) -> list[Node]:
136156
pos_x = parent.pos_x + action[1]
137157
pos_y = parent.pos_y + action[0]
138158

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:
159+
if (0 <= pos_x <= len(self.grid[0]) - 1
160+
and 0 <= pos_y <= len(self.grid) - 1
161+
and self.grid[pos_y][pos_x] == 0):
142162

143163
successors.append(
144164
Node(
@@ -166,18 +186,21 @@ def retrace_path(self, node: Node | None) -> Path:
166186

167187

168188
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
189+
for idx, grid in enumerate(TEST_GRIDS):
190+
print(f"==grid-{idx + 1}==")
181191

192+
init = (0, 0)
193+
goal = (len(grid) - 1, len(grid[0]) - 1)
182194
for elem in grid:
183195
print(elem)
196+
197+
print("------")
198+
199+
greedy_bf = GreedyBestFirst(grid, init, goal)
200+
path = greedy_bf.search()
201+
if path:
202+
for pos_x, pos_y in path:
203+
grid[pos_x][pos_y] = 2
204+
205+
for elem in grid:
206+
print(elem)

0 commit comments

Comments
 (0)