2026-05-09 19:37:10 +00:00
|
|
|
|
from abc import ABC, abstractmethod
|
|
|
|
|
|
from collections import deque
|
2026-05-21 13:30:23 +00:00
|
|
|
|
import matplotlib.pyplot as plt
|
|
|
|
|
|
import pandas as pd
|
|
|
|
|
|
import seaborn as sns
|
2026-05-14 15:51:29 +00:00
|
|
|
|
import heapq
|
2026-05-20 23:17:21 +00:00
|
|
|
|
import time
|
|
|
|
|
|
import os
|
|
|
|
|
|
import csv
|
2026-05-09 19:37:10 +00:00
|
|
|
|
#Этап 1
|
|
|
|
|
|
class Cell:
|
2026-05-20 23:17:21 +00:00
|
|
|
|
def __init__(self, x, y, isWall=False, isStart=False, isExit=False):
|
2026-05-09 19:37:10 +00:00
|
|
|
|
self.x = x
|
|
|
|
|
|
self.y = y
|
2026-05-20 23:17:21 +00:00
|
|
|
|
self.isWall = isWall
|
|
|
|
|
|
self.isStart = isStart
|
|
|
|
|
|
self.isExit = isExit
|
2026-05-09 19:37:10 +00:00
|
|
|
|
|
|
|
|
|
|
def isPassable(self):
|
2026-05-20 23:17:21 +00:00
|
|
|
|
return not self.isWall
|
2026-05-09 19:37:10 +00:00
|
|
|
|
|
|
|
|
|
|
class Maze:
|
|
|
|
|
|
def __init__(self, cells, width, height, start, exit):
|
|
|
|
|
|
self.width = width
|
|
|
|
|
|
self.height = height
|
|
|
|
|
|
self.cells =cells
|
|
|
|
|
|
self.start = start
|
|
|
|
|
|
self.exit = exit
|
|
|
|
|
|
|
|
|
|
|
|
def getCell(self, x, y):
|
|
|
|
|
|
if 0 <= x< self.width and 0 <=y< self.height:
|
|
|
|
|
|
return self.cells[y][x]
|
|
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
|
|
|
def getNeighbors(self, cell: Cell):
|
|
|
|
|
|
neighbors = []
|
|
|
|
|
|
directions = [(0, -1), (0, 1), (-1, 0), (1, 0)]
|
|
|
|
|
|
|
|
|
|
|
|
for dir_x, dir_y in directions:
|
|
|
|
|
|
neigh_x = cell.x+dir_x
|
|
|
|
|
|
neigh_y = cell.y+dir_y
|
|
|
|
|
|
neighbor = self.getCell(neigh_x, neigh_y)
|
|
|
|
|
|
if neighbor and neighbor.isPassable():
|
|
|
|
|
|
neighbors.append(neighbor)
|
|
|
|
|
|
return neighbors
|
|
|
|
|
|
|
|
|
|
|
|
#Этап 2
|
|
|
|
|
|
class MazeBuilder(ABC):
|
|
|
|
|
|
@abstractmethod
|
|
|
|
|
|
def buildFromFile(self, filename):
|
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
class TextFileMazeBuilder(MazeBuilder):
|
|
|
|
|
|
def buildFromFile(self, filename):
|
|
|
|
|
|
with open(filename, 'r') as f:
|
|
|
|
|
|
lines = [line.rstrip('\n') for line in f]
|
|
|
|
|
|
height = len(lines)
|
|
|
|
|
|
width = max(len(line) for line in lines)
|
|
|
|
|
|
|
|
|
|
|
|
grid=[]
|
|
|
|
|
|
start_cell=None
|
|
|
|
|
|
exit_cell=None
|
|
|
|
|
|
for y in range(height):
|
|
|
|
|
|
row=[]
|
|
|
|
|
|
for x in range(width):
|
|
|
|
|
|
char=lines[y][x]
|
|
|
|
|
|
|
2026-05-20 23:17:21 +00:00
|
|
|
|
isWall = (char == '#')
|
|
|
|
|
|
isStart = (char == 'S')
|
|
|
|
|
|
isExit = (char == 'E')
|
2026-05-09 19:37:10 +00:00
|
|
|
|
|
2026-05-20 23:17:21 +00:00
|
|
|
|
cell=Cell(x, y, isWall, isStart, isExit)
|
2026-05-09 19:37:10 +00:00
|
|
|
|
|
2026-05-20 23:17:21 +00:00
|
|
|
|
if isStart:
|
2026-05-09 19:37:10 +00:00
|
|
|
|
start_cell =cell
|
2026-05-20 23:17:21 +00:00
|
|
|
|
if isExit:
|
2026-05-09 19:37:10 +00:00
|
|
|
|
exit_cell =cell
|
|
|
|
|
|
row.append(cell)
|
|
|
|
|
|
grid.append(row)
|
|
|
|
|
|
return Maze(grid, width, height, start_cell, exit_cell)
|
|
|
|
|
|
|
|
|
|
|
|
#Этап 3
|
|
|
|
|
|
class PathFindingStrategy(ABC):
|
|
|
|
|
|
@abstractmethod
|
|
|
|
|
|
def findPath(self,maze, start, exit):
|
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
class BFS(PathFindingStrategy):
|
|
|
|
|
|
def findPath(self, maze, start, exit):
|
|
|
|
|
|
queue = deque([start])
|
2026-05-14 15:51:29 +00:00
|
|
|
|
traveled_path={start: None}
|
|
|
|
|
|
|
2026-05-09 19:37:10 +00:00
|
|
|
|
while queue:
|
|
|
|
|
|
current = queue.popleft()
|
|
|
|
|
|
if current==exit:
|
|
|
|
|
|
path=[]
|
|
|
|
|
|
while current is not None:
|
|
|
|
|
|
path.append(current)
|
2026-05-14 15:51:29 +00:00
|
|
|
|
current = traveled_path[current]
|
|
|
|
|
|
return path[::-1], len(traveled_path)
|
2026-05-09 19:37:10 +00:00
|
|
|
|
for neighbor in maze.getNeighbors(current):
|
2026-05-14 15:51:29 +00:00
|
|
|
|
if neighbor not in traveled_path:
|
|
|
|
|
|
traveled_path[neighbor] = current
|
2026-05-09 19:37:10 +00:00
|
|
|
|
queue.append(neighbor)
|
2026-05-14 15:51:29 +00:00
|
|
|
|
return [], len(traveled_path)
|
2026-05-09 19:37:10 +00:00
|
|
|
|
|
|
|
|
|
|
class DFS(PathFindingStrategy):
|
|
|
|
|
|
def findPath(self, maze, start, exit):
|
2026-05-14 15:51:29 +00:00
|
|
|
|
stack = [start]
|
|
|
|
|
|
traveled_path={start: None}
|
|
|
|
|
|
|
|
|
|
|
|
while stack:
|
|
|
|
|
|
current = stack.pop()
|
|
|
|
|
|
if current == exit:
|
|
|
|
|
|
path = []
|
|
|
|
|
|
while current is not None:
|
|
|
|
|
|
path.append(current)
|
|
|
|
|
|
current = traveled_path[current]
|
|
|
|
|
|
return path[::-1], len(traveled_path)
|
|
|
|
|
|
for neighbor in maze.getNeighbors(current):
|
|
|
|
|
|
if neighbor not in traveled_path:
|
|
|
|
|
|
traveled_path[neighbor] = current
|
|
|
|
|
|
stack.append(neighbor)
|
|
|
|
|
|
return [], len(traveled_path)
|
|
|
|
|
|
class AStar(PathFindingStrategy):
|
|
|
|
|
|
def findPath(self, maze, start, exit):
|
|
|
|
|
|
count = 0
|
|
|
|
|
|
open_set = [(0, count, start)]
|
|
|
|
|
|
traveled_path = {start: None}
|
|
|
|
|
|
g_score = {start: 0}
|
|
|
|
|
|
while open_set:
|
|
|
|
|
|
_,_,current = heapq.heappop(open_set)
|
|
|
|
|
|
if current == exit:
|
|
|
|
|
|
path = []
|
|
|
|
|
|
while current is not None:
|
|
|
|
|
|
path.append(current)
|
|
|
|
|
|
current = traveled_path[current]
|
|
|
|
|
|
return path[::-1], len(traveled_path)
|
|
|
|
|
|
for neighbor in maze.getNeighbors(current):
|
|
|
|
|
|
g_score_new = g_score[current]+1
|
|
|
|
|
|
if neighbor not in g_score or g_score_new < g_score[neighbor]:
|
|
|
|
|
|
traveled_path[neighbor] = current
|
|
|
|
|
|
g_score[neighbor] = g_score_new
|
2026-05-20 23:17:21 +00:00
|
|
|
|
f_score = g_score_new + abs(neighbor.x - exit.x) + abs(neighbor.y - exit.y)
|
2026-05-14 15:51:29 +00:00
|
|
|
|
count += 1
|
|
|
|
|
|
heapq.heappush(open_set, (f_score, count, neighbor))
|
|
|
|
|
|
return [],len(traveled_path)
|
|
|
|
|
|
|
|
|
|
|
|
#Этап 4
|
2026-05-20 23:17:21 +00:00
|
|
|
|
class SearchStats:
|
|
|
|
|
|
def __init__(self, time, visited_cells, path_length):
|
|
|
|
|
|
self.time = time
|
|
|
|
|
|
self.visited_cells = visited_cells
|
|
|
|
|
|
self.path_length = path_length
|
|
|
|
|
|
|
2026-05-14 15:51:29 +00:00
|
|
|
|
class MazeSolver:
|
|
|
|
|
|
def __init__(self, maze, strategy):
|
|
|
|
|
|
self.maze = maze
|
|
|
|
|
|
self.strategy = strategy
|
2026-05-20 23:17:21 +00:00
|
|
|
|
self.observers = []
|
|
|
|
|
|
def addObserver(self, observer):
|
|
|
|
|
|
self.observers.append(observer)
|
2026-05-14 15:51:29 +00:00
|
|
|
|
def setStrategy(self, strategy):
|
|
|
|
|
|
self.strategy = strategy
|
|
|
|
|
|
def solve(self):
|
2026-05-20 23:17:21 +00:00
|
|
|
|
start_cell = self.maze.start
|
|
|
|
|
|
exit_cell = self.maze.exit
|
2026-05-14 15:51:29 +00:00
|
|
|
|
|
|
|
|
|
|
start_time = time.perf_counter()
|
|
|
|
|
|
path, visited_cells = self.strategy.findPath(self.maze, start_cell, exit_cell)
|
|
|
|
|
|
end_time = time.perf_counter()
|
|
|
|
|
|
|
|
|
|
|
|
time_ms = (end_time - start_time) * 1000
|
|
|
|
|
|
path_length = len(path)
|
2026-05-20 23:17:21 +00:00
|
|
|
|
stats=SearchStats(time_ms, visited_cells, path_length)
|
|
|
|
|
|
event = Event("path_found", data=stats)
|
|
|
|
|
|
for observer in self.observers:
|
|
|
|
|
|
observer.update(event)
|
|
|
|
|
|
|
|
|
|
|
|
return stats
|
|
|
|
|
|
|
|
|
|
|
|
#Этап 5
|
|
|
|
|
|
#5.1
|
|
|
|
|
|
class Event:
|
|
|
|
|
|
def __init__(self, event_type, data=None):
|
|
|
|
|
|
self.event_type = event_type
|
|
|
|
|
|
self.data = data
|
|
|
|
|
|
|
|
|
|
|
|
class Observer(ABC):
|
|
|
|
|
|
@abstractmethod
|
|
|
|
|
|
def update(self, event):
|
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
class ConsoleView(Observer):
|
|
|
|
|
|
def update(self, event):
|
|
|
|
|
|
if event.event_type == "path_found":
|
|
|
|
|
|
stats=event.data
|
|
|
|
|
|
print("Путь найден:")
|
|
|
|
|
|
print("Время выполнения:", stats.time)
|
|
|
|
|
|
print("Количество посещённых клеток:", stats.visited_cells)
|
|
|
|
|
|
print("Длина найденного пути:", stats.path_length)
|
|
|
|
|
|
if event.event_type == "move":
|
|
|
|
|
|
x, y = event.data
|
|
|
|
|
|
print(f"Игрок переместился в ячейку: {x}, {y}")
|
|
|
|
|
|
if event.event_type == "maze_loaded":
|
|
|
|
|
|
print("Загружен новый лабиринт")
|
|
|
|
|
|
|
|
|
|
|
|
def render(self, maze, path):
|
|
|
|
|
|
for y in range(maze.height):
|
|
|
|
|
|
row_str=""
|
|
|
|
|
|
for x in range(maze.width):
|
|
|
|
|
|
cell=maze.getCell(x, y)
|
|
|
|
|
|
if cell == maze.start:
|
|
|
|
|
|
row_str += "S"
|
|
|
|
|
|
elif cell == maze.exit:
|
|
|
|
|
|
row_str += "E"
|
|
|
|
|
|
elif cell in path:
|
|
|
|
|
|
row_str += "·"
|
|
|
|
|
|
elif cell.isWall:
|
|
|
|
|
|
row_str += "#"
|
|
|
|
|
|
else:
|
|
|
|
|
|
row_str += " "
|
|
|
|
|
|
print(row_str)
|
|
|
|
|
|
|
|
|
|
|
|
#Этап 6
|
|
|
|
|
|
mazes = ["10x10.txt","50x50.txt","100x100.txt","empty.txt","without_exit.txt"]
|
|
|
|
|
|
results =[["лабиринт",
|
|
|
|
|
|
"стратегия",
|
|
|
|
|
|
"время_мс",
|
|
|
|
|
|
"посещено_клеток",
|
|
|
|
|
|
"длина_пути"]]
|
|
|
|
|
|
strategies = {
|
|
|
|
|
|
"BFS": BFS(),
|
|
|
|
|
|
"DFS": DFS(),
|
|
|
|
|
|
"AStar": AStar()
|
|
|
|
|
|
}
|
|
|
|
|
|
builder = TextFileMazeBuilder()
|
|
|
|
|
|
n=10
|
|
|
|
|
|
directory = os.path.join("docs", "data")
|
2026-05-21 13:30:23 +00:00
|
|
|
|
|
2026-05-20 23:17:21 +00:00
|
|
|
|
for maze_name in mazes:
|
|
|
|
|
|
print(maze_name)
|
|
|
|
|
|
file_name=os.path.join(directory, maze_name)
|
|
|
|
|
|
maze = builder.buildFromFile(file_name)
|
|
|
|
|
|
viewer=ConsoleView()
|
|
|
|
|
|
for strategy_name, strategy in strategies.items():
|
|
|
|
|
|
total_time = 0.0
|
|
|
|
|
|
total_visited = 0
|
|
|
|
|
|
total_path_length = 0
|
2026-05-14 15:51:29 +00:00
|
|
|
|
|
2026-05-20 23:17:21 +00:00
|
|
|
|
solver = MazeSolver(maze, strategy)
|
2026-05-14 15:51:29 +00:00
|
|
|
|
|
2026-05-20 23:17:21 +00:00
|
|
|
|
for _ in range(n):
|
|
|
|
|
|
stats = solver.solve()
|
|
|
|
|
|
total_time += stats.time
|
|
|
|
|
|
total_visited += stats.visited_cells
|
|
|
|
|
|
total_path_length += stats.path_length
|
|
|
|
|
|
avg_time = total_time/n
|
|
|
|
|
|
avg_visited = total_visited/n
|
|
|
|
|
|
avg_path_length = total_path_length/n
|
|
|
|
|
|
print(f"{maze_name} стратегия: {strategy_name} время_мс: {avg_time} посещено_клеток: {avg_visited} длина_пути: {avg_path_length}")
|
|
|
|
|
|
results.append([maze_name, strategy_name, avg_time, avg_visited, avg_path_length])
|
|
|
|
|
|
path, _ = strategy.findPath(maze, maze.start, maze.exit)
|
|
|
|
|
|
viewer.render(maze, path)
|
|
|
|
|
|
csv_filename = os.path.join(directory, "maze_results.csv")
|
|
|
|
|
|
with open(csv_filename, "w", newline="", encoding="utf-8-sig") as f:
|
|
|
|
|
|
writer = csv.writer(f)
|
2026-05-21 13:30:23 +00:00
|
|
|
|
writer.writerows(results)
|
|
|
|
|
|
|
|
|
|
|
|
#Графики
|
|
|
|
|
|
df = pd.read_csv(csv_filename)
|
|
|
|
|
|
fig, (ax1, ax2, ax3) = plt.subplots(1, 3, figsize=(16, 6))
|
|
|
|
|
|
|
|
|
|
|
|
sns.barplot(data=df, x='лабиринт', y='время_мс', hue='стратегия', ax=ax1)
|
|
|
|
|
|
ax1.set_title('Время выполнения алгоритмов')
|
|
|
|
|
|
ax1.set_xlabel('Лабиринты')
|
|
|
|
|
|
ax1.set_ylabel('Время (мс)')
|
|
|
|
|
|
ax1.grid(axis='y', linestyle='--', alpha=0.7)
|
|
|
|
|
|
ax1.legend()
|
|
|
|
|
|
|
|
|
|
|
|
sns.barplot(data=df, x='лабиринт', y='посещено_клеток', hue='стратегия', ax=ax2)
|
|
|
|
|
|
ax2.set_title('Количество посещенных клеток')
|
|
|
|
|
|
ax2.set_xlabel('Лабиринты')
|
|
|
|
|
|
ax2.set_ylabel('Количество клеток')
|
|
|
|
|
|
ax2.grid(axis='y', linestyle='--', alpha=0.7)
|
|
|
|
|
|
ax2.legend()
|
|
|
|
|
|
plt.tight_layout()
|
|
|
|
|
|
|
|
|
|
|
|
sns.barplot(data=df, x='лабиринт', y='длина_пути', hue='стратегия', ax=ax3)
|
|
|
|
|
|
ax3.set_title('Длина пути')
|
|
|
|
|
|
ax3.set_xlabel('Лабиринты')
|
|
|
|
|
|
ax3.set_ylabel('Количество клеток')
|
|
|
|
|
|
ax3.grid(axis='y', linestyle='--', alpha=0.7)
|
|
|
|
|
|
ax3.legend()
|
|
|
|
|
|
plt.tight_layout()
|
|
|
|
|
|
|
|
|
|
|
|
img = os.path.join(directory, "maze_graphics.png")
|
|
|
|
|
|
plt.savefig(img, dpi=300)
|
|
|
|
|
|
plt.show()
|