Реализация алгоритма Дейкстры
This commit is contained in:
parent
d8fb7ab226
commit
1224a5afee
11
stepushovgs/labyrinth/mazes/tests/test_lab2.txt
Normal file
11
stepushovgs/labyrinth/mazes/tests/test_lab2.txt
Normal file
|
|
@ -0,0 +1,11 @@
|
||||||
|
#####################################
|
||||||
|
#S #
|
||||||
|
# #
|
||||||
|
# #
|
||||||
|
# #
|
||||||
|
# #
|
||||||
|
# #
|
||||||
|
# #
|
||||||
|
# #
|
||||||
|
# E#
|
||||||
|
#####################################
|
||||||
20
stepushovgs/labyrinth/mazes/tests/test_lab20x20.txt
Normal file
20
stepushovgs/labyrinth/mazes/tests/test_lab20x20.txt
Normal file
|
|
@ -0,0 +1,20 @@
|
||||||
|
####################
|
||||||
|
S # # # #
|
||||||
|
### # ### # ### # ##
|
||||||
|
# # # # # # # # ##
|
||||||
|
# ### # ### # ### ##
|
||||||
|
# # # # # ##
|
||||||
|
# ### ### ### # # ##
|
||||||
|
# # # # # # # # ##
|
||||||
|
# # # # ### # # # ##
|
||||||
|
# # # # # # # # ##
|
||||||
|
# # # ### # # # # ##
|
||||||
|
# # # # # # # ##
|
||||||
|
# # ### # ### # # ##
|
||||||
|
# # # # # # ##
|
||||||
|
# ### # ### # ### ##
|
||||||
|
# # # # # # # # ##
|
||||||
|
# # # # # # # # # ##
|
||||||
|
# # # # # # # # # ##
|
||||||
|
# # # # E#
|
||||||
|
####################
|
||||||
|
|
@ -58,7 +58,7 @@ class ConsoleView(Observer):
|
||||||
elif c.toStr() in ["S", "E"]:
|
elif c.toStr() in ["S", "E"]:
|
||||||
print(c.toStr(), end='')
|
print(c.toStr(), end='')
|
||||||
elif c.getXY() in path_xy:
|
elif c.getXY() in path_xy:
|
||||||
print('*', end='')
|
print('.', end='')
|
||||||
else:
|
else:
|
||||||
print(c.toStr(), end='')
|
print(c.toStr(), end='')
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,10 +1,56 @@
|
||||||
from source.strategy import PathFindingStrategy
|
from heapq import *
|
||||||
|
|
||||||
|
|
||||||
|
from source.strategy import PathFindingStrategy, reconstruct_path
|
||||||
from source.classes import Maze, Cell
|
from source.classes import Maze, Cell
|
||||||
|
|
||||||
|
|
||||||
class Dijkstra(PathFindingStrategy):
|
class Dijkstra(PathFindingStrategy):
|
||||||
def findPath(self, maze: Maze, start: Cell, exit: Cell):
|
@property
|
||||||
pass
|
def name(self) -> str:
|
||||||
|
"""Возвращает название метода"""
|
||||||
|
return "Dijkstra"
|
||||||
|
|
||||||
|
|
||||||
|
def findPath(self, maze: Maze):
|
||||||
|
start_cell = maze.start
|
||||||
|
exit_cell = maze.exit
|
||||||
|
|
||||||
|
queue = []
|
||||||
|
counter = 0 # счётчик для уникальности, чтобы не сравнивать клетки
|
||||||
|
|
||||||
|
heappush(queue, (0, counter, start_cell))
|
||||||
|
counter += 1
|
||||||
|
|
||||||
|
cost_visited = {start_cell.getXY(): 0}
|
||||||
|
came_from = {start_cell.getXY(): None}
|
||||||
|
visited_count = 1
|
||||||
|
|
||||||
|
while queue:
|
||||||
|
current_cost, _, current_cell = heappop(queue)
|
||||||
|
|
||||||
|
if current_cell.getXY() == exit_cell.getXY():
|
||||||
|
return reconstruct_path(
|
||||||
|
came_from=came_from,
|
||||||
|
start=start_cell,
|
||||||
|
end=current_cell
|
||||||
|
), visited_count
|
||||||
|
|
||||||
|
next_cells = maze.getNeighbors(current_cell)
|
||||||
|
|
||||||
|
for next_cell in next_cells:
|
||||||
|
neighbor_cost = next_cell.value
|
||||||
|
neighbor_cell_xy = next_cell.getXY()
|
||||||
|
|
||||||
|
new_cost = current_cost + neighbor_cost
|
||||||
|
|
||||||
|
if neighbor_cell_xy not in cost_visited or new_cost < cost_visited[neighbor_cell_xy]:
|
||||||
|
heappush(queue, (new_cost, counter, next_cell))
|
||||||
|
counter += 1
|
||||||
|
|
||||||
|
cost_visited[neighbor_cell_xy] = new_cost
|
||||||
|
came_from[neighbor_cell_xy] = current_cell
|
||||||
|
visited_count += 1
|
||||||
|
|
||||||
|
return [], visited_count
|
||||||
|
|
||||||
def __dijkstra__(self, maze: Maze, start: Cell, exit: Cell):
|
|
||||||
pass
|
|
||||||
|
|
@ -2,7 +2,7 @@
|
||||||
"cells": [
|
"cells": [
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 1,
|
"execution_count": 12,
|
||||||
"id": "4dbe48b6",
|
"id": "4dbe48b6",
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
|
|
@ -17,7 +17,19 @@
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 2,
|
"execution_count": 13,
|
||||||
|
"id": "007bf97a",
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"test_lab = './mazes/tests/test_lab.txt'\n",
|
||||||
|
"test_lab2 = './mazes/tests/test_lab2.txt'\n",
|
||||||
|
"test_lab3 = './mazes/tests/test_lab20x20.txt'"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 14,
|
||||||
"id": "4489fc7e",
|
"id": "4489fc7e",
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [
|
"outputs": [
|
||||||
|
|
@ -35,7 +47,7 @@
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"source": [
|
"source": [
|
||||||
"with open('test_lab.txt') as f:\n",
|
"with open(test_lab) as f:\n",
|
||||||
" data = f.readlines()\n",
|
" data = f.readlines()\n",
|
||||||
" for el in data:\n",
|
" for el in data:\n",
|
||||||
" print(el.rstrip())"
|
" print(el.rstrip())"
|
||||||
|
|
@ -43,7 +55,7 @@
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 3,
|
"execution_count": 15,
|
||||||
"id": "fde1eddb",
|
"id": "fde1eddb",
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [
|
"outputs": [
|
||||||
|
|
@ -64,14 +76,14 @@
|
||||||
"\n",
|
"\n",
|
||||||
"\n",
|
"\n",
|
||||||
"builder = TextFileMazeBuilder()\n",
|
"builder = TextFileMazeBuilder()\n",
|
||||||
"maze = builder.buildFromFile(filename='test_lab.txt')\n",
|
"maze = builder.buildFromFile(filename=test_lab)\n",
|
||||||
"\n",
|
"\n",
|
||||||
"maze.printer()"
|
"maze.printer()"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 4,
|
"execution_count": 16,
|
||||||
"id": "22325f68",
|
"id": "22325f68",
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [
|
"outputs": [
|
||||||
|
|
@ -80,7 +92,7 @@
|
||||||
"output_type": "stream",
|
"output_type": "stream",
|
||||||
"text": [
|
"text": [
|
||||||
"Загружен лабиринт:\n",
|
"Загружен лабиринт:\n",
|
||||||
"S*P# ###\n",
|
"S.P# ###\n",
|
||||||
"## # # E\n",
|
"## # # E\n",
|
||||||
"# # #\n",
|
"# # #\n",
|
||||||
"### ## #\n",
|
"### ## #\n",
|
||||||
|
|
@ -104,7 +116,7 @@
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 5,
|
"execution_count": 17,
|
||||||
"id": "19840429",
|
"id": "19840429",
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [
|
"outputs": [
|
||||||
|
|
@ -113,11 +125,11 @@
|
||||||
"output_type": "stream",
|
"output_type": "stream",
|
||||||
"text": [
|
"text": [
|
||||||
"Путь найден:\n",
|
"Путь найден:\n",
|
||||||
"S**# ###\n",
|
"S..# ###\n",
|
||||||
"##*# #*E\n",
|
"##.# #.E\n",
|
||||||
"# ** #*#\n",
|
"# .. #.#\n",
|
||||||
"###*##*#\n",
|
"###.##.#\n",
|
||||||
"# ****#\n",
|
"# ....#\n",
|
||||||
"########\n"
|
"########\n"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
|
|
@ -142,15 +154,12 @@
|
||||||
" (7, 1)])"
|
" (7, 1)])"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
"execution_count": 5,
|
"execution_count": 17,
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"output_type": "execute_result"
|
"output_type": "execute_result"
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"source": [
|
"source": [
|
||||||
"\n",
|
|
||||||
"\n",
|
|
||||||
"\n",
|
|
||||||
"solver = MazeSolver(maze, DFS(), ConsoleView())\n",
|
"solver = MazeSolver(maze, DFS(), ConsoleView())\n",
|
||||||
"stats = solver.solve()\n",
|
"stats = solver.solve()\n",
|
||||||
"\n",
|
"\n",
|
||||||
|
|
@ -159,7 +168,7 @@
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 6,
|
"execution_count": 18,
|
||||||
"id": "73ba37a8",
|
"id": "73ba37a8",
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [
|
"outputs": [
|
||||||
|
|
@ -168,11 +177,11 @@
|
||||||
"output_type": "stream",
|
"output_type": "stream",
|
||||||
"text": [
|
"text": [
|
||||||
"Путь найден:\n",
|
"Путь найден:\n",
|
||||||
"S**# ###\n",
|
"S..# ###\n",
|
||||||
"##*# #*E\n",
|
"##.# #.E\n",
|
||||||
"# ** #*#\n",
|
"# .. #.#\n",
|
||||||
"###*##*#\n",
|
"###.##.#\n",
|
||||||
"# ****#\n",
|
"# ....#\n",
|
||||||
"########\n"
|
"########\n"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
|
|
@ -197,15 +206,12 @@
|
||||||
" (7, 1)])"
|
" (7, 1)])"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
"execution_count": 6,
|
"execution_count": 18,
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"output_type": "execute_result"
|
"output_type": "execute_result"
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"source": [
|
"source": [
|
||||||
"\n",
|
|
||||||
"\n",
|
|
||||||
"\n",
|
|
||||||
"solver = MazeSolver(maze, BFS(), ConsoleView())\n",
|
"solver = MazeSolver(maze, BFS(), ConsoleView())\n",
|
||||||
"stats = solver.solve()\n",
|
"stats = solver.solve()\n",
|
||||||
"\n",
|
"\n",
|
||||||
|
|
@ -214,7 +220,7 @@
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 7,
|
"execution_count": 19,
|
||||||
"id": "857c5c04",
|
"id": "857c5c04",
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [
|
"outputs": [
|
||||||
|
|
@ -223,10 +229,12 @@
|
||||||
"output_type": "stream",
|
"output_type": "stream",
|
||||||
"text": [
|
"text": [
|
||||||
"0\n",
|
"0\n",
|
||||||
"2\n",
|
|
||||||
"1\n",
|
"1\n",
|
||||||
|
"4\n",
|
||||||
|
"2\n",
|
||||||
"3\n",
|
"3\n",
|
||||||
"4\n"
|
"3\n",
|
||||||
|
"2\n"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
|
@ -235,7 +243,7 @@
|
||||||
"{'0', '1', '2', '3', '4'}"
|
"{'0', '1', '2', '3', '4'}"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
"execution_count": 7,
|
"execution_count": 19,
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"output_type": "execute_result"
|
"output_type": "execute_result"
|
||||||
}
|
}
|
||||||
|
|
@ -264,7 +272,7 @@
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 8,
|
"execution_count": 20,
|
||||||
"id": "9a5ea5cb",
|
"id": "9a5ea5cb",
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [
|
"outputs": [
|
||||||
|
|
@ -350,7 +358,7 @@
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 9,
|
"execution_count": 21,
|
||||||
"id": "32edf4d1",
|
"id": "32edf4d1",
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [
|
"outputs": [
|
||||||
|
|
@ -364,7 +372,7 @@
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"source": [
|
"source": [
|
||||||
"with open('test_lab.txt') as f:\n",
|
"with open(test_lab) as f:\n",
|
||||||
" data = f.read().splitlines()\n",
|
" data = f.read().splitlines()\n",
|
||||||
" x, y = 0, 0\n",
|
" x, y = 0, 0\n",
|
||||||
" width = len(data[0])\n",
|
" width = len(data[0])\n",
|
||||||
|
|
@ -375,9 +383,91 @@
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": null,
|
"execution_count": 22,
|
||||||
"id": "48d20564",
|
"id": "48d20564",
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"name": "stdout",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
"Путь найден:\n",
|
||||||
|
"# # # # # # # # # # # # # # # # # # # #\n",
|
||||||
|
"S. # # # #\n",
|
||||||
|
"#.# # # # # # # # # # # # #\n",
|
||||||
|
"#..... # # # # # # # # #\n",
|
||||||
|
"# #.# # # # # # # # # # # #\n",
|
||||||
|
"# #............. # # # # #\n",
|
||||||
|
"# # # # # # #. # # # # # # #\n",
|
||||||
|
"# # # # #..... # # # # #\n",
|
||||||
|
"# # # # # # #. # # # # #\n",
|
||||||
|
"# # # # #. # # # # #\n",
|
||||||
|
"# # # # # # #. # # # # #\n",
|
||||||
|
"# # # # #..... # # # #\n",
|
||||||
|
"# # # # # # # # #. # # # #\n",
|
||||||
|
"# # # # #. # # #\n",
|
||||||
|
"# # # # # # # # #. # # # # #\n",
|
||||||
|
"# # # # # # #. # # # #\n",
|
||||||
|
"# # # # # # #. # # # #\n",
|
||||||
|
"# # # # # # #. # # # #\n",
|
||||||
|
"# # # #...........E #\n",
|
||||||
|
"# # # # # # # # # # # # # # # # # # # #\n"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"maze2 = builder.buildFromFile(test_lab3)\n",
|
||||||
|
"\n",
|
||||||
|
"solver = MazeSolver(maze2, BFS(), ConsoleView())\n",
|
||||||
|
"stats = solver.solve()"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 23,
|
||||||
|
"id": "bf13d5ba",
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"name": "stdout",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
"Путь найден:\n",
|
||||||
|
"# # # # # # # # # # # # # # # # # # # #\n",
|
||||||
|
"S.......# # # #\n",
|
||||||
|
"# # # .# # # # # # # # # # #\n",
|
||||||
|
"#.......# # # # # # # # #\n",
|
||||||
|
"#. # # # # # # # # # # # # #\n",
|
||||||
|
"#...# ...........# # # # #\n",
|
||||||
|
"# .# # #. # # # .# # # # # # #\n",
|
||||||
|
"#...# #...# # .....# # # # #\n",
|
||||||
|
"#. # # .# # # # .# # # # #\n",
|
||||||
|
"#...# #...# #...# # # # #\n",
|
||||||
|
"# .# #. # # # #. # # # # #\n",
|
||||||
|
"#...# #..... # #.......# # # #\n",
|
||||||
|
"#. # # # #...# # # # .# # # #\n",
|
||||||
|
"#...#.......# .# #...# # #\n",
|
||||||
|
"# .#.# # .#...# # # #. # # # # #\n",
|
||||||
|
"#...#. #...#. # # #...# # # #\n",
|
||||||
|
"#. #...#. #...# # # .# # # #\n",
|
||||||
|
"#...# .#...# .# # # .# # # #\n",
|
||||||
|
"# .....# .....# # .........E #\n",
|
||||||
|
"# # # # # # # # # # # # # # # # # # # #\n"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"maze2 = builder.buildFromFile(test_lab3)\n",
|
||||||
|
"\n",
|
||||||
|
"solver = MazeSolver(maze2, DFS(), ConsoleView())\n",
|
||||||
|
"stats = solver.solve()"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"id": "9383cb75",
|
||||||
|
"metadata": {},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": []
|
"source": []
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user