From e54b6c0a7e011ac36f4d0f09c06955f6dddd56f5 Mon Sep 17 00:00:00 2001 From: GordStep Date: Fri, 22 May 2026 22:10:16 +0300 Subject: [PATCH] =?UTF-8?q?=D0=B8=D1=81=D0=BF=D1=80=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=D0=B8=D0=B5=20=D0=BC=D0=B5=D1=82=D0=BE=D0=B4=D0=B0?= =?UTF-8?q?=20name?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- stepushovgs/labyrinth/source/strategy/BFS.py | 1 + .../labyrinth/source/strategy/astar.py | 4 + .../labyrinth/source/strategy/maze_solver.py | 8 +- stepushovgs/labyrinth/test.ipynb | 159 +++++++++++------- 4 files changed, 106 insertions(+), 66 deletions(-) diff --git a/stepushovgs/labyrinth/source/strategy/BFS.py b/stepushovgs/labyrinth/source/strategy/BFS.py index e616e7c..cb88a47 100644 --- a/stepushovgs/labyrinth/source/strategy/BFS.py +++ b/stepushovgs/labyrinth/source/strategy/BFS.py @@ -5,6 +5,7 @@ from source.strategy import PathFindingStrategy, reconstruct_path from source.classes import Maze, Cell class BFS(PathFindingStrategy): + @property def name(self): """Возвращает название метода""" return "BFS" diff --git a/stepushovgs/labyrinth/source/strategy/astar.py b/stepushovgs/labyrinth/source/strategy/astar.py index 84f5552..933a139 100644 --- a/stepushovgs/labyrinth/source/strategy/astar.py +++ b/stepushovgs/labyrinth/source/strategy/astar.py @@ -3,6 +3,10 @@ from source.classes import Maze, Cell class AStar(PathFindingStrategy): + @property + def name(self) -> str: + return "A*" + def findPath(self, maze: Maze, start: Cell, exit: Cell): pass diff --git a/stepushovgs/labyrinth/source/strategy/maze_solver.py b/stepushovgs/labyrinth/source/strategy/maze_solver.py index 269a24f..89c3859 100644 --- a/stepushovgs/labyrinth/source/strategy/maze_solver.py +++ b/stepushovgs/labyrinth/source/strategy/maze_solver.py @@ -12,7 +12,7 @@ class MazeSolver: self.strategy = strategy self.observer = observer - def strategyName(self): + def strategyName(self) -> str: return self.strategy.name def setStrategy(self, strategy: PathFindingStrategy): @@ -45,4 +45,8 @@ class SearchStats: self.timeMs = timeMs self.visitedCells = visitedCells self.pathLength = pathLength - self.path = path \ No newline at end of file + self.path = path + + def show(self): + """Вывод информации о тесте в консоль""" + print(f'time: {self.timeMs} ms\nvisited cells: {self.visitedCells}\npath length: {self.pathLength}') \ No newline at end of file diff --git a/stepushovgs/labyrinth/test.ipynb b/stepushovgs/labyrinth/test.ipynb index 03ff248..6f87ca3 100644 --- a/stepushovgs/labyrinth/test.ipynb +++ b/stepushovgs/labyrinth/test.ipynb @@ -2,14 +2,14 @@ "cells": [ { "cell_type": "code", - "execution_count": 12, + "execution_count": 1, "id": "4dbe48b6", "metadata": {}, "outputs": [], "source": [ "from source.builder import TextFileMazeBuilder\n", "from source.observer import ConsoleView, Event\n", - "from source.strategy import MazeSolver, BFS, DFS\n", + "from source.strategy import MazeSolver, BFS, DFS, Dijkstra\n", "# from source.strategy.maze_solver import \n", "from source.classes import Cell\n", "# from source.strategy import " @@ -17,7 +17,7 @@ }, { "cell_type": "code", - "execution_count": 13, + "execution_count": 2, "id": "007bf97a", "metadata": {}, "outputs": [], @@ -29,7 +29,7 @@ }, { "cell_type": "code", - "execution_count": 14, + "execution_count": 3, "id": "4489fc7e", "metadata": {}, "outputs": [ @@ -55,7 +55,7 @@ }, { "cell_type": "code", - "execution_count": 15, + "execution_count": 4, "id": "fde1eddb", "metadata": {}, "outputs": [ @@ -83,7 +83,7 @@ }, { "cell_type": "code", - "execution_count": 16, + "execution_count": 5, "id": "22325f68", "metadata": {}, "outputs": [ @@ -116,7 +116,7 @@ }, { "cell_type": "code", - "execution_count": 17, + "execution_count": 6, "id": "19840429", "metadata": {}, "outputs": [ @@ -154,7 +154,7 @@ " (7, 1)])" ] }, - "execution_count": 17, + "execution_count": 6, "metadata": {}, "output_type": "execute_result" } @@ -168,7 +168,7 @@ }, { "cell_type": "code", - "execution_count": 18, + "execution_count": 7, "id": "73ba37a8", "metadata": {}, "outputs": [ @@ -206,7 +206,7 @@ " (7, 1)])" ] }, - "execution_count": 18, + "execution_count": 7, "metadata": {}, "output_type": "execute_result" } @@ -220,7 +220,7 @@ }, { "cell_type": "code", - "execution_count": 19, + "execution_count": 8, "id": "857c5c04", "metadata": {}, "outputs": [ @@ -229,12 +229,11 @@ "output_type": "stream", "text": [ "0\n", + "2\n", "1\n", "4\n", - "2\n", "3\n", - "3\n", - "2\n" + "3\n" ] }, { @@ -243,7 +242,7 @@ "{'0', '1', '2', '3', '4'}" ] }, - "execution_count": 19, + "execution_count": 8, "metadata": {}, "output_type": "execute_result" } @@ -272,7 +271,7 @@ }, { "cell_type": "code", - "execution_count": 20, + "execution_count": 9, "id": "9a5ea5cb", "metadata": {}, "outputs": [ @@ -358,7 +357,7 @@ }, { "cell_type": "code", - "execution_count": 21, + "execution_count": 10, "id": "32edf4d1", "metadata": {}, "outputs": [ @@ -383,7 +382,7 @@ }, { "cell_type": "code", - "execution_count": 22, + "execution_count": 11, "id": "48d20564", "metadata": {}, "outputs": [ @@ -391,40 +390,37 @@ "name": "stdout", "output_type": "stream", "text": [ + "BFS\n", "Путь найден:\n", - "# # # # # # # # # # # # # # # # # # # #\n", - "S. # # # #\n", - "#.# # # # # # # # # # # # #\n", - "#..... # # # # # # # # #\n", - "# #.# # # # # # # # # # # #\n", - "# #............. # # # # #\n", - "# # # # # # #. # # # # # # #\n", - "# # # # #..... # # # # #\n", - "# # # # # # #. # # # # #\n", - "# # # # #. # # # # #\n", - "# # # # # # #. # # # # #\n", - "# # # # #..... # # # #\n", - "# # # # # # # # #. # # # #\n", - "# # # # #. # # #\n", - "# # # # # # # # #. # # # # #\n", - "# # # # # # #. # # # #\n", - "# # # # # # #. # # # #\n", - "# # # # # # #. # # # #\n", - "# # # #...........E #\n", - "# # # # # # # # # # # # # # # # # # # #\n" + "#####################################\n", + "#S #\n", + "#. #\n", + "#. #\n", + "#. #\n", + "#. #\n", + "#. #\n", + "#. #\n", + "#. #\n", + "#..................................E#\n", + "#####################################\n", + "time: 0.000567700000374316 ms\n", + "visited cells: 315\n", + "path length: 43\n" ] } ], "source": [ - "maze2 = builder.buildFromFile(test_lab3)\n", + "maze2 = builder.buildFromFile(test_lab2)\n", "\n", "solver = MazeSolver(maze2, BFS(), ConsoleView())\n", - "stats = solver.solve()" + "print(solver.strategyName())\n", + "stats = solver.solve()\n", + "stats.show()" ] }, { "cell_type": "code", - "execution_count": 23, + "execution_count": 12, "id": "bf13d5ba", "metadata": {}, "outputs": [ @@ -432,41 +428,76 @@ "name": "stdout", "output_type": "stream", "text": [ + "DFS\n", "Путь найден:\n", - "# # # # # # # # # # # # # # # # # # # #\n", - "S.......# # # #\n", - "# # # .# # # # # # # # # # #\n", - "#.......# # # # # # # # #\n", - "#. # # # # # # # # # # # # #\n", - "#...# ...........# # # # #\n", - "# .# # #. # # # .# # # # # # #\n", - "#...# #...# # .....# # # # #\n", - "#. # # .# # # # .# # # # #\n", - "#...# #...# #...# # # # #\n", - "# .# #. # # # #. # # # # #\n", - "#...# #..... # #.......# # # #\n", - "#. # # # #...# # # # .# # # #\n", - "#...#.......# .# #...# # #\n", - "# .#.# # .#...# # # #. # # # # #\n", - "#...#. #...#. # # #...# # # #\n", - "#. #...#. #...# # # .# # # #\n", - "#...# .#...# .# # # .# # # #\n", - "# .....# .....# # .........E #\n", - "# # # # # # # # # # # # # # # # # # # #\n" + "#####################################\n", + "#S..................................#\n", + "# .#\n", + "#...................................#\n", + "#. #\n", + "#...................................#\n", + "# .#\n", + "#...................................#\n", + "#. #\n", + "#..................................E#\n", + "#####################################\n", + "time: 0.0004403000002639601 ms\n", + "visited cells: 315\n", + "path length: 179\n" ] } ], "source": [ - "maze2 = builder.buildFromFile(test_lab3)\n", + "maze2 = builder.buildFromFile(test_lab2)\n", "\n", "solver = MazeSolver(maze2, DFS(), ConsoleView())\n", - "stats = solver.solve()" + "print(solver.strategyName())\n", + "stats = solver.solve()\n", + "stats.show()" + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "id": "9383cb75", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Dijkstra\n", + "Путь найден:\n", + "#####################################\n", + "#S #\n", + "#. #\n", + "#. #\n", + "#. #\n", + "#. #\n", + "#. #\n", + "#. #\n", + "#. #\n", + "#..................................E#\n", + "#####################################\n", + "time: 0.0007290000003195019 ms\n", + "visited cells: 315\n", + "path length: 43\n" + ] + } + ], + "source": [ + "maze2 = builder.buildFromFile(test_lab2)\n", + "\n", + "solver = MazeSolver(maze2, Dijkstra(), ConsoleView())\n", + "print(solver.strategyName())\n", + "stats = solver.solve()\n", + "stats.show()" ] }, { "cell_type": "code", "execution_count": null, - "id": "9383cb75", + "id": "835cff61", "metadata": {}, "outputs": [], "source": []