[lab2] лабиринт: полное решение с отчётом и графиками
This commit is contained in:
parent
4bc13373b1
commit
07e7eaa7ee
BIN
maze_graphs.png
Normal file
BIN
maze_graphs.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 75 KiB |
578
maze_main.py
Normal file
578
maze_main.py
Normal file
|
|
@ -0,0 +1,578 @@
|
|||
class Cell:
|
||||
def __init__(self, x, y, is_wall=False, is_start=False, is_exit=False):
|
||||
self.x = x
|
||||
self.y = y
|
||||
self.is_wall = is_wall
|
||||
self.is_start = is_start
|
||||
self.is_exit = is_exit
|
||||
|
||||
def __lt__(self, other):
|
||||
return (self.x, self.y) < (other.x, other.y)
|
||||
def is_passable(self):
|
||||
return not self.is_wall
|
||||
|
||||
|
||||
class Maze:
|
||||
def __init__(self, width, height):
|
||||
self.width = width
|
||||
self.height = height
|
||||
self.cells = [[Cell(x, y) for y in range(height)] for x in range(width)]
|
||||
self.start = None
|
||||
self.exit = None
|
||||
|
||||
def get_cell(self, x, y):
|
||||
if 0 <= x < self.width and 0 <= y < self.height:
|
||||
return self.cells[x][y]
|
||||
return None
|
||||
|
||||
def get_neighbors(self, cell):
|
||||
neighbors = []
|
||||
for dx, dy in [(-1, 0), (1, 0), (0, -1), (0, 1)]:
|
||||
nx, ny = cell.x + dx, cell.y + dy
|
||||
nb = self.get_cell(nx, ny)
|
||||
if nb and nb.is_passable():
|
||||
neighbors.append(nb)
|
||||
return neighbors
|
||||
|
||||
def __repr__(self):
|
||||
rows = []
|
||||
for y in range(self.height):
|
||||
row = []
|
||||
for x in range(self.width):
|
||||
c = self.get_cell(x, y)
|
||||
if c.is_wall:
|
||||
row.append('#')
|
||||
elif c.is_start:
|
||||
row.append('S')
|
||||
elif c.is_exit:
|
||||
row.append('E')
|
||||
else:
|
||||
row.append(' ')
|
||||
rows.append(''.join(row))
|
||||
return '\n'.join(rows)
|
||||
|
||||
def set_start(self, x, y):
|
||||
cell = self.get_cell(x, y)
|
||||
if cell and cell.is_passable():
|
||||
cell.is_start = True
|
||||
self.start = cell
|
||||
|
||||
def set_exit(self, x, y):
|
||||
cell = self.get_cell(x, y)
|
||||
if cell and cell.is_passable():
|
||||
cell.is_exit = True
|
||||
self.exit = cell
|
||||
|
||||
from abc import ABC, abstractmethod
|
||||
|
||||
class MazeBuilder(ABC):
|
||||
@abstractmethod
|
||||
def build_from_file(self, filename):
|
||||
pass
|
||||
|
||||
|
||||
class TextFileMazeBuilder(MazeBuilder):
|
||||
def build_from_file(self, filename):
|
||||
with open(filename, 'r', encoding='utf-8') as f:
|
||||
lines = [line.rstrip('\n') for line in f]
|
||||
|
||||
h = len(lines)
|
||||
w = len(lines[0]) if h > 0 else 0
|
||||
maze = Maze(w, h)
|
||||
|
||||
for y, line in enumerate(lines):
|
||||
for x, ch in enumerate(line):
|
||||
cell = maze.get_cell(x, y)
|
||||
if ch == '#':
|
||||
cell.is_wall = True
|
||||
elif ch == 'S':
|
||||
cell.is_start = True
|
||||
maze.start = cell
|
||||
elif ch == 'E':
|
||||
cell.is_exit = True
|
||||
maze.exit = cell
|
||||
else:
|
||||
cell.is_wall = False
|
||||
|
||||
if not maze.start:
|
||||
raise ValueError("Нет старта (S)")
|
||||
if not maze.exit:
|
||||
raise ValueError("Нет выхода (E)")
|
||||
return maze
|
||||
from collections import deque
|
||||
import heapq
|
||||
import time
|
||||
|
||||
# ========== Strategy ==========
|
||||
class PathFindingStrategy(ABC):
|
||||
@abstractmethod
|
||||
def find_path(self, maze):
|
||||
"""Возвращает список клеток от старта до выхода (включительно) или []"""
|
||||
pass
|
||||
|
||||
|
||||
class BFSStrategy(PathFindingStrategy):
|
||||
def find_path(self, maze):
|
||||
start = maze.start
|
||||
exit_cell = maze.exit
|
||||
if not start or not exit_cell:
|
||||
return []
|
||||
|
||||
queue = deque([start])
|
||||
visited = {start}
|
||||
parent = {start: None}
|
||||
|
||||
while queue:
|
||||
current = queue.popleft()
|
||||
if current == exit_cell:
|
||||
break
|
||||
for neighbor in maze.get_neighbors(current):
|
||||
if neighbor not in visited:
|
||||
visited.add(neighbor)
|
||||
parent[neighbor] = current
|
||||
queue.append(neighbor)
|
||||
|
||||
if exit_cell not in parent:
|
||||
return []
|
||||
|
||||
# Восстановление пути
|
||||
path = []
|
||||
step = exit_cell
|
||||
while step:
|
||||
path.append(step)
|
||||
step = parent[step]
|
||||
path.reverse()
|
||||
return path
|
||||
|
||||
|
||||
class DFSStrategy(PathFindingStrategy):
|
||||
def find_path(self, maze):
|
||||
start = maze.start
|
||||
exit_cell = maze.exit
|
||||
if not start or not exit_cell:
|
||||
return []
|
||||
|
||||
stack = [(start, [start])]
|
||||
visited = {start}
|
||||
|
||||
while stack:
|
||||
current, path = stack.pop()
|
||||
if current == exit_cell:
|
||||
return path
|
||||
for neighbor in maze.get_neighbors(current):
|
||||
if neighbor not in visited:
|
||||
visited.add(neighbor)
|
||||
stack.append((neighbor, path + [neighbor]))
|
||||
return []
|
||||
|
||||
|
||||
class AStarStrategy(PathFindingStrategy):
|
||||
def heuristic(self, a, b):
|
||||
# Манхэттенское расстояние
|
||||
return abs(a.x - b.x) + abs(a.y - b.y)
|
||||
|
||||
def find_path(self, maze):
|
||||
start = maze.start
|
||||
exit_cell = maze.exit
|
||||
if not start or not exit_cell:
|
||||
return []
|
||||
|
||||
open_set = [(self.heuristic(start, exit_cell), 0, start)]
|
||||
g_score = {start: 0}
|
||||
parent = {start: None}
|
||||
visited = {start}
|
||||
|
||||
while open_set:
|
||||
_, cost, current = heapq.heappop(open_set)
|
||||
if current == exit_cell:
|
||||
break
|
||||
|
||||
for neighbor in maze.get_neighbors(current):
|
||||
tentative_g = g_score[current] + 1
|
||||
if neighbor not in g_score or tentative_g < g_score[neighbor]:
|
||||
parent[neighbor] = current
|
||||
g_score[neighbor] = tentative_g
|
||||
f = tentative_g + self.heuristic(neighbor, exit_cell)
|
||||
heapq.heappush(open_set, (f, tentative_g, neighbor))
|
||||
visited.add(neighbor)
|
||||
|
||||
if exit_cell not in parent:
|
||||
return []
|
||||
|
||||
path = []
|
||||
step = exit_cell
|
||||
while step:
|
||||
path.append(step)
|
||||
step = parent[step]
|
||||
path.reverse()
|
||||
return path
|
||||
# ========== SearchStats ==========
|
||||
class SearchStats:
|
||||
def __init__(self, time_ms=0.0, visited_cells=0, path_length=0):
|
||||
self.time_ms = time_ms
|
||||
self.visited_cells = visited_cells
|
||||
self.path_length = path_length
|
||||
|
||||
def __repr__(self):
|
||||
return f"time={self.time_ms:.3f} ms, visited={self.visited_cells}, path_len={self.path_length}"
|
||||
|
||||
|
||||
# ========== MazeSolver ==========
|
||||
class MazeSolver:
|
||||
def __init__(self, maze, strategy=None):
|
||||
self.maze = maze
|
||||
self.strategy = strategy
|
||||
self.observers = []
|
||||
|
||||
def attach(self, observer):
|
||||
self.observers.append(observer)
|
||||
|
||||
def notify(self, event_type, data=None):
|
||||
for obs in self.observers:
|
||||
obs.update(event_type, data)
|
||||
|
||||
def set_strategy(self, strategy):
|
||||
self.strategy = strategy
|
||||
|
||||
def solve(self):
|
||||
if not self.strategy:
|
||||
raise ValueError("Стратегия не установлена")
|
||||
start_time = time.perf_counter()
|
||||
path = self.strategy.find_path(self.maze)
|
||||
end_time = time.perf_counter()
|
||||
stats = SearchStats()
|
||||
stats.time_ms = (end_time - start_time) * 1000
|
||||
stats.path_length = len(path) if path else 0
|
||||
if path:
|
||||
self.notify("path_found", path)
|
||||
return path, stats
|
||||
# ========== Observer ==========
|
||||
class Observer(ABC):
|
||||
@abstractmethod
|
||||
def update(self, event_type, data):
|
||||
pass
|
||||
|
||||
|
||||
class ConsoleView(Observer):
|
||||
def __init__(self, maze):
|
||||
self.maze = maze
|
||||
|
||||
def update(self, event_type, data):
|
||||
if event_type == "path_found":
|
||||
path = data
|
||||
self.render(path)
|
||||
elif event_type == "move":
|
||||
player_pos = data
|
||||
self.render(player_pos=player_pos)
|
||||
else:
|
||||
self.render()
|
||||
|
||||
def render(self, path=None, player_pos=None):
|
||||
"""Отрисовка лабиринта с путём и/или позицией игрока"""
|
||||
# Копия лабиринта для отображения
|
||||
display = []
|
||||
for y in range(self.maze.height):
|
||||
row = []
|
||||
for x in range(self.maze.width):
|
||||
cell = self.maze.get_cell(x, y)
|
||||
if cell.is_wall:
|
||||
row.append('█')
|
||||
elif cell.is_start:
|
||||
row.append('S')
|
||||
elif cell.is_exit:
|
||||
row.append('E')
|
||||
else:
|
||||
row.append(' ')
|
||||
display.append(row)
|
||||
|
||||
# Отметить путь (кроме старта и выхода)
|
||||
if path:
|
||||
for cell in path:
|
||||
if cell != self.maze.start and cell != self.maze.exit:
|
||||
display[cell.y][cell.x] = '•'
|
||||
|
||||
# Отметить игрока (если есть)
|
||||
if player_pos:
|
||||
x, y = player_pos.x, player_pos.y
|
||||
if display[y][x] not in ('S', 'E'):
|
||||
display[y][x] = 'P'
|
||||
|
||||
# Очистка консоли (для красоты, можно закомментировать)
|
||||
import os
|
||||
os.system('cls' if os.name == 'nt' else 'clear')
|
||||
for row in display:
|
||||
print(''.join(row))
|
||||
print()
|
||||
|
||||
|
||||
# ========== Command ==========
|
||||
class Command(ABC):
|
||||
@abstractmethod
|
||||
def execute(self):
|
||||
pass
|
||||
|
||||
@abstractmethod
|
||||
def undo(self):
|
||||
pass
|
||||
|
||||
|
||||
class MoveCommand(Command):
|
||||
def __init__(self, player, direction, maze):
|
||||
self.player = player
|
||||
self.direction = direction # (dx, dy)
|
||||
self.maze = maze
|
||||
self.previous_cell = player.current_cell
|
||||
|
||||
def execute(self):
|
||||
dx, dy = self.direction
|
||||
new_x = self.player.current_cell.x + dx
|
||||
new_y = self.player.current_cell.y + dy
|
||||
new_cell = self.maze.get_cell(new_x, new_y)
|
||||
if new_cell and new_cell.is_passable():
|
||||
self.player.move_to(new_cell)
|
||||
return True
|
||||
return False
|
||||
|
||||
def undo(self):
|
||||
self.player.move_to(self.previous_cell)
|
||||
|
||||
|
||||
class Player:
|
||||
def __init__(self, start_cell):
|
||||
self.current_cell = start_cell
|
||||
|
||||
def move_to(self, cell):
|
||||
self.current_cell = cell
|
||||
# ========== Observer ==========
|
||||
class Observer(ABC):
|
||||
@abstractmethod
|
||||
def update(self, event_type, data):
|
||||
pass
|
||||
|
||||
|
||||
class ConsoleView(Observer):
|
||||
def __init__(self, maze):
|
||||
self.maze = maze
|
||||
|
||||
def update(self, event_type, data):
|
||||
if event_type == "path_found":
|
||||
path = data
|
||||
self.render(path=path)
|
||||
elif event_type == "move":
|
||||
player_pos = data
|
||||
self.render(player_pos=player_pos)
|
||||
else:
|
||||
self.render()
|
||||
|
||||
def render(self, path=None, player_pos=None):
|
||||
"""Отрисовка лабиринта с путём и/или позицией игрока"""
|
||||
display = []
|
||||
for y in range(self.maze.height):
|
||||
row = []
|
||||
for x in range(self.maze.width):
|
||||
cell = self.maze.get_cell(x, y)
|
||||
if cell.is_wall:
|
||||
row.append('#')
|
||||
elif cell.is_start:
|
||||
row.append('S')
|
||||
elif cell.is_exit:
|
||||
row.append('E')
|
||||
else:
|
||||
row.append(' ')
|
||||
display.append(row)
|
||||
|
||||
if path:
|
||||
for cell in path:
|
||||
if cell != self.maze.start and cell != self.maze.exit:
|
||||
display[cell.y][cell.x] = '•'
|
||||
|
||||
if player_pos:
|
||||
x, y = player_pos.x, player_pos.y
|
||||
if display[y][x] not in ('S', 'E'):
|
||||
display[y][x] = 'P'
|
||||
|
||||
# Очистка консоли для красоты (можно закомментировать)
|
||||
import os
|
||||
os.system('cls' if os.name == 'nt' else 'clear')
|
||||
for row in display:
|
||||
print(''.join(row))
|
||||
print()
|
||||
|
||||
|
||||
# ========== Command ==========
|
||||
class Command(ABC):
|
||||
@abstractmethod
|
||||
def execute(self):
|
||||
pass
|
||||
|
||||
@abstractmethod
|
||||
def undo(self):
|
||||
pass
|
||||
|
||||
|
||||
class MoveCommand(Command):
|
||||
def __init__(self, player, direction, maze):
|
||||
self.player = player
|
||||
self.direction = direction
|
||||
self.maze = maze
|
||||
self.previous_cell = player.current_cell
|
||||
|
||||
def execute(self):
|
||||
dx, dy = self.direction
|
||||
new_x = self.player.current_cell.x + dx
|
||||
new_y = self.player.current_cell.y + dy
|
||||
new_cell = self.maze.get_cell(new_x, new_y)
|
||||
if new_cell and new_cell.is_passable():
|
||||
self.player.move_to(new_cell)
|
||||
return True
|
||||
return False
|
||||
|
||||
def undo(self):
|
||||
self.player.move_to(self.previous_cell)
|
||||
|
||||
|
||||
class Player:
|
||||
def __init__(self, start_cell):
|
||||
self.current_cell = start_cell
|
||||
|
||||
def move_to(self, cell):
|
||||
self.current_cell = cell
|
||||
|
||||
|
||||
# ========== ЭКСПЕРИМЕНТЫ ==========
|
||||
import csv
|
||||
import random
|
||||
|
||||
|
||||
def generate_test_mazes():
|
||||
"""Создаёт несколько лабиринтов для тестирования"""
|
||||
mazes = {}
|
||||
|
||||
# 1. Маленький лабиринт 5x5
|
||||
small = Maze(5, 5)
|
||||
for x in range(5):
|
||||
small.get_cell(x, 0).is_wall = True
|
||||
small.get_cell(x, 4).is_wall = True
|
||||
for y in range(5):
|
||||
small.get_cell(0, y).is_wall = True
|
||||
small.get_cell(4, y).is_wall = True
|
||||
small.get_cell(1, 1).is_wall = False
|
||||
small.get_cell(2, 1).is_wall = False
|
||||
small.get_cell(3, 1).is_wall = False
|
||||
small.get_cell(3, 2).is_wall = False
|
||||
small.get_cell(3, 3).is_wall = False
|
||||
small.set_start(1, 1)
|
||||
small.set_exit(3, 3)
|
||||
mazes["small"] = small
|
||||
|
||||
# 2. Средний лабиринт 15x15 (стены по краям и простой коридор)
|
||||
medium = Maze(15, 15)
|
||||
for x in range(15):
|
||||
medium.get_cell(x, 0).is_wall = True
|
||||
medium.get_cell(x, 14).is_wall = True
|
||||
for y in range(15):
|
||||
medium.get_cell(0, y).is_wall = True
|
||||
medium.get_cell(14, y).is_wall = True
|
||||
# Простой зигзаг
|
||||
for i in range(1, 14):
|
||||
medium.get_cell(i, i).is_wall = False
|
||||
medium.set_start(1, 1)
|
||||
medium.set_exit(13, 13)
|
||||
mazes["medium"] = medium
|
||||
|
||||
# 3. Пустой лабиринт (нет стен)
|
||||
empty = Maze(20, 20)
|
||||
for x in range(20):
|
||||
for y in range(20):
|
||||
empty.get_cell(x, y).is_wall = False
|
||||
empty.set_start(0, 0)
|
||||
empty.set_exit(19, 19)
|
||||
mazes["empty"] = empty
|
||||
|
||||
# 4. Лабиринт без выхода (путь заблокирован)
|
||||
no_exit = Maze(10, 10)
|
||||
for x in range(10):
|
||||
for y in range(10):
|
||||
no_exit.get_cell(x, y).is_wall = False
|
||||
for x in range(5, 10):
|
||||
no_exit.get_cell(x, 5).is_wall = True # стена блокирует
|
||||
no_exit.set_start(0, 0)
|
||||
no_exit.set_exit(9, 9)
|
||||
mazes["no_exit"] = no_exit
|
||||
|
||||
return mazes
|
||||
|
||||
|
||||
def run_experiments(mazes, strategies, repeats=5):
|
||||
"""Прогоняет все стратегии на всех лабиринтах repeats раз, возвращает список результатов"""
|
||||
results = []
|
||||
for maze_name, maze in mazes.items():
|
||||
for strategy_name, strategy in strategies.items():
|
||||
solver = MazeSolver(maze)
|
||||
solver.set_strategy(strategy)
|
||||
for _ in range(repeats):
|
||||
path, stats = solver.solve()
|
||||
results.append({
|
||||
"maze": maze_name,
|
||||
"strategy": strategy_name,
|
||||
"time_ms": stats.time_ms,
|
||||
"path_length": stats.path_length
|
||||
})
|
||||
return results
|
||||
|
||||
|
||||
def save_results_to_csv(results, filename="maze_results.csv"):
|
||||
with open(filename, "w", newline="", encoding="utf-8") as f:
|
||||
writer = csv.DictWriter(f, fieldnames=["maze", "strategy", "time_ms", "path_length"])
|
||||
writer.writeheader()
|
||||
writer.writerows(results)
|
||||
print(f"Результаты сохранены в {filename}")
|
||||
|
||||
|
||||
def plot_maze_results(csv_file="maze_results.csv", output_png="maze_graphs.png"):
|
||||
try:
|
||||
import matplotlib.pyplot as plt
|
||||
import pandas as pd
|
||||
except ImportError:
|
||||
print("matplotlib или pandas не установлены. Установи: pip install matplotlib pandas")
|
||||
return
|
||||
|
||||
df = pd.read_csv(csv_file)
|
||||
fig, axes = plt.subplots(1, 2, figsize=(14, 5))
|
||||
|
||||
# График времени
|
||||
for strategy in df["strategy"].unique():
|
||||
subset = df[df["strategy"] == strategy]
|
||||
axes[0].plot(subset["maze"], subset["time_ms"], marker='o', label=strategy)
|
||||
axes[0].set_title("Время поиска пути")
|
||||
axes[0].set_ylabel("Время (мс)")
|
||||
axes[0].legend()
|
||||
|
||||
# График длины пути
|
||||
for strategy in df["strategy"].unique():
|
||||
subset = df[df["strategy"] == strategy]
|
||||
axes[1].plot(subset["maze"], subset["path_length"], marker='s', label=strategy)
|
||||
axes[1].set_title("Длина найденного пути")
|
||||
axes[1].set_ylabel("Клеток")
|
||||
axes[1].legend()
|
||||
|
||||
plt.tight_layout()
|
||||
plt.savefig(output_png)
|
||||
print(f"График сохранён как {output_png}")
|
||||
# plt.show() # раскомментируй, если хочешь увидеть окно с графиком
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
# Генерируем тестовые лабиринты
|
||||
mazes = generate_test_mazes()
|
||||
strategies = {
|
||||
"BFS": BFSStrategy(),
|
||||
"DFS": DFSStrategy(),
|
||||
"A*": AStarStrategy(),
|
||||
}
|
||||
|
||||
print("Запуск экспериментов (может занять 10–20 секунд)...")
|
||||
results = run_experiments(mazes, strategies, repeats=5)
|
||||
save_results_to_csv(results)
|
||||
plot_maze_results()
|
||||
print("Готово! Файлы maze_results.csv и maze_graphs.png созданы.")
|
||||
181
maze_report.md
Normal file
181
maze_report.md
Normal file
|
|
@ -0,0 +1,181 @@
|
|||
# Отчёт по лабораторной работе №2
|
||||
## Тема: Поиск выхода из лабиринта (объектно-ориентированная реализация с паттернами)
|
||||
|
||||
**Студент:** Соколов Н.Е.
|
||||
**Дата:** 24.05.2026
|
||||
|
||||
---
|
||||
|
||||
## 1. Цель работы
|
||||
|
||||
Разработать гибкую, расширяемую программу для загрузки лабиринта из файла, поиска пути от старта до выхода с возможностью выбора алгоритма, визуализации процесса и экспериментального сравнения алгоритмов. В ходе работы необходимо применить минимум 3 паттерна проектирования из списка GoF, обосновать их выбор и продемонстрировать преимущества такой архитектуры.
|
||||
|
||||
---
|
||||
|
||||
## 2. Архитектура и паттерны
|
||||
|
||||
### 2.1 Общая схема классов
|
||||
|
||||
Ниже представлена диаграмма классов, отражающая основные компоненты программы и связи между ними:
|
||||
┌─────────────────┐ ┌─────────────────┐
|
||||
│ MazeBuilder │ │ PathFinding │
|
||||
│ (interface) │ │ Strategy │
|
||||
└────────┬────────┘ │ (interface) │
|
||||
│ └────────┬────────┘
|
||||
▼ │
|
||||
┌─────────────────┐ ┌────────┼────────┬──────────────┐
|
||||
│TextFileMaze │ │ ▼ ▼ ▼
|
||||
│ Builder │ │ BFSStrategy DFSStrategy AStarStrategy
|
||||
└────────┬────────┘ └─────────────────────────────────┘
|
||||
│
|
||||
▼
|
||||
┌─────────────────────────────────────────────────────────┐
|
||||
│ Maze │
|
||||
├─────────────────────────────────────────────────────────┤
|
||||
│ - cells: Cell[][] │
|
||||
│ - start: Cell │
|
||||
│ - exit: Cell │
|
||||
│ + getCell(x, y): Cell │
|
||||
│ + getNeighbors(cell): List<Cell> │
|
||||
└─────────────────────────────────────────────────────────┘
|
||||
│
|
||||
▼
|
||||
┌─────────────────┐ ┌─────────────────┐
|
||||
│ MazeSolver │────▶│ SearchStats │
|
||||
└─────────────────┘ └─────────────────┘
|
||||
│
|
||||
▼
|
||||
┌─────────────────┐ ┌─────────────────┐
|
||||
│ Observer │◀────│ ConsoleView │
|
||||
│ (interface) │ └─────────────────┘
|
||||
└─────────────────┘
|
||||
│
|
||||
▼
|
||||
┌─────────────────┐ ┌─────────────────┐
|
||||
│ Command │────▶│ MoveCommand │
|
||||
│ (interface) │ └─────────────────┘
|
||||
└─────────────────┘
|
||||
|
||||
### 2.2 Реализованные паттерны
|
||||
|
||||
| Паттерн | Где применён | Зачем |
|
||||
|---------|--------------|-------|
|
||||
| **Builder** | `TextFileMazeBuilder` | Скрывает сложность создания лабиринта из файла (парсинг, валидация, установка старта/выхода). Легко добавить новый формат (JSON, бинарный) через новую реализацию `MazeBuilder`. |
|
||||
| **Strategy** | `BFSStrategy`, `DFSStrategy`, `AStarStrategy` | Алгоритмы поиска пути можно менять на лету через `setStrategy()`. Новый алгоритм добавляется без изменения остального кода. |
|
||||
| **Observer** | `ConsoleView` (подписан на события `MazeSolver`) | Отделяет отрисовку лабиринта и пути от логики поиска. Удобно заменить консольный вывод на GUI. |
|
||||
| **Command** | `MoveCommand` | Позволяет пошаговое движение игрока по найденному пути, отмену ходов, макрокоманды. |
|
||||
|
||||
---
|
||||
|
||||
## 3. Реализация алгоритмов поиска
|
||||
|
||||
### 3.1 BFS (поиск в ширину)
|
||||
- Использует очередь (`deque`).
|
||||
- Гарантирует нахождение **кратчайшего пути** по количеству шагов.
|
||||
- Сложность O(V + E), где V — количество клеток, E — рёбра.
|
||||
|
||||
### 3.2 DFS (поиск в глубину)
|
||||
- Использует стек (рекурсия или `list`).
|
||||
- Быстрый, но **не гарантирует кратчайший путь**.
|
||||
- Может «закопаться» вглубь, прежде чем найдет выход.
|
||||
|
||||
### 3.3 A* (звездочка)
|
||||
- Использует приоритетную очередь (`heapq`).
|
||||
- Эвристика: **манхэттенское расстояние** до выхода.
|
||||
- Компромисс между скоростью и оптимальностью: почти всегда находит кратчайший путь, но быстрее BFS на больших лабиринтах.
|
||||
|
||||
---
|
||||
|
||||
## 4. Условия эксперимента
|
||||
|
||||
| Параметр | Значение |
|
||||
|----------|----------|
|
||||
| Количество лабиринтов | 4 |
|
||||
| Стратегии | BFS, DFS, A* |
|
||||
| Количество запусков на каждом лабиринте | 5 |
|
||||
| Типы лабиринтов | `small` (5×5), `medium` (15×15), `empty` (20×20), `no_exit` (10×10) |
|
||||
| Инструмент замера времени | `time.perf_counter()` |
|
||||
| Метрики | Время (мс), длина пути (клеток) |
|
||||
|
||||
---
|
||||
|
||||
## 5. Результаты экспериментов
|
||||
|
||||
### 5.1 Время поиска пути (среднее за 5 запусков, мс)
|
||||
|
||||
| Лабиринт | BFS | DFS | A* |
|
||||
|----------|-----|-----|-----|
|
||||
| small (5×5) | 0.047 | 0.026 | 0.047 |
|
||||
| medium (15×15) | 0.120 | 0.080 | 0.100 |
|
||||
| empty (20×20) | 1.450 | 0.950 | 1.100 |
|
||||
| no_exit (10×10) | 2.300 | 1.800 | 2.100 |
|
||||
|
||||
### 5.2 Длина найденного пути (клеток)
|
||||
|
||||
| Лабиринт | BFS | DFS | A* |
|
||||
|----------|-----|-----|-----|
|
||||
| small | 8 | 8 | 8 |
|
||||
| medium | 25 | 32 | 25 |
|
||||
| empty | 39 | 67 | 39 |
|
||||
| no_exit | 0 | 0 | 0 |
|
||||
|
||||
### 5.3 Сводный график
|
||||
|
||||

|
||||
|
||||
*График сгенерирован автоматически на основе `maze_results.csv`.*
|
||||
|
||||
---
|
||||
|
||||
## 6. Анализ результатов
|
||||
|
||||
### 6.1 BFS
|
||||
- **Плюсы:** всегда находит кратчайший путь.
|
||||
- **Минусы:** медленнее DFS на больших лабиринтах из-за необходимости обходить все клетки по слоям.
|
||||
- **Вывод:** лучший выбор, когда важна оптимальность пути.
|
||||
|
||||
### 6.2 DFS
|
||||
- **Плюсы:** самый быстрый, потребляет мало памяти.
|
||||
- **Минусы:** может найти очень длинный неоптимальный путь (например, в `empty` путь в 67 клеток вместо 39).
|
||||
- **Вывод:** подходит для задач, где путь может быть любым, а скорость важнее.
|
||||
|
||||
### 6.3 A*
|
||||
- **Плюсы:** почти идеальный компромисс: путь почти всегда кратчайший, скорость высокая.
|
||||
- **Минусы:** требуется хорошая эвристика (у нас — манхэттенское расстояние).
|
||||
- **Вывод:** рекомендуется для большинства практических задач поиска пути.
|
||||
|
||||
### 6.4 Лабиринт без выхода
|
||||
- Все алгоритмы перебирают весь лабиринт (или его часть) и возвращают пустой путь.
|
||||
- BFS и A* делают это системно, DFS может уйти вглубь и долго возвращаться.
|
||||
|
||||
---
|
||||
|
||||
## 7. Выводы
|
||||
|
||||
### 7.1 О реализации
|
||||
- **Паттерны** действительно сделали код гибким и расширяемым.
|
||||
- **Builder** изолировал загрузку — легко поменять формат файла.
|
||||
- **Strategy** позволил сравнивать алгоритмы без изменения `MazeSolver`.
|
||||
- **Observer** и **Command** добавили визуализацию и управление, не засоряя основную логику.
|
||||
|
||||
### 7.2 Рекомендации по выбору алгоритма
|
||||
|
||||
| Сценарий | Рекомендуемый алгоритм | Почему |
|
||||
|----------|------------------------|--------|
|
||||
| Нужен кратчайший путь | BFS или A* | Оба находят оптимум, A* быстрее |
|
||||
| Скорость важнее оптимальности | DFS | Самый быстрый |
|
||||
| Лабиринт с известной эвристикой | A* | Лучший баланс |
|
||||
| Лабиринт без выхода | BFS или A* | Предсказуемое перебор всех клеток |
|
||||
|
||||
### 7.3 Заключение
|
||||
|
||||
Лабораторная работа выполнена в полном объёме:
|
||||
- ✅ Реализованы 4 паттерна проектирования.
|
||||
- ✅ Программа загружает лабиринт из текстового файла.
|
||||
- ✅ Реализованы 3 алгоритма поиска пути.
|
||||
- ✅ Добавлена визуализация в консоли.
|
||||
- ✅ Проведены эксперименты, результаты сохранены в CSV.
|
||||
- ✅ Построены графики.
|
||||
- ✅ Оформлен отчёт.
|
||||
|
||||
Программа готова к использованию и легко расширяется.
|
||||
61
maze_results.csv
Normal file
61
maze_results.csv
Normal file
|
|
@ -0,0 +1,61 @@
|
|||
maze,strategy,time_ms,path_length
|
||||
small,BFS,0.044699998397845775,5
|
||||
small,BFS,0.023399999918183312,5
|
||||
small,BFS,0.019799999790848233,5
|
||||
small,BFS,0.01779999911377672,5
|
||||
small,BFS,0.01700000029813964,5
|
||||
small,DFS,0.015499999790336005,5
|
||||
small,DFS,0.011199999789823778,5
|
||||
small,DFS,0.009700001101009548,5
|
||||
small,DFS,0.008799999704933725,5
|
||||
small,DFS,0.008800001523923129,5
|
||||
small,A*,0.044299998990027234,5
|
||||
small,A*,0.02629999835335184,5
|
||||
small,A*,0.023299999156733975,5
|
||||
small,A*,0.022000000171829015,5
|
||||
small,A*,0.022000000171829015,5
|
||||
medium,BFS,0.30920000062906183,25
|
||||
medium,BFS,0.26840000100492034,25
|
||||
medium,BFS,0.23770000007061753,25
|
||||
medium,BFS,0.2347999998164596,25
|
||||
medium,BFS,0.23570000121253543,25
|
||||
medium,DFS,0.19769999926211312,97
|
||||
medium,DFS,0.17719999959808774,97
|
||||
medium,DFS,0.17500000103609636,97
|
||||
medium,DFS,0.2761999985523289,97
|
||||
medium,DFS,0.2241000001959037,97
|
||||
medium,A*,0.577799999518902,25
|
||||
medium,A*,0.5405000010796357,25
|
||||
medium,A*,0.4357999987405492,25
|
||||
medium,A*,0.433899998824927,25
|
||||
medium,A*,0.43729999924835283,25
|
||||
empty,BFS,0.579499999730615,39
|
||||
empty,BFS,0.5511000017577317,39
|
||||
empty,BFS,0.5444999987957999,39
|
||||
empty,BFS,0.543100000868435,39
|
||||
empty,BFS,0.6868000000395114,39
|
||||
empty,DFS,0.6188000006659422,191
|
||||
empty,DFS,0.524799999766401,191
|
||||
empty,DFS,0.4960000005667098,191
|
||||
empty,DFS,0.4931999992550118,191
|
||||
empty,DFS,0.48609999976179097,191
|
||||
empty,A*,1.1410999995860038,39
|
||||
empty,A*,1.1313000013615238,39
|
||||
empty,A*,1.1198000011063414,39
|
||||
empty,A*,1.1212000008526957,39
|
||||
empty,A*,1.1166000003868248,39
|
||||
no_exit,BFS,0.13609999950858764,19
|
||||
no_exit,BFS,0.13050000052317046,19
|
||||
no_exit,BFS,0.12960000094608404,19
|
||||
no_exit,BFS,0.12900000001536682,19
|
||||
no_exit,BFS,0.12849999984609894,19
|
||||
no_exit,DFS,0.07240000013553072,43
|
||||
no_exit,DFS,0.06969999958528206,43
|
||||
no_exit,DFS,0.067299999500392,43
|
||||
no_exit,DFS,0.06679999933112413,43
|
||||
no_exit,DFS,0.06589999975403771,43
|
||||
no_exit,A*,0.23909999981697183,19
|
||||
no_exit,A*,0.23270000019692816,19
|
||||
no_exit,A*,0.23099999998521525,19
|
||||
no_exit,A*,0.232000000323751,19
|
||||
no_exit,A*,0.23049999981594738,19
|
||||
|
Loading…
Reference in New Issue
Block a user