70 lines
1.8 KiB
Python
70 lines
1.8 KiB
Python
import csv
|
|
import sys
|
|
from pathlib import Path
|
|
|
|
# Adjust sys.path to allow imports from src when run directly
|
|
BASE_DIR = Path(__file__).resolve().parent
|
|
sys.path.append(str(BASE_DIR.parent))
|
|
|
|
from src.builder import TextFileMazeBuilder
|
|
from src.solver import MazeSolver
|
|
from src.strategies import AStarStrategy, BFSStrategy, DFSStrategy
|
|
|
|
MAZE_DIR = BASE_DIR / "mazes"
|
|
|
|
|
|
def run_benchmarks():
|
|
builder = TextFileMazeBuilder()
|
|
strategies = [
|
|
("BFS", BFSStrategy()),
|
|
("DFS", DFSStrategy()),
|
|
("A*", AStarStrategy()),
|
|
]
|
|
|
|
results = []
|
|
|
|
for maze_path in sorted(MAZE_DIR.glob("*.txt")):
|
|
maze_name = maze_path.stem
|
|
try:
|
|
maze = builder.build_from_file(maze_path)
|
|
except Exception as e:
|
|
continue
|
|
|
|
solver = MazeSolver(maze)
|
|
|
|
for strat_name, strat in strategies:
|
|
solver.set_strategy(strat)
|
|
|
|
times = []
|
|
stats = None
|
|
for _ in range(10):
|
|
stats = solver.solve()
|
|
times.append(stats.time_ms)
|
|
|
|
avg_time = sum(times) / len(times)
|
|
|
|
results.append(
|
|
{
|
|
"maze": maze_name,
|
|
"strategy": strat_name,
|
|
"time_ms": avg_time,
|
|
"visited_cells": stats.visited_cells,
|
|
"path_length": stats.path_length,
|
|
}
|
|
)
|
|
|
|
csv_path = BASE_DIR.parent / "results.csv"
|
|
with open(csv_path, "w", newline="", encoding="utf-8") as f:
|
|
writer = csv.DictWriter(
|
|
f,
|
|
fieldnames=["maze", "strategy", "time_ms", "visited_cells", "path_length"],
|
|
)
|
|
writer.writeheader()
|
|
writer.writerows(results)
|
|
|
|
print(f"Benchmark results stored in {csv_path}")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
run_benchmarks()
|