Merge pull request '[2] task2' (#343) from kuznetsovTD/2026-rff_mp:task2 into develop
Reviewed-on: #343
This commit is contained in:
commit
db85133d0c
0
kuznetsovTD/tusk 2/__init__.py
Normal file
0
kuznetsovTD/tusk 2/__init__.py
Normal file
0
kuznetsovTD/tusk 2/builders/__init__.py
Normal file
0
kuznetsovTD/tusk 2/builders/__init__.py
Normal file
3
kuznetsovTD/tusk 2/builders/maze_builder.py
Normal file
3
kuznetsovTD/tusk 2/builders/maze_builder.py
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
class MazeBuilder:
|
||||
def build_from_file(self, filename):
|
||||
raise NotImplementedError
|
||||
52
kuznetsovTD/tusk 2/builders/text_file_maze_builder.py
Normal file
52
kuznetsovTD/tusk 2/builders/text_file_maze_builder.py
Normal file
|
|
@ -0,0 +1,52 @@
|
|||
from builders.maze_builder import MazeBuilder
|
||||
from models.cell import Cell
|
||||
from models.maze import Maze
|
||||
|
||||
|
||||
class TextFileMazeBuilder(MazeBuilder):
|
||||
SYMBOL_MAP = {
|
||||
"#": {"is_wall": True},
|
||||
"S": {"is_start": True},
|
||||
"E": {"is_exit": True},
|
||||
" ": {},
|
||||
}
|
||||
|
||||
def create_cell(self, symbol, x, y):
|
||||
props = self.SYMBOL_MAP.get(symbol)
|
||||
if props is None:
|
||||
raise ValueError(f"Unknown symbol: {symbol}")
|
||||
return Cell(x, y, **props)
|
||||
|
||||
def build_from_file(self, filename):
|
||||
with open(filename, "r", encoding="utf-8") as file:
|
||||
rows = [line.rstrip("\n") for line in file]
|
||||
|
||||
if not rows:
|
||||
raise ValueError("File is empty")
|
||||
|
||||
width = len(rows[0])
|
||||
for row in rows:
|
||||
if len(row) != width:
|
||||
raise ValueError("Maze rows must have same length")
|
||||
|
||||
cells = []
|
||||
start_cell = None
|
||||
exit_cell = None
|
||||
|
||||
for y, row in enumerate(rows):
|
||||
current_row = []
|
||||
for x, symbol in enumerate(row):
|
||||
cell = self.create_cell(symbol, x, y)
|
||||
if cell.is_start:
|
||||
start_cell = cell
|
||||
if cell.is_exit:
|
||||
exit_cell = cell
|
||||
current_row.append(cell)
|
||||
cells.append(current_row)
|
||||
|
||||
if start_cell is None:
|
||||
raise ValueError("Start not found")
|
||||
if exit_cell is None:
|
||||
raise ValueError("Exit not found")
|
||||
|
||||
return Maze(cells, start_cell, exit_cell)
|
||||
0
kuznetsovTD/tusk 2/experiments/__init__.py
Normal file
0
kuznetsovTD/tusk 2/experiments/__init__.py
Normal file
42
kuznetsovTD/tusk 2/experiments/benchmark.py
Normal file
42
kuznetsovTD/tusk 2/experiments/benchmark.py
Normal file
|
|
@ -0,0 +1,42 @@
|
|||
import sys
|
||||
import os
|
||||
|
||||
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
||||
|
||||
from builders.text_file_maze_builder import TextFileMazeBuilder
|
||||
from strategies.bfs_strategy import BFSStrategy
|
||||
from strategies.dfs_strategy import DFSStrategy
|
||||
from strategies.astar_strategy import AStarStrategy
|
||||
from solver.maze_solver import MazeSolver
|
||||
|
||||
PROJECT_ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
|
||||
MAZES_DIR = os.path.join(PROJECT_ROOT, "mazes")
|
||||
|
||||
mazes = ["small.txt", "medium.txt", "large.txt", "no_exit.txt", "empty.txt"]
|
||||
strategies = [BFSStrategy(), DFSStrategy(), AStarStrategy()]
|
||||
results = []
|
||||
builder = TextFileMazeBuilder()
|
||||
|
||||
for maze_file in mazes:
|
||||
try:
|
||||
maze_path = os.path.join(MAZES_DIR, maze_file)
|
||||
maze = builder.build_from_file(maze_path)
|
||||
|
||||
for strategy in strategies:
|
||||
solver = MazeSolver(maze, strategy)
|
||||
stats = solver.solve(maze_file)
|
||||
results.append(stats)
|
||||
except ValueError as e:
|
||||
print(f"Error with {maze_file}: {e}")
|
||||
except FileNotFoundError as e:
|
||||
print(f"File not found: {e}")
|
||||
|
||||
os.makedirs(os.path.join(PROJECT_ROOT, "experiments"), exist_ok=True)
|
||||
|
||||
results_path = os.path.join(PROJECT_ROOT, "experiments", "results.csv")
|
||||
with open(results_path, "w") as file:
|
||||
file.write("maze,strategy,time_ms,visited_cells,path_length\n")
|
||||
for stat in results:
|
||||
file.write(stat.to_csv_row())
|
||||
|
||||
print(f"Saved {len(results)} results to {results_path}")
|
||||
36
kuznetsovTD/tusk 2/experiments/plot_results.py
Normal file
36
kuznetsovTD/tusk 2/experiments/plot_results.py
Normal file
|
|
@ -0,0 +1,36 @@
|
|||
import sys
|
||||
import os
|
||||
|
||||
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
||||
|
||||
import pandas as pd
|
||||
import matplotlib.pyplot as plt
|
||||
|
||||
PROJECT_ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
|
||||
RESULTS_PATH = os.path.join(PROJECT_ROOT, "experiments", "results.csv")
|
||||
PLOTS_DIR = os.path.join(PROJECT_ROOT, "experiments", "plots")
|
||||
|
||||
try:
|
||||
data = pd.read_csv(RESULTS_PATH)
|
||||
except FileNotFoundError:
|
||||
print(f"Run benchmark.py first to generate {RESULTS_PATH}")
|
||||
exit(1)
|
||||
|
||||
os.makedirs(PLOTS_DIR, exist_ok=True)
|
||||
|
||||
for maze_name in data["maze"].unique():
|
||||
maze_data = data[data["maze"] == maze_name]
|
||||
plt.figure(figsize=(8, 5))
|
||||
plt.bar(maze_data["strategy"], maze_data["time_ms"], color=['blue', 'green', 'red'])
|
||||
plt.title(f"{maze_name} maze - Performance Comparison", fontsize=14)
|
||||
plt.ylabel("Time (ms)", fontsize=12)
|
||||
plt.xlabel("Strategy", fontsize=12)
|
||||
plt.grid(axis='y', alpha=0.3)
|
||||
plt.tight_layout()
|
||||
|
||||
plot_filename = os.path.join(PLOTS_DIR, f"{maze_name.replace('.txt', '')}.png")
|
||||
plt.savefig(plot_filename, dpi=150)
|
||||
plt.close()
|
||||
print(f"Saved plot for {maze_name}")
|
||||
|
||||
print(f"All plots saved to {PLOTS_DIR}")
|
||||
BIN
kuznetsovTD/tusk 2/experiments/plots/empty.png
Normal file
BIN
kuznetsovTD/tusk 2/experiments/plots/empty.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 37 KiB |
BIN
kuznetsovTD/tusk 2/experiments/plots/large.png
Normal file
BIN
kuznetsovTD/tusk 2/experiments/plots/large.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 32 KiB |
BIN
kuznetsovTD/tusk 2/experiments/plots/medium.png
Normal file
BIN
kuznetsovTD/tusk 2/experiments/plots/medium.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 36 KiB |
BIN
kuznetsovTD/tusk 2/experiments/plots/no_exit.png
Normal file
BIN
kuznetsovTD/tusk 2/experiments/plots/no_exit.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 42 KiB |
BIN
kuznetsovTD/tusk 2/experiments/plots/small.png
Normal file
BIN
kuznetsovTD/tusk 2/experiments/plots/small.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 33 KiB |
16
kuznetsovTD/tusk 2/experiments/results.csv
Normal file
16
kuznetsovTD/tusk 2/experiments/results.csv
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
maze,strategy,time_ms,visited_cells,path_length
|
||||
small.txt,BFSStrategy,0.022,8,7
|
||||
small.txt,DFSStrategy,0.011,8,7
|
||||
small.txt,AStarStrategy,0.017,8,7
|
||||
medium.txt,BFSStrategy,0.057,53,23
|
||||
medium.txt,DFSStrategy,0.033,31,31
|
||||
medium.txt,AStarStrategy,0.070,46,23
|
||||
large.txt,BFSStrategy,0.762,718,431
|
||||
large.txt,DFSStrategy,0.482,451,431
|
||||
large.txt,AStarStrategy,1.031,591,431
|
||||
no_exit.txt,BFSStrategy,0.004,1,0
|
||||
no_exit.txt,DFSStrategy,0.002,1,0
|
||||
no_exit.txt,AStarStrategy,0.002,1,0
|
||||
empty.txt,BFSStrategy,0.113,100,19
|
||||
empty.txt,DFSStrategy,0.064,55,55
|
||||
empty.txt,AStarStrategy,0.154,100,19
|
||||
|
60
kuznetsovTD/tusk 2/main.py
Normal file
60
kuznetsovTD/tusk 2/main.py
Normal file
|
|
@ -0,0 +1,60 @@
|
|||
import sys
|
||||
import os
|
||||
|
||||
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
|
||||
|
||||
from builders.text_file_maze_builder import TextFileMazeBuilder
|
||||
from strategies.bfs_strategy import BFSStrategy
|
||||
from strategies.dfs_strategy import DFSStrategy
|
||||
from strategies.astar_strategy import AStarStrategy
|
||||
from solver.maze_solver import MazeSolver
|
||||
from observer.console_view import ConsoleView
|
||||
|
||||
PROJECT_ROOT = os.path.dirname(os.path.abspath(__file__))
|
||||
MAZES_DIR = os.path.join(PROJECT_ROOT, "mazes")
|
||||
|
||||
mazes = {
|
||||
"1": "small.txt",
|
||||
"2": "medium.txt",
|
||||
"3": "large.txt",
|
||||
"4": "no_exit.txt"
|
||||
}
|
||||
|
||||
strategies = {
|
||||
"1": BFSStrategy(),
|
||||
"2": DFSStrategy(),
|
||||
"3": AStarStrategy()
|
||||
}
|
||||
|
||||
print("Choose maze:")
|
||||
print("1 - small")
|
||||
print("2 - medium")
|
||||
print("3 - large")
|
||||
print("4 - no_exit")
|
||||
maze_choice = input("> ").strip()
|
||||
|
||||
while maze_choice not in mazes:
|
||||
print("Invalid choice. Try again.")
|
||||
maze_choice = input("> ").strip()
|
||||
|
||||
print("\nChoose strategy:")
|
||||
print("1 - BFS")
|
||||
print("2 - DFS")
|
||||
print("3 - A*")
|
||||
strategy_choice = input("> ").strip()
|
||||
|
||||
while strategy_choice not in strategies:
|
||||
print("Invalid choice. Try again.")
|
||||
strategy_choice = input("> ").strip()
|
||||
|
||||
builder = TextFileMazeBuilder()
|
||||
maze_path = os.path.join(MAZES_DIR, mazes[maze_choice])
|
||||
maze = builder.build_from_file(maze_path)
|
||||
strategy = strategies[strategy_choice]
|
||||
|
||||
solver = MazeSolver(maze, strategy)
|
||||
view = ConsoleView(maze)
|
||||
solver.attach(view)
|
||||
stats = solver.solve(mazes[maze_choice])
|
||||
|
||||
print(stats)
|
||||
0
kuznetsovTD/tusk 2/mazes/__init__.py
Normal file
0
kuznetsovTD/tusk 2/mazes/__init__.py
Normal file
10
kuznetsovTD/tusk 2/mazes/empty.txt
Normal file
10
kuznetsovTD/tusk 2/mazes/empty.txt
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
S
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
E
|
||||
50
kuznetsovTD/tusk 2/mazes/large.txt
Normal file
50
kuznetsovTD/tusk 2/mazes/large.txt
Normal file
|
|
@ -0,0 +1,50 @@
|
|||
S # # # #
|
||||
####### ### # # ########### ##### # ##### ##### #
|
||||
# # # # # # # # # #
|
||||
## # # ### ####### ##### ####### # ### ######### #
|
||||
# # # # # # # # # # # # #
|
||||
####### # # # # ### ##### # ### ### # # # # ### #
|
||||
# # # # # # # # # # # # # # # # #
|
||||
# ### # ### # ### ### # # ### ### ####### # # ###
|
||||
# # # # # # # # # # # # # #
|
||||
# # ############# # ### ### # ######### # # ### #
|
||||
# # # # # # # # #
|
||||
########### ########### # ##### ### ### # # # ###
|
||||
# # # # # # # # # # # # # #
|
||||
# # ####### # ### # ##### ### ### ### ### # # # #
|
||||
# # # # # # # # # # # # # # # #
|
||||
###### ### ### # # # # # # # ### ### ##### # # # #
|
||||
# # # # # # # # # # # # # # #
|
||||
### # # ######### ### # # # # ####### ##### # # #
|
||||
# # # # # # # # # # # # # #
|
||||
### ### # ##### # # ######### # # # # ##### # # #
|
||||
# # # # # # # # # # # #
|
||||
## # ######### # # ### ### # ### ######### ##### #
|
||||
# # # # # # # # # # # #
|
||||
##### # ### # ### ##### # # # ####### ##### # # #
|
||||
# # # # # # # # # # # # # # #
|
||||
## # ##### # # ##### ##### ### ### # ### # # # ###
|
||||
# # # # # # # # # # # # #
|
||||
##### # ### # # ##### ### # ### ######### # #####
|
||||
# # # # # # # # # # #
|
||||
# ####### ######### ### ####### # # ####### ### #
|
||||
# # # # # # # # # # # # # #
|
||||
# # ####### # # ##### # # ### ### # # # # ##### #
|
||||
# # # # # # # # # # # # # # # #
|
||||
# ##### # ####### # # # # # ### # ### # # # ### #
|
||||
# # # # # # # # # # # # # # #
|
||||
# ########### # ### ####### ### # ### # # # # # #
|
||||
# # # # # # # # # # # # #
|
||||
# # ####### ##### ########### ##### # # ##### # #
|
||||
# # # # # # # # # # #
|
||||
### ### ### # ############### # # # ##### ### ###
|
||||
# # # # # # # # # # # # # #
|
||||
# ### ### # ### ##### # # # # # ##### # ### # # #
|
||||
# # # # # # # # # # # # # #
|
||||
# # ####### # ### ######### ######### ### # # # #
|
||||
# # # # # # # # # # # # # # #
|
||||
##### # ####### # # # ### # # # # # ### ### # # #
|
||||
# # # # # # # # # # # # # # #
|
||||
## ### ##### ####### ### # # ### ##### # ### ### #
|
||||
# # # #
|
||||
################################################ E
|
||||
10
kuznetsovTD/tusk 2/mazes/medium.txt
Normal file
10
kuznetsovTD/tusk 2/mazes/medium.txt
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
S #
|
||||
### ### #
|
||||
# # #
|
||||
## # # ###
|
||||
# # #
|
||||
####### #
|
||||
# #
|
||||
## # #####
|
||||
|
||||
######## E
|
||||
10
kuznetsovTD/tusk 2/mazes/no_exit.txt
Normal file
10
kuznetsovTD/tusk 2/mazes/no_exit.txt
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
S#########
|
||||
# #
|
||||
######## #
|
||||
# #
|
||||
# ###### #
|
||||
# # #
|
||||
###### # #
|
||||
# # #
|
||||
# ########
|
||||
######## E
|
||||
5
kuznetsovTD/tusk 2/mazes/small.txt
Normal file
5
kuznetsovTD/tusk 2/mazes/small.txt
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
#####
|
||||
# S #
|
||||
# ###
|
||||
# E
|
||||
#####
|
||||
0
kuznetsovTD/tusk 2/models/__init__.py
Normal file
0
kuznetsovTD/tusk 2/models/__init__.py
Normal file
13
kuznetsovTD/tusk 2/models/cell.py
Normal file
13
kuznetsovTD/tusk 2/models/cell.py
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
class Cell:
|
||||
def __init__(self, x, y, is_wall=False, is_start=False, is_exit=False):
|
||||
self.x = x
|
||||
self.y = y
|
||||
self.is_wall = is_wall
|
||||
self.is_start = is_start
|
||||
self.is_exit = is_exit
|
||||
|
||||
def is_passable(self):
|
||||
return not self.is_wall
|
||||
|
||||
def __repr__(self):
|
||||
return f"Cell({self.x}, {self.y})"
|
||||
32
kuznetsovTD/tusk 2/models/maze.py
Normal file
32
kuznetsovTD/tusk 2/models/maze.py
Normal file
|
|
@ -0,0 +1,32 @@
|
|||
from models.cell import Cell
|
||||
|
||||
|
||||
class Maze:
|
||||
def __init__(self, cells, start_cell, exit_cell):
|
||||
self.cells = cells
|
||||
self.height = len(cells)
|
||||
self.width = len(cells[0])
|
||||
self.start_cell = start_cell
|
||||
self.exit_cell = exit_cell
|
||||
|
||||
def get_cell(self, x, y):
|
||||
if 0 <= x < self.width and 0 <= y < self.height:
|
||||
return self.cells[y][x]
|
||||
return None
|
||||
|
||||
def check_cell(self, x, y):
|
||||
cell = self.get_cell(x, y)
|
||||
return cell and cell.is_passable()
|
||||
|
||||
def get_neighbors(self, cell: Cell):
|
||||
directions = [(0, -1), (0, 1), (-1, 0), (1, 0)]
|
||||
neighbors = []
|
||||
for dx, dy in directions:
|
||||
x = cell.x + dx
|
||||
y = cell.y + dy
|
||||
if self.check_cell(x, y):
|
||||
neighbors.append(self.get_cell(x, y))
|
||||
return neighbors
|
||||
|
||||
def __repr__(self):
|
||||
return f"Maze({self.width}x{self.height})"
|
||||
21
kuznetsovTD/tusk 2/models/search_stats.py
Normal file
21
kuznetsovTD/tusk 2/models/search_stats.py
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
class SearchStats:
|
||||
def __init__(self, strategy, maze_name, duration, visited_cells, path_length):
|
||||
self.strategy = strategy
|
||||
self.maze_name = maze_name
|
||||
self.duration = duration
|
||||
self.visited_cells = visited_cells
|
||||
self.path_length = path_length
|
||||
|
||||
def to_csv_row(self):
|
||||
return f"{self.maze_name},{self.strategy},{self.duration:.3f},{self.visited_cells},{self.path_length}\n"
|
||||
|
||||
def __str__(self):
|
||||
return (
|
||||
f"\n=== SEARCH RESULT ===\n"
|
||||
f"Strategy : {self.strategy}\n"
|
||||
f"Maze : {self.maze_name}\n"
|
||||
f"Time (ms) : {self.duration:.3f}\n"
|
||||
f"Visited cells : {self.visited_cells}\n"
|
||||
f"Path length : {self.path_length}\n"
|
||||
f"=====================\n"
|
||||
)
|
||||
0
kuznetsovTD/tusk 2/observer/__init__.py
Normal file
0
kuznetsovTD/tusk 2/observer/__init__.py
Normal file
37
kuznetsovTD/tusk 2/observer/console_view.py
Normal file
37
kuznetsovTD/tusk 2/observer/console_view.py
Normal file
|
|
@ -0,0 +1,37 @@
|
|||
import os
|
||||
from observer.observer import Observer
|
||||
from observer.maze_event import MazeEventType
|
||||
|
||||
|
||||
class ConsoleView(Observer):
|
||||
def __init__(self, maze=None):
|
||||
self.maze = maze
|
||||
self.path = []
|
||||
|
||||
def update(self, event):
|
||||
if event.event_type == MazeEventType.PATH_FOUND:
|
||||
self.path = event.data if event.data else []
|
||||
self.render()
|
||||
|
||||
def render(self):
|
||||
if self.maze is None:
|
||||
return
|
||||
|
||||
os.system("cls" if os.name == "nt" else "clear")
|
||||
path_positions = {(cell.x, cell.y) for cell in self.path}
|
||||
|
||||
for row in self.maze.cells:
|
||||
line = ""
|
||||
for cell in row:
|
||||
pos = (cell.x, cell.y)
|
||||
if cell.is_wall:
|
||||
line += "#"
|
||||
elif cell.is_start:
|
||||
line += "S"
|
||||
elif cell.is_exit:
|
||||
line += "E"
|
||||
elif pos in path_positions:
|
||||
line += "*"
|
||||
else:
|
||||
line += " "
|
||||
print(line)
|
||||
12
kuznetsovTD/tusk 2/observer/maze_event.py
Normal file
12
kuznetsovTD/tusk 2/observer/maze_event.py
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
from enum import Enum
|
||||
|
||||
|
||||
class MazeEventType(Enum):
|
||||
MAZE_LOADED = 1
|
||||
PATH_FOUND = 2
|
||||
|
||||
|
||||
class MazeEvent:
|
||||
def __init__(self, event_type, data=None):
|
||||
self.event_type = event_type
|
||||
self.data = data
|
||||
3
kuznetsovTD/tusk 2/observer/observer.py
Normal file
3
kuznetsovTD/tusk 2/observer/observer.py
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
class Observer:
|
||||
def update(self, event):
|
||||
raise NotImplementedError
|
||||
13
kuznetsovTD/tusk 2/observer/subject.py
Normal file
13
kuznetsovTD/tusk 2/observer/subject.py
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
class Subject:
|
||||
def __init__(self):
|
||||
self.observers = []
|
||||
|
||||
def attach(self, observer):
|
||||
self.observers.append(observer)
|
||||
|
||||
def detach(self, observer):
|
||||
self.observers.remove(observer)
|
||||
|
||||
def notify(self, event):
|
||||
for observer in self.observers:
|
||||
observer.update(event)
|
||||
0
kuznetsovTD/tusk 2/solver/__init__.py
Normal file
0
kuznetsovTD/tusk 2/solver/__init__.py
Normal file
31
kuznetsovTD/tusk 2/solver/maze_solver.py
Normal file
31
kuznetsovTD/tusk 2/solver/maze_solver.py
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
import time
|
||||
from observer.subject import Subject
|
||||
from observer.maze_event import MazeEvent, MazeEventType
|
||||
from models.search_stats import SearchStats
|
||||
|
||||
|
||||
class MazeSolver(Subject):
|
||||
def __init__(self, maze, strategy):
|
||||
super().__init__()
|
||||
self.maze = maze
|
||||
self.strategy = strategy
|
||||
|
||||
def set_strategy(self, strategy):
|
||||
self.strategy = strategy
|
||||
|
||||
def solve(self, maze_name="maze"):
|
||||
start_time = time.perf_counter()
|
||||
path, visited = self.strategy.find_path(
|
||||
self.maze, self.maze.start_cell, self.maze.exit_cell
|
||||
)
|
||||
end_time = time.perf_counter()
|
||||
|
||||
self.notify(MazeEvent(MazeEventType.PATH_FOUND, path))
|
||||
|
||||
return SearchStats(
|
||||
strategy=self.strategy.__class__.__name__,
|
||||
maze_name=maze_name,
|
||||
duration=(end_time - start_time) * 1000,
|
||||
visited_cells=visited,
|
||||
path_length=len(path) if path else 0 # ЭТУ СТРОКУ ИЗМЕНИТЬ
|
||||
)
|
||||
0
kuznetsovTD/tusk 2/strategies/__init__.py
Normal file
0
kuznetsovTD/tusk 2/strategies/__init__.py
Normal file
45
kuznetsovTD/tusk 2/strategies/astar_strategy.py
Normal file
45
kuznetsovTD/tusk 2/strategies/astar_strategy.py
Normal file
|
|
@ -0,0 +1,45 @@
|
|||
import heapq
|
||||
import itertools
|
||||
from strategies.pathfinding_strategy import PathFindingStrategy
|
||||
|
||||
|
||||
class AStarStrategy(PathFindingStrategy):
|
||||
def heuristic(self, cell, exit_cell):
|
||||
return abs(cell.x - exit_cell.x) + abs(cell.y - exit_cell.y)
|
||||
|
||||
def find_path(self, maze, start_cell, exit_cell):
|
||||
open_set = []
|
||||
counter = itertools.count()
|
||||
heapq.heappush(open_set, (0, next(counter), start_cell))
|
||||
|
||||
parents = {start_cell: None}
|
||||
g_score = {start_cell: 0}
|
||||
visited = set()
|
||||
visited_count = 0
|
||||
|
||||
while open_set:
|
||||
_, _, current = heapq.heappop(open_set)
|
||||
|
||||
if current in visited:
|
||||
continue
|
||||
|
||||
visited.add(current)
|
||||
visited_count += 1
|
||||
|
||||
if current == exit_cell:
|
||||
path = []
|
||||
while current is not None:
|
||||
path.append(current)
|
||||
current = parents[current]
|
||||
path.reverse()
|
||||
return path, visited_count
|
||||
|
||||
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]:
|
||||
parents[neighbor] = current
|
||||
g_score[neighbor] = tentative_g
|
||||
f_score = tentative_g + self.heuristic(neighbor, exit_cell)
|
||||
heapq.heappush(open_set, (f_score, next(counter), neighbor))
|
||||
|
||||
return [], visited_count
|
||||
31
kuznetsovTD/tusk 2/strategies/bfs_strategy.py
Normal file
31
kuznetsovTD/tusk 2/strategies/bfs_strategy.py
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
from collections import deque
|
||||
from strategies.pathfinding_strategy import PathFindingStrategy
|
||||
|
||||
|
||||
class BFSStrategy(PathFindingStrategy):
|
||||
def find_path(self, maze, start_cell, exit_cell):
|
||||
queue = deque([start_cell])
|
||||
parents = {start_cell: None}
|
||||
visited = {start_cell}
|
||||
visited_count = 0
|
||||
|
||||
while queue:
|
||||
current = queue.popleft()
|
||||
visited_count += 1
|
||||
|
||||
if current == exit_cell:
|
||||
path = []
|
||||
while current is not None:
|
||||
path.append(current)
|
||||
current = parents[current]
|
||||
path.reverse()
|
||||
return path, visited_count
|
||||
|
||||
for neighbor in maze.get_neighbors(current):
|
||||
if neighbor in visited:
|
||||
continue
|
||||
visited.add(neighbor)
|
||||
parents[neighbor] = current
|
||||
queue.append(neighbor)
|
||||
|
||||
return [], visited_count
|
||||
30
kuznetsovTD/tusk 2/strategies/dfs_strategy.py
Normal file
30
kuznetsovTD/tusk 2/strategies/dfs_strategy.py
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
from strategies.pathfinding_strategy import PathFindingStrategy
|
||||
|
||||
|
||||
class DFSStrategy(PathFindingStrategy):
|
||||
def find_path(self, maze, start_cell, exit_cell):
|
||||
stack = [start_cell]
|
||||
parents = {start_cell: None}
|
||||
visited = {start_cell}
|
||||
visited_count = 0
|
||||
|
||||
while stack:
|
||||
current = stack.pop()
|
||||
visited_count += 1
|
||||
|
||||
if current == exit_cell:
|
||||
path = []
|
||||
while current is not None:
|
||||
path.append(current)
|
||||
current = parents[current]
|
||||
path.reverse()
|
||||
return path, visited_count
|
||||
|
||||
for neighbor in maze.get_neighbors(current):
|
||||
if neighbor in visited:
|
||||
continue
|
||||
visited.add(neighbor)
|
||||
parents[neighbor] = current
|
||||
stack.append(neighbor)
|
||||
|
||||
return [], visited_count
|
||||
3
kuznetsovTD/tusk 2/strategies/pathfinding_strategy.py
Normal file
3
kuznetsovTD/tusk 2/strategies/pathfinding_strategy.py
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
class PathFindingStrategy:
|
||||
def find_path(self, maze, start_cell, exit_cell):
|
||||
raise NotImplementedError
|
||||
Loading…
Reference in New Issue
Block a user