Манхэттенская эвристика
This commit is contained in:
parent
ccb1fca2a9
commit
d8eebad0a5
90
MininaVD/docs2/data2/strategiesA_star_strategy.ipynb
Normal file
90
MininaVD/docs2/data2/strategiesA_star_strategy.ipynb
Normal file
|
|
@ -0,0 +1,90 @@
|
|||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "f8e6c2ad-712c-44a0-8ebc-ed0d67234c05",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import heapq\n",
|
||||
"from typing import List, Dict, Optional, Tuple\n",
|
||||
"from strategiesPathfinding_strategy import PathFindingStrategy\n",
|
||||
"from modelsMaze import Maze\n",
|
||||
"from modelsCell import Cell\n",
|
||||
"\n",
|
||||
"class AStarStrategy(PathFindingStrategy):\n",
|
||||
" \"\"\"Алгоритм A* с манхэттенской эвристикой.\"\"\"\n",
|
||||
" \n",
|
||||
" @property\n",
|
||||
" def name(self) -> str:\n",
|
||||
" return \"A*\"\n",
|
||||
" \n",
|
||||
" def _heuristic(self, a: Cell, b: Cell) -> int:\n",
|
||||
" \"\"\"Манхэттенское расстояние.\"\"\"\n",
|
||||
" return abs(a.x - b.x) + abs(a.y - b.y)\n",
|
||||
" \n",
|
||||
" def find_path(self, maze: Maze, start: Cell, exit_cell: Cell) -> List[Cell]:\n",
|
||||
" if start == exit_cell:\n",
|
||||
" return [start]\n",
|
||||
" \n",
|
||||
" # Приоритетная очередь: (f_score, counter, cell)\n",
|
||||
" open_set = [(0, 0, start)]\n",
|
||||
" counter = 1\n",
|
||||
" \n",
|
||||
" came_from: Dict[Cell, Optional[Cell]] = {}\n",
|
||||
" \n",
|
||||
" g_score: Dict[Cell, float] = {start: 0}\n",
|
||||
" f_score: Dict[Cell, float] = {start: self._heuristic(start, exit_cell)}\n",
|
||||
" \n",
|
||||
" visited_count = 0\n",
|
||||
" \n",
|
||||
" while open_set:\n",
|
||||
" current_f, _, current = heapq.heappop(open_set)\n",
|
||||
" visited_count += 1\n",
|
||||
" \n",
|
||||
" if current == exit_cell:\n",
|
||||
" self._last_visited_count = visited_count\n",
|
||||
" return self._reconstruct_path(came_from, start, current)\n",
|
||||
" \n",
|
||||
" for neighbor in maze.get_neighbors(current):\n",
|
||||
" tentative_g_score = g_score.get(current, float('inf')) + 1\n",
|
||||
" \n",
|
||||
" if tentative_g_score < g_score.get(neighbor, float('inf')):\n",
|
||||
" came_from[neighbor] = current\n",
|
||||
" g_score[neighbor] = tentative_g_score\n",
|
||||
" f_score[neighbor] = tentative_g_score + self._heuristic(neighbor, exit_cell)\n",
|
||||
" heapq.heappush(open_set, (f_score[neighbor], counter, neighbor))\n",
|
||||
" counter += 1\n",
|
||||
" \n",
|
||||
" self._last_visited_count = visited_count\n",
|
||||
" return []\n",
|
||||
" \n",
|
||||
" @property\n",
|
||||
" def last_visited_count(self) -> int:\n",
|
||||
" return getattr(self, '_last_visited_count', 0)"
|
||||
]
|
||||
}
|
||||
],
|
||||
"metadata": {
|
||||
"kernelspec": {
|
||||
"display_name": "Python [conda env:base] *",
|
||||
"language": "python",
|
||||
"name": "conda-base-py"
|
||||
},
|
||||
"language_info": {
|
||||
"codemirror_mode": {
|
||||
"name": "ipython",
|
||||
"version": 3
|
||||
},
|
||||
"file_extension": ".py",
|
||||
"mimetype": "text/x-python",
|
||||
"name": "python",
|
||||
"nbconvert_exporter": "python",
|
||||
"pygments_lexer": "ipython3",
|
||||
"version": "3.13.9"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 5
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user