2026-rff_mp/ZelentsovAV/task2/visualize.py
2026-05-24 20:33:47 +03:00

77 lines
2.6 KiB
Python

import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
from pathlib import Path
def plot_results(csv_file='experiment_results.csv'):
if not Path(csv_file).exists():
print(f"{csv_file} не найден. Сначала запустите main.py")
return
df = pd.read_csv(csv_file)
df = df[df['path_found'] == True]
if df.empty:
print("Нет данных для графиков")
return
mazes = [m.replace('.txt', '') for m in df['maze_file'].unique()]
strategies = df['strategy'].unique()
fig, axes = plt.subplots(1, 3, figsize=(14, 5))
fig.suptitle('Сравнение алгоритмов поиска в лабиринте', fontsize=14, fontweight='bold')
x = np.arange(len(mazes))
width = 0.25
colors = {'BFS': '#3498db', 'DFS': '#2ecc71', 'A*': '#e74c3c'}
for i, strategy in enumerate(strategies):
times, visited, lengths = [], [], []
for maze in df['maze_file'].unique():
data = df[(df['strategy'] == strategy) & (df['maze_file'] == maze)]
if not data.empty:
times.append(data['time_mean'].values[0])
visited.append(data['visited_mean'].values[0])
lengths.append(data['path_length_mean'].values[0])
else:
times.append(0)
visited.append(0)
lengths.append(0)
axes[0].bar(x + i*width, times, width, label=strategy,
color=colors.get(strategy, 'gray'), alpha=0.7)
axes[1].bar(x + i*width, visited, width, label=strategy,
color=colors.get(strategy, 'gray'), alpha=0.7)
axes[2].bar(x + i*width, lengths, width, label=strategy,
color=colors.get(strategy, 'gray'), alpha=0.7)
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)
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)
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('experiment_results.png', dpi=150, bbox_inches='tight')
plt.show()
if __name__ == "__main__":
plot_results()