forked from UNN/2026-rff_mp
[2] experimental module and plotting
This commit is contained in:
parent
641e1a8aac
commit
cb5e43f857
|
|
@ -3,6 +3,9 @@ import os
|
||||||
from collections import deque
|
from collections import deque
|
||||||
import heapq
|
import heapq
|
||||||
import time
|
import time
|
||||||
|
import csv
|
||||||
|
import matplotlib.pyplot as plt
|
||||||
|
import numpy as np
|
||||||
|
|
||||||
# ---------- 1. Модель клетки и лабиринта ----------
|
# ---------- 1. Модель клетки и лабиринта ----------
|
||||||
class Tile:
|
class Tile:
|
||||||
|
|
@ -343,9 +346,125 @@ class MoveAction(Action):
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
|
||||||
# ---------- 7. Главный интерактивный цикл (коммит №4) ----------
|
# ---------- 7. Экспериментальные функции ----------
|
||||||
|
def run_benchmark(maze_file, strategy, runs=5):
|
||||||
|
loader = TextLabyrinthLoader()
|
||||||
|
lab = loader.load(maze_file)
|
||||||
|
total_time = 0.0
|
||||||
|
total_visited = 0
|
||||||
|
total_len = 0
|
||||||
|
for _ in range(runs):
|
||||||
|
solver = LabyrinthSolver(lab)
|
||||||
|
solver.set_strategy(strategy)
|
||||||
|
stats = solver.solve()
|
||||||
|
if stats:
|
||||||
|
total_time += stats['time_ms']
|
||||||
|
total_visited += stats['visited']
|
||||||
|
total_len += stats['length']
|
||||||
|
return {
|
||||||
|
'time_ms': total_time / runs,
|
||||||
|
'visited': total_visited / runs,
|
||||||
|
'length': total_len / runs
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
def make_plots(results):
|
||||||
|
mazes = list({r['maze'] for r in results})
|
||||||
|
algos = ['BFS', 'DFS', 'AStar']
|
||||||
|
fig, axes = plt.subplots(1, 3, figsize=(15, 5))
|
||||||
|
x = np.arange(len(mazes))
|
||||||
|
width = 0.25
|
||||||
|
|
||||||
|
for i, algo in enumerate(algos):
|
||||||
|
times = []
|
||||||
|
for m in mazes:
|
||||||
|
val = next((r['time_ms'] for r in results if r['maze'] == m and r['algo'] == algo), 0)
|
||||||
|
times.append(val)
|
||||||
|
axes[0].bar(x + i*width, times, width, label=algo)
|
||||||
|
axes[0].set_xlabel('Лабиринт')
|
||||||
|
axes[0].set_ylabel('Время (мс)')
|
||||||
|
axes[0].set_title('Сравнение времени выполнения')
|
||||||
|
axes[0].set_xticks(x + width)
|
||||||
|
axes[0].set_xticklabels(mazes, rotation=45, ha='right')
|
||||||
|
axes[0].legend()
|
||||||
|
axes[0].grid(True, alpha=0.3)
|
||||||
|
|
||||||
|
for i, algo in enumerate(algos):
|
||||||
|
visited_vals = []
|
||||||
|
for m in mazes:
|
||||||
|
val = next((r['visited'] for r in results if r['maze'] == m and r['algo'] == algo), 0)
|
||||||
|
visited_vals.append(val)
|
||||||
|
axes[1].bar(x + i*width, visited_vals, width, label=algo)
|
||||||
|
axes[1].set_xlabel('Лабиринт')
|
||||||
|
axes[1].set_ylabel('Посещено клеток')
|
||||||
|
axes[1].set_title('Сравнение посещённых клеток')
|
||||||
|
axes[1].set_xticks(x + width)
|
||||||
|
axes[1].set_xticklabels(mazes, rotation=45, ha='right')
|
||||||
|
axes[1].legend()
|
||||||
|
axes[1].grid(True, alpha=0.3)
|
||||||
|
|
||||||
|
for i, algo in enumerate(algos):
|
||||||
|
lengths = []
|
||||||
|
for m in mazes:
|
||||||
|
val = next((r['length'] for r in results if r['maze'] == m and r['algo'] == algo), 0)
|
||||||
|
lengths.append(val)
|
||||||
|
axes[2].bar(x + i*width, lengths, width, label=algo)
|
||||||
|
axes[2].set_xlabel('Лабиринт')
|
||||||
|
axes[2].set_ylabel('Длина пути')
|
||||||
|
axes[2].set_title('Сравнение длины пути')
|
||||||
|
axes[2].set_xticks(x + width)
|
||||||
|
axes[2].set_xticklabels(mazes, rotation=45, ha='right')
|
||||||
|
axes[2].legend()
|
||||||
|
axes[2].grid(True, alpha=0.3)
|
||||||
|
|
||||||
|
plt.tight_layout()
|
||||||
|
plt.savefig('performance_comparison.png', dpi=150, bbox_inches='tight')
|
||||||
|
plt.show()
|
||||||
|
|
||||||
|
|
||||||
|
# ---------- 8. Главный блок ----------
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
# Загружаем лабиринт (предполагается, что файл maze/maze1.txt существует)
|
if len(sys.argv) > 1 and sys.argv[1] == 'experiment':
|
||||||
|
# Режим экспериментов
|
||||||
|
test_mazes = [
|
||||||
|
("maze/maze1.txt", "Small 10x6"),
|
||||||
|
("maze/maze10x10.txt", "Medium 10x10"),
|
||||||
|
("maze/maze20x20.txt", "Large 20x20"),
|
||||||
|
("maze/maze_empty.txt", "Empty 15x15"),
|
||||||
|
("maze/maze_no_exit.txt", "No exit 10x10")
|
||||||
|
]
|
||||||
|
strategies = [
|
||||||
|
("BFS", BFS()),
|
||||||
|
("DFS", DFS()),
|
||||||
|
("AStar", AStar())
|
||||||
|
]
|
||||||
|
results = []
|
||||||
|
for maze_path, maze_name in test_mazes:
|
||||||
|
print(f"Тестируем {maze_name}...")
|
||||||
|
for algo_name, algo in strategies:
|
||||||
|
try:
|
||||||
|
stats = run_benchmark(maze_path, algo, runs=3)
|
||||||
|
results.append({
|
||||||
|
'maze': maze_name,
|
||||||
|
'algo': algo_name,
|
||||||
|
'time_ms': stats['time_ms'],
|
||||||
|
'visited': stats['visited'],
|
||||||
|
'length': stats['length']
|
||||||
|
})
|
||||||
|
print(f" {algo_name}: время={stats['time_ms']:.3f}мс, посещено={stats['visited']:.0f}, длина={stats['length']:.0f}")
|
||||||
|
except Exception as e:
|
||||||
|
print(f" {algo_name}: ошибка - {e}")
|
||||||
|
# Сохраняем CSV
|
||||||
|
with open('experiment_results.csv', 'w', newline='', encoding='utf-8') as f:
|
||||||
|
writer = csv.DictWriter(f, fieldnames=['maze', 'algo', 'time_ms', 'visited', 'length'])
|
||||||
|
writer.writeheader()
|
||||||
|
writer.writerows(results)
|
||||||
|
# Строим графики
|
||||||
|
if results:
|
||||||
|
make_plots(results)
|
||||||
|
print("\nРезультаты сохранены в experiment_results.csv и performance_comparison.png")
|
||||||
|
else:
|
||||||
|
# Интерактивный режим
|
||||||
loader = TextLabyrinthLoader()
|
loader = TextLabyrinthLoader()
|
||||||
lab = loader.load("maze/maze1.txt")
|
lab = loader.load("maze/maze1.txt")
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user