2026-rff_mp/volkovim/task2/experiments/benchmark.py
2026-05-24 21:48:59 +03:00

93 lines
2.1 KiB
Python

import csv
from solver.maze_solver import MazeSolver
class BenchmarkRunner:
def __init__(
self,
maze,
strategies,
cycles=5
):
self.maze = maze
self.strategies = strategies
self.cycles = cycles
def launch(self):
report = []
for strategy in self.strategies:
solver = MazeSolver(
self.maze,
strategy
)
total_time = 0
total_visited = 0
total_path = 0
for _ in range(self.cycles):
_, stats = solver.solve()
total_time += stats.time_ms
total_visited += stats.visited_cells
total_path += stats.path_length
report.append(
{
"maze": "",
"strategy":
strategy.__class__.__name__,
"time_ms":
round(
total_time / self.cycles,
4
),
"visited_cells":
round(
total_visited / self.cycles,
2
),
"path_length":
round(
total_path / self.cycles,
2
)
}
)
return report
def exportCSV(
self,
filename,
results
):
with open(
filename,
"w",
newline="",
encoding="utf-8"
) as file:
writer = csv.DictWriter(
file,
fieldnames=[
"maze",
"strategy",
"time_ms",
"visited_cells",
"path_length"
]
)
writer.writeheader()
for row in results:
writer.writerow(row)