написано тестирование
This commit is contained in:
parent
8331065da2
commit
8a6344f301
|
|
@ -31,7 +31,7 @@ class AStarStrategy(PathFindingStrategy):
|
|||
path.append(current)
|
||||
current = parents[current]
|
||||
path.reverse()
|
||||
return path
|
||||
return path, len(parents)
|
||||
|
||||
childs = maze.getNeighbors(u)
|
||||
for child in childs:
|
||||
|
|
|
|||
|
|
@ -21,7 +21,7 @@ class BFSStrategy(PathFindingStrategy):
|
|||
path.append(current)
|
||||
current = parents[current]
|
||||
path.reverse()
|
||||
return path
|
||||
return path, len(parents)
|
||||
|
||||
childs = maze.getNeighbors(u)
|
||||
for child in childs:
|
||||
|
|
|
|||
|
|
@ -11,9 +11,12 @@ class DFSStrategy(PathFindingStrategy):
|
|||
visited = set()
|
||||
path = []
|
||||
|
||||
count_cell = 0
|
||||
|
||||
def dfs(root: Cell) -> bool:
|
||||
visited.add(root)
|
||||
path.append(root)
|
||||
count_cell += 1
|
||||
|
||||
if root == exit:
|
||||
return True
|
||||
|
|
@ -28,6 +31,6 @@ class DFSStrategy(PathFindingStrategy):
|
|||
return False
|
||||
|
||||
if dfs(start):
|
||||
return path
|
||||
return path, count_cell
|
||||
|
||||
return []
|
||||
|
|
|
|||
|
|
@ -12,6 +12,10 @@ class TextFileMazeBuilder(MazeBuilder):
|
|||
def __init__(self):
|
||||
self._maze = None
|
||||
|
||||
@property
|
||||
def maze(self):
|
||||
return self._maze
|
||||
|
||||
def buildFromFile(self, filename: str):
|
||||
|
||||
with open(filename, mode='r', encoding='utf-8') as file:
|
||||
|
|
|
|||
|
|
@ -1,5 +1,6 @@
|
|||
from MazeBuilder import TextFileMazeBuilder
|
||||
from BreadthFirstSearch import BFSStrategy
|
||||
from Maze import Maze
|
||||
|
||||
|
||||
maze1 = TextFileMazeBuilder().buildFromFile("text.txt")
|
||||
|
|
|
|||
124
ProninVV/task-2-oop/test.py
Normal file
124
ProninVV/task-2-oop/test.py
Normal file
|
|
@ -0,0 +1,124 @@
|
|||
import csv
|
||||
import time
|
||||
import os
|
||||
import matplotlib.pyplot as plt
|
||||
import numpy as np
|
||||
|
||||
from MazeBuilder import TextFileMazeBuilder
|
||||
from MazeSolver import MazeSolver, SearchStats
|
||||
from DepthFirstSearch import DFSStrategy
|
||||
from BreadthFirstSearch import BFSStrategy
|
||||
from Deikstra import DeikstraFind
|
||||
from AStarStrategy import AStarStrategy
|
||||
|
||||
|
||||
def run_benchmarks():
|
||||
files = ["maze_small.txt", "maze_empty.txt",
|
||||
"maze_no_exit.txt", "maze_medium.txt", "maze_large.txt"]
|
||||
strategies = {
|
||||
"BFS": BFSStrategy(),
|
||||
"DFS": DFSStrategy(),
|
||||
"A*": AStarStrategy(),
|
||||
"Deikstra": DeikstraFind()
|
||||
}
|
||||
|
||||
NUM_RUNS = 5
|
||||
results = []
|
||||
|
||||
print("Запуск экспериментов...")
|
||||
|
||||
for file in files:
|
||||
if not os.path.exists(file):
|
||||
print(f"Файл {file} не найден. Пропуск.")
|
||||
continue
|
||||
|
||||
for name, strategy in strategies.items():
|
||||
total_time = 0.0
|
||||
visited_counts = []
|
||||
path_lengths = []
|
||||
|
||||
for _ in range(NUM_RUNS):
|
||||
# Пересоздаем лабиринт
|
||||
builder = TextFileMazeBuilder()
|
||||
builder.buildFromFile(file)
|
||||
maze = builder.maze
|
||||
|
||||
solver = MazeSolver(maze, strategy)
|
||||
stats = solver.solve()
|
||||
|
||||
total_time += stats.execution_time
|
||||
visited_counts.append(stats.visited_count)
|
||||
path_lengths.append(stats.path_length)
|
||||
|
||||
# средние значения
|
||||
avg_time = total_time / NUM_RUNS
|
||||
avg_visited = int(np.mean(visited_counts))
|
||||
avg_path = int(np.mean(path_lengths))
|
||||
|
||||
results.append({
|
||||
"лабиринт": file,
|
||||
"стратегия": name,
|
||||
"время_мс": round(avg_time, 4),
|
||||
"посещено_клеток": avg_visited,
|
||||
"длина_пути": avg_path
|
||||
})
|
||||
|
||||
# Запись в CSV
|
||||
csv_file = "maze_benchmark_results.csv"
|
||||
with open(csv_file, mode="w", encoding="utf-8", newline="") as f:
|
||||
writer = csv.DictWriter(f, fieldnames=[
|
||||
"лабиринт", "стратегия", "время_мс", "посещено_клеток", "длина_пути"])
|
||||
writer.writeheader()
|
||||
writer.writerows(results)
|
||||
|
||||
print(f"Результаты успешно сохранены в {csv_file}")
|
||||
return results
|
||||
|
||||
# Построение графиков
|
||||
|
||||
|
||||
def plot_results(results):
|
||||
print("Генерация графиков...")
|
||||
mazes = sorted(list(set(r["лабиринт"] for r in results)))
|
||||
strategies = ["BFS", "DFS", "A*"]
|
||||
|
||||
# График Количество посещенных клеток
|
||||
fig, ax = plt.subplots(figsize=(10, 6))
|
||||
x = np.arange(len(mazes))
|
||||
width = 0.25
|
||||
|
||||
for i, strat in enumerate(strategies):
|
||||
visited = [next(r["посещено_клеток"] for r in results if r["лабиринт"]
|
||||
== m and r["стратегия"] == strat) for m in mazes]
|
||||
ax.bar(x + i*width, visited, width, label=strat)
|
||||
|
||||
ax.set_ylabel('Количество посещенных клеток')
|
||||
ax.set_title('Сравнение эффективности обхода лабиринтов (меньше = лучше)')
|
||||
ax.set_xticks(x + width)
|
||||
ax.set_xticklabels(mazes, rotation=15)
|
||||
ax.legend()
|
||||
plt.tight_layout()
|
||||
plt.savefig("benchmark_visited_cells.png", dpi=300)
|
||||
plt.close()
|
||||
|
||||
# График Время выполнения
|
||||
fig, ax = plt.subplots(figsize=(10, 6))
|
||||
for i, strat in enumerate(strategies):
|
||||
times = [next(r["время_мс"] for r in results if r["лабиринт"]
|
||||
== m and r["стратегия"] == strat) for m in mazes]
|
||||
ax.bar(x + i*width, times, width, label=strat)
|
||||
|
||||
ax.set_ylabel('Время выполнения (мс)')
|
||||
ax.set_title('Сравнение времени работы алгоритмов')
|
||||
ax.set_xticks(x + width)
|
||||
ax.set_xticklabels(mazes, rotation=15)
|
||||
ax.legend()
|
||||
plt.tight_layout()
|
||||
plt.savefig("benchmark_execution_time.png", dpi=300)
|
||||
plt.close()
|
||||
print("Графики сохранены в текущую директорию.")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
data = run_benchmarks()
|
||||
plot_results(data)
|
||||
Loading…
Reference in New Issue
Block a user