[2] task2

This commit is contained in:
volkovim 2026-05-24 21:48:59 +03:00
parent a94ba3321a
commit e6c58ac054
28 changed files with 1214 additions and 0 deletions

View File

@ -0,0 +1,8 @@
from abc import ABC, abstractmethod
class MazeBuilder(ABC):
@abstractmethod
def buildFromFile(self, filename):
pass

View File

@ -0,0 +1,102 @@
from builders.maze_builder import MazeBuilder
from core.cell import Cell
from core.maze import Maze
class TextFileMazeBuilder(MazeBuilder):
def buildFromFile(self, filename):
with open(filename, "r", encoding="utf-8") as source:
raw_lines = [line.rstrip("\n") for line in source]
if not raw_lines:
raise ValueError("Maze file is empty")
expected_width = len(raw_lines[0])
blueprint = []
start_cell = None
exit_cell = None
for y_index, raw in enumerate(raw_lines):
if len(raw) != expected_width:
raise ValueError(
f"Broken maze shape at line {y_index + 1}"
)
row_pack = []
for x_index, symbol in enumerate(raw):
current = None
if symbol == "#":
current = Cell(
x_index,
y_index,
isWall=True
)
elif symbol == " ":
current = Cell(
x_index,
y_index
)
elif symbol == "S":
if start_cell:
raise ValueError(
"Multiple start cells detected"
)
current = Cell(
x_index,
y_index,
isStart=True
)
start_cell = current
elif symbol == "E":
if exit_cell:
raise ValueError(
"Multiple exit cells detected"
)
current = Cell(
x_index,
y_index,
isExit=True
)
exit_cell = current
else:
raise ValueError(
f"Unsupported symbol '{symbol}' "
f"at ({x_index}, {y_index})"
)
row_pack.append(current)
blueprint.append(row_pack)
if start_cell is None:
raise ValueError(
"Start cell S not found"
)
if exit_cell is None:
raise ValueError(
"Exit cell E not found"
)
return Maze(
blueprint,
start_cell=start_cell,
exit_cell=exit_cell
)

View File

@ -0,0 +1,12 @@
from abc import ABC, abstractmethod
class Command(ABC):
@abstractmethod
def execute(self):
pass
@abstractmethod
def undo(self):
pass

View File

@ -0,0 +1,65 @@
from command.command import Command
class MoveCommand(Command):
def __init__(
self,
player,
maze,
direction
):
self.player = player
self.maze = maze
self.direction = direction
self.previous = None
def _targetCell(self):
offsets = {
"W": (0, -1),
"S": (0, 1),
"A": (-1, 0),
"D": (1, 0)
}
dx, dy = offsets.get(
self.direction.upper(),
(0, 0)
)
x, y = self.player.getPosition()
return self.maze.getCell(
x + dx,
y + dy
)
def execute(self):
destination = self._targetCell()
if destination is None:
return False
if not destination.isPassable():
return False
self.previous = self.player.current
self.player.place(
destination
)
return True
def undo(self):
if self.previous is None:
return False
self.player.place(
self.previous
)
return True

View File

@ -0,0 +1,13 @@
class Player:
def __init__(self, start_cell):
self.current = start_cell
def place(self, cell):
self.current = cell
def getPosition(self):
return self.current.getPosition()
def __str__(self):
x, y = self.getPosition()
return f"Player({x}, {y})"

View File

@ -0,0 +1,40 @@
class Cell:
def __init__(
self,
x: int,
y: int,
isWall: bool = False,
isStart: bool = False,
isExit: bool = False
):
self.x = x
self.y = y
self.isWall = isWall
self.isStart = isStart
self.isExit = isExit
def isPassable(self) -> bool:
return self.isWall is False
def getPosition(self):
return self.x, self.y
def __str__(self):
if self.isStart:
return "S"
if self.isExit:
return "E"
if self.isWall:
return "#"
return " "
def __repr__(self):
return (
f"Cell(x={self.x}, y={self.y}, "
f"wall={self.isWall}, "
f"start={self.isStart}, "
f"exit={self.isExit})"
)

View File

@ -0,0 +1,59 @@
from core.cell import Cell
class Maze:
def __init__(self, cells_map, start_cell=None, exit_cell=None):
self.cells = cells_map
self.start = start_cell
self.exit = exit_cell
self.height = len(cells_map)
self.width = len(cells_map[0]) if self.height else 0
def getCell(self, x: int, y: int):
if y < 0 or y >= self.height:
return None
if x < 0 or x >= self.width:
return None
return self.cells[y][x]
def getNeighbors(self, current: Cell):
reachable = []
top = self.getCell(current.x, current.y - 1)
right = self.getCell(current.x + 1, current.y)
bottom = self.getCell(current.x, current.y + 1)
left = self.getCell(current.x - 1, current.y)
for candidate in (top, right, bottom, left):
if candidate is None:
continue
if candidate.isPassable():
reachable.append(candidate)
return reachable
def hasStart(self):
return self.start is not None
def hasExit(self):
return self.exit is not None
def size(self):
return self.width, self.height
def __str__(self):
rows = []
for line in self.cells:
visual = ""
for cell in line:
visual += str(cell)
rows.append(visual)
return "\n".join(rows)

View File

@ -0,0 +1,22 @@
class SearchStats:
def __init__(
self,
strategy_name,
elapsed_ms,
visited_cells,
path_length
):
self.strategy_name = strategy_name
self.elapsed_ms = elapsed_ms
self.visited_cells = visited_cells
self.path_length = path_length
def __str__(self):
lines = [
f"Strategy: {self.strategy_name}",
f"Time: {self.elapsed_ms:.3f} ms",
f"Visited: {self.visited_cells}",
f"Path length: {self.path_length}"
]
return "\n".join(lines)

View File

@ -0,0 +1,93 @@
import csv
from solver.maze_solver import MazeSolver
class BenchmarkRunner:
def __init__(
self,
maze,
strategies,
cycles=5
):
self.maze = maze
self.strategies = strategies
self.cycles = cycles
def launch(self):
report = []
for strategy in self.strategies:
solver = MazeSolver(
self.maze,
strategy
)
total_time = 0
total_visited = 0
total_path = 0
for _ in range(self.cycles):
_, stats = solver.solve()
total_time += stats.time_ms
total_visited += stats.visited_cells
total_path += stats.path_length
report.append(
{
"maze": "",
"strategy":
strategy.__class__.__name__,
"time_ms":
round(
total_time / self.cycles,
4
),
"visited_cells":
round(
total_visited / self.cycles,
2
),
"path_length":
round(
total_path / self.cycles,
2
)
}
)
return report
def exportCSV(
self,
filename,
results
):
with open(
filename,
"w",
newline="",
encoding="utf-8"
) as file:
writer = csv.DictWriter(
file,
fieldnames=[
"maze",
"strategy",
"time_ms",
"visited_cells",
"path_length"
]
)
writer.writeheader()
for row in results:
writer.writerow(row)

View File

@ -0,0 +1,161 @@
import csv
import matplotlib.pyplot as plt
class ChartBuilder:
def __init__(
self,
csv_file
):
self.csv_file = csv_file
def _read(self):
rows = []
with open(
self.csv_file,
"r",
encoding="utf-8"
) as file:
reader = csv.DictReader(file)
for row in reader:
rows.append(row)
return rows
def buildTimeChart(self):
rows = self._read()
labels = []
values = []
for row in rows:
labels.append(
f"{row['maze']}\n"
f"{row['strategy']}"
)
values.append(
float(
row["time_ms"]
)
)
plt.figure()
plt.bar(
labels,
values
)
plt.title(
"Search Time"
)
plt.ylabel(
"Milliseconds"
)
plt.xticks(
rotation=45
)
plt.tight_layout()
plt.show()
def buildVisitedChart(self):
rows = self._read()
labels = []
values = []
for row in rows:
labels.append(
f"{row['maze']}\n"
f"{row['strategy']}"
)
values.append(
float(
row[
"visited_cells"
]
)
)
plt.figure()
plt.bar(
labels,
values
)
plt.title(
"Visited Cells"
)
plt.ylabel(
"Cells"
)
plt.xticks(
rotation=45
)
plt.tight_layout()
plt.show()
def buildPathChart(self):
rows = self._read()
labels = []
values = []
for row in rows:
labels.append(
f"{row['maze']}\n"
f"{row['strategy']}"
)
values.append(
float(
row[
"path_length"
]
)
)
plt.figure()
plt.bar(
labels,
values
)
plt.title(
"Path Length"
)
plt.ylabel(
"Cells"
)
plt.xticks(
rotation=45
)
plt.tight_layout()
plt.show()

View File

@ -0,0 +1,16 @@
maze,strategy,time_ms,visited_cells,path_length
small.txt,BFSStrategy,0.0676,53.0,23.0
small.txt,DFSStrategy,0.0527,31.0,31.0
small.txt,AStarStrategy,0.0682,46.0,23.0
medium.txt,BFSStrategy,0.7144,717.0,431.0
medium.txt,DFSStrategy,0.6878,737.0,431.0
medium.txt,AStarStrategy,0.6968,591.0,431.0
large.txt,BFSStrategy,2.103,2491.0,1171.0
large.txt,DFSStrategy,2.7719,3019.0,1243.0
large.txt,AStarStrategy,2.5953,1995.0,1171.0
empty.txt,BFSStrategy,0.027,19.0,8.0
empty.txt,DFSStrategy,0.0115,8.0,8.0
empty.txt,AStarStrategy,0.0151,8.0,8.0
blocked.txt,BFSStrategy,0.002,1.0,0.0
blocked.txt,DFSStrategy,0.0013,1.0,0.0
blocked.txt,AStarStrategy,0.0019,1.0,0.0
Internal Server Error - DRE.lab repo

Internal Server Error

Gitea Version: 1.22.0