diff --git a/romanovpv/task 2/docs/data/main.py b/romanovpv/task 2/docs/data/main.py index 98b53cc..2b2270e 100644 --- a/romanovpv/task 2/docs/data/main.py +++ b/romanovpv/task 2/docs/data/main.py @@ -5,12 +5,28 @@ from strategies import ( DFSStrategy, AStarStrategy ) +from solver import (MazeSolver) builder = TextFileMazeBuilder() maze = builder.buildFromFile("maze.txt") -strategy = DFSStrategy() -path = strategy.findPath(maze, maze.start, maze.exit) -print(f"Метод: {strategy}") -print("Путь:\n") -for cell in path: - print(f"({cell.x},{cell.y})") \ No newline at end of file +maze.printMaze() +print("Выберете алгоритм") +print("1 - BFS") +print("2 - DFS") +print("3 - A*") +choice = input() +if choice == "1": + strategy = BFSStrategy() +elif choice == "2": + strategy = DFSStrategy() +elif choice == "3": + strategy = AStarStrategy() +else: + print("Неверный выбор") + exit() + +solver = MazeSolver(maze, strategy) +stats = solver.solve() +print("Результат:") +print(stats) + \ No newline at end of file diff --git a/romanovpv/task 2/docs/data/solver.py b/romanovpv/task 2/docs/data/solver.py new file mode 100644 index 0000000..2d9948c --- /dev/null +++ b/romanovpv/task 2/docs/data/solver.py @@ -0,0 +1,62 @@ +import time + +class SearchStats: + def __init__( + self, + time_ms, + visited_cells, + path_length + ): + self.time_ms = time_ms + self.visited_cells = visited_cells + self.path_length = path_length + def __str__(self): + return ( + f"Время: " + f"{self.time_ms:.3f} мс\n" + + f"Посещено клеток: " + f"{self.visited_cells}\n" + + f"Длина пути: " + f"{self.path_length}" + ) + +class MazeSolver: + def __init__( + self, + maze, + strategy + ): + self.maze = maze + self.strategy = strategy + def setStrategy( + self, + strategy + ): + self.strategy = strategy + def solve(self): + start_time = ( + time.perf_counter() + ) + path, visited = ( + self.strategy.findPath( + self.maze, + self.maze.start, + self.maze.exit + ) + ) + end_time = ( + time.perf_counter() + ) + time_ms = ( + (end_time-start_time) + *1000 + ) + visited = len(path) + stats = SearchStats( + time_ms, + visited, + len(path) + ) + return stats \ No newline at end of file diff --git a/romanovpv/task 2/docs/data/strategies.py b/romanovpv/task 2/docs/data/strategies.py index fdca4ed..573b507 100644 --- a/romanovpv/task 2/docs/data/strategies.py +++ b/romanovpv/task 2/docs/data/strategies.py @@ -33,10 +33,12 @@ class BFSStrategy(PathFindingStrategy): while queue: current = queue.popleft() if current == exit_cell: - return self.restorePath( + return ( + self.restorePath( parent, start, - exit_cell + exit_cell), + len(visited) ) for neighbor in maze.getNeighbors(current): if neighbor not in visited: @@ -49,7 +51,7 @@ class BFSStrategy(PathFindingStrategy): queue.append( neighbor ) - return [] + return [], len(visited) class DFSStrategy(PathFindingStrategy): def findPath( @@ -64,10 +66,12 @@ class DFSStrategy(PathFindingStrategy): while stack: current = stack.pop() if current == exit_cell: - return self.restorePath( + return (self.restorePath + ( parent, start, - exit_cell + exit_cell), + len(visited) ) for neighbor in maze.getNeighbors(current): if neighbor not in visited: @@ -81,7 +85,7 @@ class DFSStrategy(PathFindingStrategy): stack.append( neighbor ) - return [] + return [], len(visited) class AStarStrategy(PathFindingStrategy): def heuristic( @@ -89,17 +93,7 @@ class AStarStrategy(PathFindingStrategy): cell, exit_cell ): - return ( - abs( - cell.x - - exit_cell.x - ) - + - abs( - cell.y - - exit_cell.y - ) - ) + return (abs(cell.x - exit_cell.x) + abs(cell.y - exit_cell.y)) def findPath( self, maze, @@ -132,36 +126,24 @@ class AStarStrategy(PathFindingStrategy): current ) if current == exit_cell: - return self.restorePath( + return (self.restorePath( parent, start, - exit_cell + exit_cell), + len(visited) ) for neighbor in maze.getNeighbors(current): new_cost = ( g_score[current] + 1 ) - if ( - neighbor - not in g_score - or - new_cost < - g_score[neighbor] - ): - g_score[ - neighbor + if (neighbor not in g_score or new_cost < g_score[neighbor] ): + g_score[neighbor ] = new_cost parent[ neighbor ] = current - priority = ( - new_cost -+ - self.heuristic( - neighbor, - exit_cell - ) + priority = (new_cost + self.heuristic(neighbor, exit_cell) ) heapq.heappush( pq, @@ -171,4 +153,4 @@ class AStarStrategy(PathFindingStrategy): neighbor ) ) - return [] \ No newline at end of file + return [], len(visited) \ No newline at end of file