-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
130 lines (101 loc) · 5.21 KB
/
main.py
File metadata and controls
130 lines (101 loc) · 5.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
import os
import time
import platform
from grid import Grid
from planner import astar, bfs
from visualizer import print_emoji_grid, render_frame
from gif_generator import generate_gif, save_screenshots
# ── paths ─────────────────────────────────────────────────────
BASE_OUT = 'outputs'
SCREENSHOTS_DIR = os.path.join(BASE_OUT, 'screenshots')
# ── config ────────────────────────────────────────────────────
GRID_ROWS = 10
GRID_COLS = 10
OBSTACLE_RATIO = 0.20
TERMINAL_DELAY = 0.03 # seconds between terminal frames
EXPLORE_DURATION = 0.10 # GIF frame duration (exploration)
FINAL_DURATION = 3.00 # GIF final frame linger (3 seconds)
def clear():
os.system('cls' if platform.system() == 'Windows' else 'clear')
def setup_algo_dirs(algo_name):
frames_dir = os.path.join(BASE_OUT, f'frames_{algo_name}')
os.makedirs(frames_dir, exist_ok=True)
for f in os.listdir(frames_dir):
if f.endswith('.png'):
os.remove(os.path.join(frames_dir, f))
return frames_dir
def run_algo(algo_name, grid, explored_order, final_path):
"""Animate one algorithm: terminal emoji + save PNG frames."""
frames_dir = setup_algo_dirs(algo_name)
total_steps = len(explored_order)
frame_idx = 0
# ── exploration frames ────────────────────────────────────
explored_so_far = set()
for step, node in enumerate(explored_order):
explored_so_far.add(node)
clear()
print(f'[{algo_name}] Exploring… {step + 1}/{total_steps}')
print_emoji_grid(grid, explored=explored_so_far)
frame_path = os.path.join(frames_dir, f'frame_{frame_idx:04d}.png')
render_frame(
grid, explored_so_far, set(), frame_path,
title=f'{algo_name} – exploring {step + 1}/{total_steps}',
)
frame_idx += 1
time.sleep(TERMINAL_DELAY)
# ── final path frame ──────────────────────────────────────
final_set = set(final_path)
clear()
if final_path:
print(f'[{algo_name}] Path found! length={len(final_path)}, explored={total_steps} nodes')
else:
print(f'[{algo_name}] No path found.')
print_emoji_grid(grid, explored=explored_so_far, path=final_set)
frame_path = os.path.join(frames_dir, f'frame_{frame_idx:04d}.png')
render_frame(
grid, explored_so_far, final_set, frame_path,
title=f'{algo_name} – path length {len(final_path)}, explored {total_steps}',
)
return frames_dir
def main():
os.makedirs(BASE_OUT, exist_ok=True)
# ── 1. shared grid (same map for both algorithms) ─────────
grid = Grid(
rows=GRID_ROWS, cols=GRID_COLS,
obstacle_ratio=OBSTACLE_RATIO,
start=(0, 0), goal=(GRID_ROWS - 1, GRID_COLS - 1),
seed=None, # None = random each run
)
# ── 2. run both algorithms ────────────────────────────────
print('Running A* …')
astar_explored, astar_path = astar(grid)
print('Running BFS …')
bfs_explored, bfs_path = bfs(grid)
# ── 3. summary ────────────────────────────────────────────
print(f'\n{"":=<44}')
print(f' {"Algorithm":<10} {"Path len":>10} {"Nodes explored":>14}')
print(f' {"─"*10} {"─"*10} {"─"*14}')
print(f' {"A*":<10} {len(astar_path):>10} {len(astar_explored):>14}')
print(f' {"BFS":<10} {len(bfs_path):>10} {len(bfs_explored):>14}')
print(f'{"":=<44}\n')
time.sleep(1.5)
# ── 4. animate A* ────────────────────────────────────────
print('\n── Animating A* ──')
astar_frames = run_algo('A*', grid, astar_explored, astar_path)
# ── 5. animate BFS ───────────────────────────────────────
print('\n── Animating BFS ──')
bfs_frames = run_algo('BFS', grid, bfs_explored, bfs_path)
# ── 6. generate GIFs + screenshots ───────────────────────
astar_gif = os.path.join(BASE_OUT, 'result_astar.gif')
bfs_gif = os.path.join(BASE_OUT, 'result_bfs.gif')
generate_gif(astar_frames, astar_gif,
explore_duration=EXPLORE_DURATION,
final_duration=FINAL_DURATION)
generate_gif(bfs_frames, bfs_gif,
explore_duration=EXPLORE_DURATION,
final_duration=FINAL_DURATION)
save_screenshots(astar_frames, os.path.join(SCREENSHOTS_DIR, 'astar'))
save_screenshots(bfs_frames, os.path.join(SCREENSHOTS_DIR, 'bfs'))
print('\nDone! Check outputs/ for result_astar.gif and result_bfs.gif')
if __name__ == '__main__':
main()