2026-rff_mp/YanyaevAA/task2/task_2.py

266 lines
8.6 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

from abc import ABC, abstractmethod
from collections import deque
import heapq
import time
import os
import csv
#Этап 1
class Cell:
def __init__(self, x, y, isWall=False, isStart=False, isExit=False):
self.x = x
self.y = y
self.isWall = isWall
self.isStart = isStart
self.isExit = isExit
def isPassable(self):
return not self.isWall
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]
isWall = (char == '#')
isStart = (char == 'S')
isExit = (char == 'E')
cell=Cell(x, y, isWall, isStart, isExit)
if isStart:
start_cell =cell
if isExit:
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])
traveled_path={start: None}
while queue:
current = queue.popleft()
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
queue.append(neighbor)
return [], len(traveled_path)
class DFS(PathFindingStrategy):
def findPath(self, maze, start, exit):
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
f_score = g_score_new + abs(neighbor.x - exit.x) + abs(neighbor.y - exit.y)
count += 1
heapq.heappush(open_set, (f_score, count, neighbor))
return [],len(traveled_path)
#Этап 4
class SearchStats:
def __init__(self, time, visited_cells, path_length):
self.time = time
self.visited_cells = visited_cells
self.path_length = path_length
class MazeSolver:
def __init__(self, maze, strategy):
self.maze = maze
self.strategy = strategy
self.observers = []
def addObserver(self, observer):
self.observers.append(observer)
def setStrategy(self, strategy):
self.strategy = strategy
def solve(self):
start_cell = self.maze.start
exit_cell = self.maze.exit
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)
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")
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
solver = MazeSolver(maze, strategy)
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)
path=path[1:-1]
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)
writer.writerows(results)