2026-rff_mp/semyanovra/scr/maze.py

345 lines
9.8 KiB
Python

import sys
from collections import deque
import heapq
import time
import os
import csv
import matplotlib.pyplot as plt
import numpy as np
# ----------------------------- Модель клетки -----------------------------
class GridCell:
def __init__(self, x, y):
self._x = x
self._y = y
self._blocked = False
self._entry = False
self._exit_flag = False
@property
def x(self):
return self._x
@property
def y(self):
return self._y
@property
def is_wall(self):
return self._blocked
@is_wall.setter
def is_wall(self, value):
self._blocked = value
@property
def is_start(self):
return self._entry
@is_start.setter
def is_start(self, value):
self._entry = value
@property
def is_exit(self):
return self._exit_flag
@is_exit.setter
def is_exit(self, value):
self._exit_flag = value
def passable(self):
return not self._blocked
# ----------------------------- Модель лабиринта -----------------------------
class Labyrinth:
def __init__(self, width, height):
self._width = width
self._height = height
self._cells = [[GridCell(x, y) for x in range(width)] for y in range(height)]
self._start_cell = None
self._exit_cell = None
@property
def width(self):
return self._width
@property
def height(self):
return self._height
@property
def start(self):
return self._start_cell
@property
def exit(self):
return self._exit_cell
def cell_at(self, x, y):
if 0 <= x < self._width and 0 <= y < self._height:
return self._cells[y][x]
return None
def configure_cell(self, x, y, cell_type):
cell = self.cell_at(x, y)
if cell is None:
return
if cell_type == 'wall':
cell.is_wall = True
elif cell_type == 'start':
if self._start_cell:
self._start_cell.is_start = False
cell.is_start = True
cell.is_wall = False
self._start_cell = cell
elif cell_type == 'exit':
if self._exit_cell:
self._exit_cell.is_exit = False
cell.is_exit = True
cell.is_wall = False
self._exit_cell = cell
elif cell_type == 'path':
cell.is_wall = False
def adjacent_cells(self, cell):
neighbours = []
directions = [(0, -1), (0, 1), (-1, 0), (1, 0)]
for dx, dy in directions:
nx, ny = cell.x + dx, cell.y + dy
neighbour = self.cell_at(nx, ny)
if neighbour and neighbour.passable():
neighbours.append(neighbour)
return neighbours
# ----------------------------- Загрузка лабиринта -----------------------------
class LabyrinthBuilder:
def build_from_file(self, filename):
raise NotImplementedError
class TxtLabyrinthBuilder(LabyrinthBuilder):
def build_from_file(self, filename):
with open(filename, 'r') as f:
lines = [line.rstrip('\n') for line in f.readlines()]
height = len(lines)
width = max(len(line) for line in lines) if height > 0 else 0
start_cnt = 0
exit_cnt = 0
lab = Labyrinth(width, height)
for y, line in enumerate(lines):
for x, ch in enumerate(line):
if ch == "#":
lab.configure_cell(x, y, "wall")
elif ch == "S":
lab.configure_cell(x, y, "start")
start_cnt += 1
elif ch == "E":
lab.configure_cell(x, y, "exit")
exit_cnt += 1
else:
lab.configure_cell(x, y, 'path')
if start_cnt != 1 or exit_cnt != 1:
raise ValueError(f"Maze must have exactly one S and one E. Found S={start_cnt}, E={exit_cnt}")
return lab
# ----------------------------- Алгоритмы поиска -----------------------------
class SearchAlgorithm:
def compute_path(self, maze, start, goal):
raise NotImplementedError
def _build_path(self, came_from, start, goal):
path = []
cur = goal
while cur is not None:
path.append(cur)
cur = came_from.get(cur)
path.reverse()
return path
def visited_nodes(self):
return getattr(self, '_visited', 0)
class BFS(SearchAlgorithm):
def compute_path(self, maze, start, goal):
q = deque()
q.append(start)
came_from = {start: None}
visited = {start}
while q:
cur = q.popleft()
if cur == goal:
self._visited = len(visited)
return self._build_path(came_from, start, goal)
for nb in maze.adjacent_cells(cur):
if nb not in visited:
visited.add(nb)
came_from[nb] = cur
q.append(nb)
self._visited = len(visited)
return []
class DFS(SearchAlgorithm):
def compute_path(self, maze, start, goal):
stack = [start]
came_from = {start: None}
visited = {start}
while stack:
cur = stack.pop()
if cur == goal:
self._visited = len(visited)
return self._build_path(came_from, start, goal)
for nb in maze.adjacent_cells(cur):
if nb not in visited:
visited.add(nb)
came_from[nb] = cur
stack.append(nb)
self._visited = len(visited)
return []
class AStar(SearchAlgorithm):
def _heuristic(self, cell, goal):
return abs(cell.x - goal.x) + abs(cell.y - goal.y)
def compute_path(self, maze, start, goal):
heap = []
counter = 0
start_f = self._heuristic(start, goal)
heapq.heappush(heap, (start_f, counter, start))
counter += 1
came_from = {}
g_score = {start: 0}
f_score = {start: start_f}
visited = set()
while heap:
cur_f, _, cur = heapq.heappop(heap)
visited.add(cur)
if cur == goal:
self._visited = len(visited)
return self._build_path(came_from, start, goal)
if cur_f > f_score.get(cur, float('inf')):
continue
for nb in maze.adjacent_cells(cur):
tentative_g = g_score[cur] + 1
if tentative_g < g_score.get(nb, float('inf')):
came_from[nb] = cur
g_score[nb] = tentative_g
new_f = tentative_g + self._heuristic(nb, goal)
f_score[nb] = new_f
heapq.heappush(heap, (new_f, counter, nb))
counter += 1
self._visited = len(visited)
return []
# ----------------------------- Оркестратор -----------------------------
class Pathfinder:
def __init__(self, maze):
self._maze = maze
self._algorithm = None
self._listeners = []
def attach(self, listener):
self._listeners.append(listener)
def notify(self, event, data):
for lst in self._listeners:
lst.update(event, data)
def set_algorithm(self, algorithm):
self._algorithm = algorithm
def solve(self):
if self._algorithm is None:
return None
t0 = time.perf_counter()
path = self._algorithm.compute_path(self._maze, self._maze.start, self._maze.exit)
t1 = time.perf_counter()
elapsed_ms = (t1 - t0) * 1000
self.notify("path_found", path)
return PerformanceData(elapsed_ms, self._algorithm.visited_nodes(), len(path))
class PerformanceData:
def __init__(self, time_ms, visited, length):
self.time_ms = time_ms
self.visited_cells = visited
self.path_length = length
# ----------------------------- Наблюдатель и отображение -----------------------------
class EventListener:
def update(self, event_type, data):
raise NotImplementedError
class ConsoleDisplay(EventListener):
def __init__(self):
self._last_path = None
def update(self, event_type, data):
if event_type == "maze_loaded":
self._render_maze(data)
elif event_type == "path_found":
self._last_path = data
self._render_path(data)
def _render_maze(self, maze):
os.system('cls' if os.name == 'nt' else 'clear')
print("=" * (maze.width * 2 + 4))
print(" LABYRINTH")
print("=" * (maze.width * 2 + 4))
for y in range(maze.height):
print(" ", end='')
for x in range(maze.width):
cell = maze.cell_at(x, y)
if cell == maze.start:
print('S', end=' ')
elif cell == maze.exit:
print('E', end=' ')
elif cell.is_wall:
print('#', end=' ')
else:
print('.', end=' ')
print()
print("=" * (maze.width * 2 + 4))
print(" S - start E - exit # - wall . - path")
def _render_path(self, path):
if not path:
print("\n Path not found!")
return
print(f"\n Path found! Length: {len(path)}")
if __name__ == "__main__":
builder = TxtLabyrinthBuilder()
maze = builder.build_from_file("maze/level1.txt")
view = ConsoleDisplay()
view.update("maze_loaded", maze)
pf = Pathfinder(maze)
pf.attach(view)
for algo, name in [(BFS(), "BFS"), (DFS(), "DFS"), (AStar(), "A*")]:
pf.set_algorithm(algo)
stats = pf.solve()
print(f"{name}: {stats.time_ms:.3f}ms, visited={stats.visited_cells}, length={stats.path_length}")