88 lines
3.1 KiB
Python
88 lines
3.1 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=(15, 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()
|
|
|
|
print("✅ Графики сохранены в experiment_results.png")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
plot_results() |