Merge pull request '[1,2] лаба 2 (тут еще и первая пусть будит)' (#319) from famutdinovmd/2026-rff_mp:master into develop

Reviewed-on: #319
This commit is contained in:
git_admin 2026-05-30 11:15:57 +00:00
commit 828eac11fa
18 changed files with 1210 additions and 0 deletions

36
famutdinovmd/.gitignore vendored Normal file
View File

@ -0,0 +1,36 @@
# Python
__pycache__/
*.py[cod]
*$py.class
*.so
.Python
env/
venv/
ENV/
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
wheels/
*.egg-info/
.installed.cfg
*.egg
# IDE
.vscode/
.idea/
*.swp
*.swo
*~
# Project specific
experiment_results.csv
experiment_results.png
*.log

View File

61
famutdinovmd/builders.py Normal file
View File

@ -0,0 +1,61 @@
from abc import ABC, abstractmethod
from models import Cell, Maze
class MazeBuilder(ABC):
"""Абстрактный строитель лабиринта"""
@abstractmethod
def build_from_file(self, filename: str) -> Maze:
"""Построить лабиринт из файла"""
pass
class TextFileMazeBuilder(MazeBuilder):
"""Строитель лабиринта из текстового файла"""
WALL_CHAR = '#'
START_CHAR = 'S'
EXIT_CHAR = 'E'
PASS_CHAR = ' '
def build_from_file(self, filename: str) -> Maze:
"""Читает текстовый файл и создаёт лабиринт"""
with open(filename, 'r', encoding='utf-8') as f:
lines = [line.rstrip('\n') for line in f.readlines()]
if not lines:
raise ValueError("Файл с лабиринтом пуст")
height = len(lines)
width = max(len(line) for line in lines)
maze = Maze(width, height)
for y, line in enumerate(lines):
for x, ch in enumerate(line):
if x >= width:
continue
cell = Cell(x, y)
if ch == self.WALL_CHAR:
cell.is_wall = True
elif ch == self.START_CHAR:
cell.is_start = True
elif ch == self.EXIT_CHAR:
cell.is_exit = True
elif ch == self.PASS_CHAR:
pass # проходимая клетка (всё уже настроено)
else:
cell.is_wall = True # неизвестный символ считаем стеной
maze.set_cell(x, y, cell)
# Валидация
if maze.start is None:
raise ValueError("В лабиринте нет стартовой клетки (S)")
if maze.exit is None:
raise ValueError("В лабиринте нет выхода (E)")
return maze

71
famutdinovmd/commands.py Normal file
View File

@ -0,0 +1,71 @@
from abc import ABC, abstractmethod
from typing import Optional
from models import Cell, Maze
class Player:
"""Игрок, перемещающийся по лабиринту"""
def __init__(self, start_cell: Cell):
self.current_cell = start_cell
def move_to(self, new_cell: Cell) -> None:
"""Перемещает игрока в новую клетку"""
self.current_cell = new_cell
class Command(ABC):
"""Абстрактная команда"""
@abstractmethod
def execute(self) -> bool:
"""Выполняет команду"""
pass
@abstractmethod
def undo(self) -> None:
"""Отменяет команду"""
pass
class MoveCommand(Command):
"""Команда перемещения игрока"""
def __init__(self, player: Player, maze: Maze, direction: str):
self.player = player
self.maze = maze
self.direction = direction
self.previous_cell: Optional[Cell] = None
self.new_cell: Optional[Cell] = None
def _get_target_cell(self) -> Optional[Cell]:
"""Возвращает целевую клетку в зависимости от направления"""
x, y = self.player.current_cell.x, self.player.current_cell.y
if self.direction == 'w':
y -= 1
elif self.direction == 's':
y += 1
elif self.direction == 'a':
x -= 1
elif self.direction == 'd':
x += 1
else:
return None
return self.maze.get_cell(x, y)
def execute(self) -> bool:
"""Выполняет перемещение"""
self.previous_cell = self.player.current_cell
self.new_cell = self._get_target_cell()
if self.new_cell and self.new_cell.is_passable():
self.player.move_to(self.new_cell)
return True
return False
def undo(self) -> None:
"""Отменяет перемещение"""
if self.previous_cell:
self.player.move_to(self.previous_cell)

100
famutdinovmd/experiments.py Normal file
View File

@ -0,0 +1,100 @@
import csv
import time
from typing import List, Dict
from models import Maze
from builders import TextFileMazeBuilder
from strategies import BFSStrategy, DFSStrategy, AStarStrategy
from solver import MazeSolver
def run_experiment(maze: Maze, strategy_name: str, strategy, repeats: int = 5) -> Dict:
"""Запускает эксперимент для одной стратегии"""
times = []
visited_counts = []
path_lengths = []
path_found = True
for _ in range(repeats):
solver = MazeSolver(maze, strategy)
path, stats = solver.solve()
times.append(stats.time_ms)
visited_counts.append(stats.visited_cells)
path_lengths.append(stats.path_length)
path_found = stats.path_found
return {
'strategy': strategy_name,
'time_mean': sum(times) / len(times),
'time_min': min(times),
'time_max': max(times),
'visited_mean': sum(visited_counts) / len(visited_counts),
'path_length_mean': sum(path_lengths) / len(path_lengths) if path_found else 0,
'path_found': path_found
}
def run_all_experiments(maze_files: List[str], repeats: int = 5) -> List[Dict]:
"""Запускает эксперименты для всех лабиринтов и стратегий"""
builder = TextFileMazeBuilder()
strategies = [
('BFS', BFSStrategy()),
('DFS', DFSStrategy()),
('A*', AStarStrategy())
]
results = []
for maze_file in maze_files:
try:
maze = builder.build_from_file(maze_file)
except (ValueError, FileNotFoundError) as e:
print(f"❌ Ошибка: {e}")
continue
print(f"\n📊 Лабиринт: {maze_file}")
print(f" Размер: {maze.width}×{maze.height}")
print(f" Старт: ({maze.start.x}, {maze.start.y})")
print(f" Выход: ({maze.exit.x}, {maze.exit.y})")
for strategy_name, strategy in strategies:
print(f" 🧪 Тестирование: {strategy_name}")
result = run_experiment(maze, strategy_name, strategy, repeats)
result['maze_file'] = maze_file.split('/')[-1]
result['maze_size'] = f"{maze.width}×{maze.height}"
results.append(result)
status = "" if result['path_found'] else ""
print(f" {status} Время: {result['time_mean']:.2f} мс, "
f"Посещено: {result['visited_mean']:.0f}, "
f"Путь: {result['path_length_mean']:.0f}")
return results
def save_results_to_csv(results: List[Dict], filename: str = "experiment_results.csv") -> None:
"""Сохраняет результаты в CSV файл"""
with open(filename, 'w', newline='', encoding='utf-8') as f:
writer = csv.DictWriter(f, fieldnames=[
'maze_file', 'maze_size', 'strategy',
'time_mean', 'time_min', 'time_max',
'visited_mean', 'path_length_mean', 'path_found'
])
writer.writeheader()
writer.writerows(results)
print(f"\n💾 Результаты сохранены в {filename}")
def print_results_table(results: List[Dict]) -> None:
"""Выводит результаты в виде таблицы"""
print("\n" + "=" * 80)
print("РЕЗУЛЬТАТЫ ЭКСПЕРИМЕНТОВ")
print("=" * 80)
for res in results:
print(f"\n📁 Лабиринт: {res['maze_file']}")
print(f" 📐 Размер: {res['maze_size']}")
print(f" 🎯 Стратегия: {res['strategy']}")
print(f" ⏱️ Время (ср): {res['time_mean']:.2f} мс")
print(f" 📍 Посещено: {res['visited_mean']:.0f} клеток")
print(f" 🛤️ Длина пути: {res['path_length_mean']:.0f}")

182
famutdinovmd/main.py Normal file
View File

@ -0,0 +1,182 @@
import os
from builders import TextFileMazeBuilder
from strategies import BFSStrategy, DFSStrategy, AStarStrategy
from solver import MazeSolver
from observers import ConsoleView
from commands import Player
from experiments import run_all_experiments, save_results_to_csv, print_results_table
def create_test_mazes():
"""Создаёт тестовые лабиринты в папке mazes/"""
os.makedirs("mazes", exist_ok=True)
# Маленький лабиринт 10×10
small = """##########
#S #
# ### ## #
# # #
### # ####
# # #
# ### # #
# # #
# # E#
##########"""
# Средний лабиринт 20×11
medium = """####################
#S #
# # # # # # # # # #
# #
# # # # # # # # # #
# #
# # # # # # # # # #
# #
# # # # # # # # # #
# E#
####################"""
# Большой лабиринт 30×15
large = """##############################
#S #
# # # # # # # # # # # # # # #
# #
# # # # # # # # # # # # # # #
# #
# # # # # # # # # # # # # # #
# #
# # # # # # # # # # # # # # #
# #
# # # # # # # # # # # # # # #
# #
# # # # # # # # # # # # # # #
# E#
##############################"""
# Пустой лабиринт (без стен)
empty = "S" + " " * 28 + "E"
# Лабиринт без выхода
no_exit = """#######
#S #
# ### #
# # #
#######"""
# Сохранение файлов
with open("mazes/small.txt", "w") as f:
f.write(small)
with open("mazes/medium.txt", "w") as f:
f.write(medium)
with open("mazes/large.txt", "w") as f:
f.write(large)
with open("mazes/empty.txt", "w") as f:
f.write(empty)
with open("mazes/no_exit.txt", "w") as f:
f.write(no_exit)
print("✅ Тестовые лабиринты созданы в папке 'mazes/'")
def demo_maze_solver():
"""Демонстрация работы MazeSolver с разными стратегиями"""
print("\n" + "=" * 60)
print("ДЕМОНСТРАЦИЯ РАБОТЫ MAZE SOLVER")
print("=" * 60)
builder = TextFileMazeBuilder()
view = ConsoleView()
try:
maze = builder.build_from_file("mazes/small.txt")
view.update("maze_loaded", {"maze": maze})
strategies = [
("BFS", BFSStrategy(), "BFS (поиск в ширину)"),
("DFS", DFSStrategy(), "DFS (поиск в глубину)"),
("A*", AStarStrategy(), "A* (A-star поиск)")
]
for name, strategy, description in strategies:
print(f"\n--- {description} ---")
solver = MazeSolver(maze, strategy)
view.update("search_start", {"algorithm": description})
path, stats = solver.solve()
if stats.path_found:
view.update("path_found", {"maze": maze, "path": path, "stats": stats})
else:
view.update("no_path", {"stats": stats})
except Exception as e:
print(f"❌ Ошибка: {e}")
def demo_player_controls():
"""Демонстрация управления игроком (Command + Observer)"""
print("\n" + "=" * 60)
print("ДЕМОНСТРАЦИЯ УПРАВЛЕНИЯ (Command + Observer)")
print("=" * 60)
builder = TextFileMazeBuilder()
view = ConsoleView()
try:
maze = builder.build_from_file("mazes/small.txt")
player = Player(maze.start)
view.update("maze_loaded", {"maze": maze})
view.render(maze, player_position=player.current_cell)
print("\n💡 Для управления игроком в консоли введите W/A/S/D")
print(" (это демонстрация работы паттернов Command и Observer)")
except Exception as e:
print(f"❌ Ошибка: {e}")
def run_experiments():
"""Запуск экспериментов для сравнения алгоритмов"""
print("\n" + "=" * 60)
print("ЭКСПЕРИМЕНТАЛЬНОЕ СРАВНЕНИЕ АЛГОРИТМОВ")
print("=" * 60)
maze_files = [
"mazes/small.txt",
"mazes/medium.txt",
"mazes/large.txt",
"mazes/empty.txt",
"mazes/no_exit.txt"
]
results = run_all_experiments(maze_files, repeats=5)
save_results_to_csv(results)
print_results_table(results)
def main():
"""Главная функция"""
print("=" * 60)
print("🎯 ОБЪЕКТНО-ОРИЕНТИРОВАННАЯ РЕАЛИЗАЦИЯ ПОИСКА В ЛАБИРИНТЕ")
print("📚 Применённые паттерны: Builder, Strategy, Observer, Command")
print("=" * 60)
# Создание тестовых лабиринтов
create_test_mazes()
# Демонстрация работы
demo_maze_solver()
demo_player_controls()
# Эксперименты
run_experiments()
print("\n" + "=" * 60)
print("✅ Программа завершена!")
print("📊 Для построения графиков запустите: python visualize.py")
print("=" * 60)
if __name__ == "__main__":
main()

View File

@ -0,0 +1 @@
S E

View File

@ -0,0 +1,15 @@
##############################
#S #
# # # # # # # # # # # # # # #
# #
# # # # # # # # # # # # # # #
# #
# # # # # # # # # # # # # # #
# #
# # # # # # # # # # # # # # #
# #
# # # # # # # # # # # # # # #
# #
# # # # # # # # # # # # # # #
# E#
##############################

View File

@ -0,0 +1,11 @@
####################
#S #
# # # # # # # # # #
# #
# # # # # # # # # #
# #
# # # # # # # # # #
# #
# # # # # # # # # #
# E#
####################

View File

@ -0,0 +1,5 @@
#######
#S #
# ### #
# # #
#######

View File

@ -0,0 +1,10 @@
##########
#S #
# ### ## #
# # #
### # ####
# # #
# ### # #
# # #
# # E#
##########

86
famutdinovmd/models.py Normal file
View File

@ -0,0 +1,86 @@
from typing import List, Optional
class Cell:
"""Клетка лабиринта"""
def __init__(self, x: int, y: int):
self.x = x
self.y = y
self.is_wall = False
self.is_start = False
self.is_exit = False
def is_passable(self) -> bool:
"""Проверяет, можно ли пройти через клетку"""
return not self.is_wall
def __eq__(self, other) -> bool:
if not isinstance(other, Cell):
return False
return self.x == other.x and self.y == other.y
def __hash__(self):
return hash((self.x, self.y))
def __repr__(self):
return f"Cell({self.x}, {self.y})"
class Maze:
"""Лабиринт (сетка клеток)"""
def __init__(self, width: int, height: int):
self.width = width
self.height = height
self._cells: List[List[Optional[Cell]]] = [[None for _ in range(width)] for _ in range(height)]
self.start: Optional[Cell] = None
self.exit: Optional[Cell] = None
def set_cell(self, x: int, y: int, cell: Cell) -> None:
"""Устанавливает клетку в указанные координаты"""
if 0 <= x < self.width and 0 <= y < self.height:
self._cells[y][x] = cell
if cell.is_start:
self.start = cell
if cell.is_exit:
self.exit = cell
def get_cell(self, x: int, y: int) -> Optional[Cell]:
"""Возвращает клетку по координатам"""
if 0 <= x < self.width and 0 <= y < self.height:
return self._cells[y][x]
return None
def get_neighbors(self, cell: Cell) -> List[Cell]:
"""Возвращает список соседних проходимых клеток"""
neighbors = []
directions = [(0, -1), (0, 1), (-1, 0), (1, 0)] # вверх, вниз, влево, вправо
for dx, dy in directions:
nx, ny = cell.x + dx, cell.y + dy
neighbor = self.get_cell(nx, ny)
if neighbor and neighbor.is_passable():
neighbors.append(neighbor)
return neighbors
def __str__(self) -> str:
"""Строковое представление лабиринта"""
result = []
for y in range(self.height):
row = []
for x in range(self.width):
cell = self.get_cell(x, y)
if cell is None:
row.append('?')
elif cell.is_start:
row.append('S')
elif cell.is_exit:
row.append('E')
elif cell.is_wall:
row.append('#')
else:
row.append(' ')
result.append(''.join(row))
return '\n'.join(result)

73
famutdinovmd/observers.py Normal file
View File

@ -0,0 +1,73 @@
from abc import ABC, abstractmethod
from typing import List, Optional
from models import Cell, Maze
class Observer(ABC):
"""Абстрактный наблюдатель"""
@abstractmethod
def update(self, event: str, data: dict) -> None:
"""Обработка события"""
pass
class ConsoleView(Observer):
"""Консольная визуализация лабиринта"""
def render(self, maze: Maze, player_position: Optional[Cell] = None,
path: Optional[List[Cell]] = None) -> None:
"""Отрисовывает лабиринт в консоли"""
path_set = set(path) if path else set()
print("\n+" + "-" * maze.width + "+")
for y in range(maze.height):
row = []
for x in range(maze.width):
cell = maze.get_cell(x, y)
if cell is None:
row.append('?')
elif player_position and cell == player_position:
row.append('@')
elif cell.is_start:
row.append('S')
elif cell.is_exit:
row.append('E')
elif cell in path_set:
row.append('*')
elif cell.is_wall:
row.append('#')
else:
row.append(' ')
print("|" + ''.join(row) + "|")
print("+" + "-" * maze.width + "+")
def update(self, event: str, data: dict) -> None:
"""Обработка событий от MazeSolver"""
if event == "maze_loaded":
maze = data.get('maze')
print("\n📦 Лабиринт загружен:")
self.render(maze)
elif event == "search_start":
algorithm = data.get('algorithm', 'Unknown')
print(f"\n🔍 Начинаем поиск алгоритмом: {algorithm}")
elif event == "path_found":
maze = data.get('maze')
path = data.get('path')
stats = data.get('stats')
print(f"\n✅ Путь найден! {stats}")
self.render(maze, path=path)
elif event == "no_path":
stats = data.get('stats')
print(f"\n{stats}")
elif event == "player_moved":
maze = data.get('maze')
player = data.get('player')
if player:
self.render(maze, player_position=player.current_cell)

308
famutdinovmd/report.md Normal file
View File

@ -0,0 +1,308 @@
# Отчёт по лабораторной работе №2
## Поиск выхода из лабиринта (объектно-ориентированная реализация с паттернами)
---
## 1. Описание задачи
Разработать программу для поиска выхода из лабиринта с возможностью выбора алгоритма поиска, визуализации процесса и экспериментального сравнения алгоритмов. Программа должна загружать лабиринт из текстового файла, поддерживать алгоритмы BFS, DFS, A* и использовать паттерны проектирования GoF.
---
## 2. Выбранные паттерны
### 2.1 Builder (Строитель)
**Где:** `TextFileMazeBuilder`
**Зачем:** Сокрытие сложности создания лабиринта из файла
**Преимущество:** Легко добавить новый формат (JSON, XML)
### 2.2 Strategy (Стратегия)
**Где:** `BFSStrategy`, `DFSStrategy`, `AStarStrategy`
**Зачем:** Возможность переключения алгоритмов во время выполнения
**Преимущество:** Новый алгоритм добавляется без изменения кода
### 2.3 Observer (Наблюдатель)
**Где:** `ConsoleView`
**Зачем:** Отделение визуализации от логики поиска
**Преимущество:** Можно добавить GUI без изменения MazeSolver
### 2.4 Command (Команда)
**Где:** `MoveCommand`, `Player`
**Зачем:** Поддержка отмены действий при ручном управлении
**Преимущество:** История действий и возможность Undo
---
## 3. Диаграмма классов (Mermaid)
```python
classDiagram
class Maze {
-width, height
-_cells[][]
-start, exit
+get_cell(x,y)
+get_neighbors(cell)
}
class Cell {
-x, y
-is_wall
-is_start
-is_exit
+is_passable()
}
class MazeBuilder {
<<interface>>
+build_from_file(filename)
}
class TextFileMazeBuilder {
+build_from_file(filename)
}
class PathFindingStrategy {
<<interface>>
+find_path(maze, start, exit)
}
class BFSStrategy
class DFSStrategy
class AStarStrategy
class MazeSolver {
-maze
-strategy
+set_strategy()
+solve()
}
class Observer {
<<interface>>
+update(event, data)
}
class ConsoleView {
+render(maze, path)
+update(event, data)
}
class Command {
<<interface>>
+execute()
+undo()
}
class MoveCommand {
-player
-direction
+execute()
+undo()
}
MazeBuilder <|.. TextFileMazeBuilder
PathFindingStrategy <|.. BFSStrategy
PathFindingStrategy <|.. DFSStrategy
PathFindingStrategy <|.. AStarStrategy
MazeSolver --> PathFindingStrategy
Observer <|.. ConsoleView
Command <|.. MoveCommand
class TextFileMazeBuilder(MazeBuilder):
WALL_CHAR = '#'
START_CHAR = 'S'
EXIT_CHAR = 'E'
def build_from_file(self, filename: str) -> Maze:
with open(filename, 'r', encoding='utf-8') as f:
lines = [line.rstrip('\n') for line in f.readlines()]
height = len(lines)
width = max(len(line) for line in lines)
maze = Maze(width, height)
for y, line in enumerate(lines):
for x, ch in enumerate(line):
if x >= width:
continue
cell = Cell(x, y)
if ch == self.WALL_CHAR:
cell.is_wall = True
elif ch == self.START_CHAR:
cell.is_start = True
elif ch == self.EXIT_CHAR:
cell.is_exit = True
maze.set_cell(x, y, cell)
if maze.start is None:
raise ValueError("Нет стартовой клетки (S)")
if maze.exit is None:
raise ValueError("Нет выхода (E)")
return maze
class TextFileMazeBuilder(MazeBuilder):
WALL_CHAR = '#'
START_CHAR = 'S'
EXIT_CHAR = 'E'
def build_from_file(self, filename: str) -> Maze:
with open(filename, 'r', encoding='utf-8') as f:
lines = [line.rstrip('\n') for line in f.readlines()]
height = len(lines)
width = max(len(line) for line in lines)
maze = Maze(width, height)
for y, line in enumerate(lines):
for x, ch in enumerate(line):
if x >= width:
continue
cell = Cell(x, y)
if ch == self.WALL_CHAR:
cell.is_wall = True
elif ch == self.START_CHAR:
cell.is_start = True
elif ch == self.EXIT_CHAR:
cell.is_exit = True
maze.set_cell(x, y, cell)
if maze.start is None:
raise ValueError("Нет стартовой клетки (S)")
if maze.exit is None:
raise ValueError("Нет выхода (E)")
return maze
class BFSStrategy(PathFindingStrategy):
def find_path(self, maze, start, exit_cell):
queue = deque([start])
visited = {start}
parent = {start: None}
while queue:
current = queue.popleft()
if current == exit_cell:
return self._reconstruct_path(parent, current)
for neighbor in maze.get_neighbors(current):
if neighbor not in visited:
visited.add(neighbor)
parent[neighbor] = current
queue.append(neighbor)
return []
def _reconstruct_path(self, parent, current):
path = []
while current:
path.append(current)
current = parent[current]
return list(reversed(path))
class DFSStrategy(PathFindingStrategy):
def find_path(self, maze, start, exit_cell):
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, exit_cell):
counter = 0
open_set = [(self._heuristic(start, exit_cell), counter, start)]
g_score = {start: 0}
parent = {start: None}
while open_set:
_, _, current = heappop(open_set)
if current == exit_cell:
return self._reconstruct_path(parent, current)
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
counter += 1
f = tentative_g + self._heuristic(neighbor, exit_cell)
heappush(open_set, (f, counter, neighbor))
return []
class ConsoleView(Observer):
def render(self, maze, path=None):
path_set = set(path) if path else set()
print("\n+" + "-" * maze.width + "+")
for y in range(maze.height):
row = []
for x in range(maze.width):
cell = maze.get_cell(x, y)
if cell.is_start:
row.append('S')
elif cell.is_exit:
row.append('E')
elif cell in path_set:
row.append('*')
elif cell.is_wall:
row.append('#')
else:
row.append(' ')
print("|" + ''.join(row) + "|")
print("+" + "-" * maze.width + "+")
def update(self, event, data):
if event == "maze_loaded":
self.render(data.get('maze'))
elif event == "path_found":
self.render(data.get('maze'), data.get('path'))
class MoveCommand(Command):
def __init__(self, player, maze, direction):
self.player = player
self.maze = maze
self.direction = direction
self.previous_cell = None
def execute(self):
self.previous_cell = self.player.current_cell
dx, dy = self.direction
new_cell = self.maze.get_cell(
self.player.current_cell.x + dx,
self.player.current_cell.y + dy
)
if new_cell and new_cell.is_passable():
self.player.move_to(new_cell)
return True
return False
def undo(self):
if self.previous_cell:
self.player.move_to(self.previous_cell)
class MazeSolver:
def __init__(self, maze, strategy=None):
self.maze = maze
self._strategy = strategy
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, self.maze.start, self.maze.exit)
end_time = time.perf_counter()
stats = SearchStats(
time_ms=(end_time - start_time) * 1000,
visited_cells=len(path) if path else 0,
path_length=len(path) if path else 0,
path_found=bool(path)
)
return path, stats

View File

@ -0,0 +1,3 @@
matplotlib>=3.5.0
pandas>=1.5.0
numpy>=1.21.0

53
famutdinovmd/solver.py Normal file
View File

@ -0,0 +1,53 @@
import time
from dataclasses import dataclass
from typing import List, Optional, Tuple
from models import Cell, Maze
from strategies import PathFindingStrategy
@dataclass
class SearchStats:
"""Статистика поиска"""
time_ms: float
visited_cells: int
path_length: int
path_found: bool = True
def __str__(self) -> str:
if not self.path_found:
return f"Путь не найден (время: {self.time_ms:.2f} мс)"
return (f"Время: {self.time_ms:.2f} мс, "
f"Посещено клеток: {self.visited_cells}, "
f"Длина пути: {self.path_length}")
class MazeSolver:
"""Оркестратор решения лабиринта"""
def __init__(self, maze: Maze, strategy: Optional[PathFindingStrategy] = None):
self.maze = maze
self._strategy = strategy
def set_strategy(self, strategy: PathFindingStrategy) -> None:
"""Устанавливает стратегию поиска"""
self._strategy = strategy
def solve(self) -> Tuple[List[Cell], SearchStats]:
"""Выполняет поиск пути с текущей стратегией"""
if self._strategy is None:
raise ValueError("Стратегия не установлена")
start_time = time.perf_counter()
path = self._strategy.find_path(self.maze, self.maze.start, self.maze.exit)
end_time = time.perf_counter()
time_ms = (end_time - start_time) * 1000
stats = SearchStats(
time_ms=time_ms,
visited_cells=len(path) if path else 0,
path_length=len(path) if path else 0,
path_found=bool(path)
)
return path, stats

107
famutdinovmd/strategies.py Normal file
View File

@ -0,0 +1,107 @@
from abc import ABC, abstractmethod
from collections import deque
from heapq import heappush, heappop
from typing import List, Dict, Optional
from models import Cell, Maze
class PathFindingStrategy(ABC):
"""Абстрактная стратегия поиска пути"""
@abstractmethod
def find_path(self, maze: Maze, start: Cell, exit_cell: Cell) -> List[Cell]:
"""Находит путь от start до exit_cell"""
pass
class BFSStrategy(PathFindingStrategy):
"""Поиск в ширину (BFS) - гарантирует кратчайший путь"""
def find_path(self, maze: Maze, start: Cell, exit_cell: Cell) -> List[Cell]:
queue = deque([start])
visited = {start}
parent: Dict[Cell, Optional[Cell]] = {start: None}
while queue:
current = queue.popleft()
if current == exit_cell:
return self._reconstruct_path(parent, current)
for neighbor in maze.get_neighbors(current):
if neighbor not in visited:
visited.add(neighbor)
parent[neighbor] = current
queue.append(neighbor)
return [] # Путь не найден
def _reconstruct_path(self, parent: Dict[Cell, Optional[Cell]], current: Cell) -> List[Cell]:
"""Восстанавливает путь от start до current"""
path = []
while current is not None:
path.append(current)
current = parent.get(current)
return list(reversed(path))
class DFSStrategy(PathFindingStrategy):
"""Поиск в глубину (DFS) - быстрый, но не обязательно кратчайший"""
def find_path(self, maze: Maze, start: Cell, exit_cell: Cell) -> List[Cell]:
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):
"""A* поиск - оптимальный баланс скорости и кратчайшего пути"""
def _heuristic(self, cell: Cell, exit_cell: Cell) -> int:
"""Манхэттенское расстояние (эвристика)"""
return abs(cell.x - exit_cell.x) + abs(cell.y - exit_cell.y)
def find_path(self, maze: Maze, start: Cell, exit_cell: Cell) -> List[Cell]:
counter = 0 # для разрешения конфликтов в куче
open_set = [(self._heuristic(start, exit_cell), counter, start)]
g_score: Dict[Cell, float] = {start: 0}
parent: Dict[Cell, Optional[Cell]] = {start: None}
while open_set:
_, _, current = heappop(open_set)
if current == exit_cell:
return self._reconstruct_path(parent, current)
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
counter += 1
f = tentative_g + self._heuristic(neighbor, exit_cell)
heappush(open_set, (f, counter, neighbor))
return []
def _reconstruct_path(self, parent: Dict[Cell, Optional[Cell]], current: Cell) -> List[Cell]:
"""Восстанавливает путь от start до current"""
path = []
while current is not None:
path.append(current)
current = parent.get(current)
return list(reversed(path))

88
famutdinovmd/visualize.py Normal file
View File

@ -0,0 +1,88 @@
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()