2026-rff_mp/pomelovsd/ExitMaze/main.ipynb
2026-05-23 13:37:43 +03:00

348 lines
13 KiB
Plaintext

{
"cells": [
{
"cell_type": "code",
"execution_count": 21,
"id": "a1dff6b4",
"metadata": {},
"outputs": [],
"source": [
"import time\n",
"import csv\n",
"import matplotlib.pyplot as plt"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "66bfd079",
"metadata": {},
"outputs": [
{
"ename": "ModuleNotFoundError",
"evalue": "No module named 'path'",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mModuleNotFoundError\u001b[0m Traceback (most recent call last)",
"Cell \u001b[0;32mIn[4], line 5\u001b[0m\n\u001b[1;32m 3\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01mMazeSolver\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mSolver\u001b[39;00m \u001b[38;5;28;01mimport\u001b[39;00m MazeSolver\n\u001b[1;32m 4\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01mObserver_Command\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mConsoleView\u001b[39;00m \u001b[38;5;28;01mimport\u001b[39;00m ConsoleView\n\u001b[0;32m----> 5\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01mStrategies\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mBFS\u001b[39;00m \u001b[38;5;28;01mimport\u001b[39;00m BFS\n\u001b[1;32m 6\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01mStrategies\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mDFS\u001b[39;00m \u001b[38;5;28;01mimport\u001b[39;00m DFS\n\u001b[1;32m 7\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01mStrategies\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mAStar\u001b[39;00m \u001b[38;5;28;01mimport\u001b[39;00m AStar\n",
"File \u001b[0;32m~/2026-rff_mp/pomelovsd/ExitMaze/Strategies/BFS.py:2\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mstrat\u001b[39;00m \u001b[38;5;28;01mimport\u001b[39;00m PathFindingStrategy\n\u001b[0;32m----> 2\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01mpath\u001b[39;00m \u001b[38;5;28;01mimport\u001b[39;00m restore\n\u001b[1;32m 3\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01mcollections\u001b[39;00m \u001b[38;5;28;01mimport\u001b[39;00m deque\n\u001b[1;32m 5\u001b[0m \u001b[38;5;28;01mclass\u001b[39;00m \u001b[38;5;21;01mBFS\u001b[39;00m(PathFindingStrategy):\n",
"\u001b[0;31mModuleNotFoundError\u001b[0m: No module named 'path'"
]
}
],
"source": [
"from Builder.Builder import TextFileMazeBuilder\n",
"from Core.Benchmark import RunBenchmark\n",
"from MazeSolver.Solver import MazeSolver\n",
"from Observer_Command.ConsoleView import ConsoleView\n",
"from Strategies.BFS import BFS\n",
"from Strategies.DFS import DFS\n",
"from Strategies.AStar import AStar"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "50c7010d",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"<class 'Builder.Builder.TextFileMazeBuilder'>\n"
]
}
],
"source": [
"print(TextFileMazeBuilder)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "6ed7f922",
"metadata": {},
"outputs": [
{
"ename": "ImportError",
"evalue": "cannot import name 'BFS' from 'Strategies.BFS' (/home/i4eker/2026-rff_mp/pomelovsd/ExitMaze/Strategies/BFS.py)",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mImportError\u001b[0m Traceback (most recent call last)",
"Cell \u001b[0;32mIn[13], line 3\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01mBuilder\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mBuilder\u001b[39;00m \u001b[38;5;28;01mimport\u001b[39;00m TextFileMazeBuilder\n\u001b[0;32m----> 3\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01mStrategies\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mBFS\u001b[39;00m \u001b[38;5;28;01mimport\u001b[39;00m BFS\n\u001b[1;32m 4\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01mStrategies\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mDFS\u001b[39;00m \u001b[38;5;28;01mimport\u001b[39;00m DFS\n\u001b[1;32m 5\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01mStrategies\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mAStar\u001b[39;00m \u001b[38;5;28;01mimport\u001b[39;00m AStar\n",
"\u001b[0;31mImportError\u001b[0m: cannot import name 'BFS' from 'Strategies.BFS' (/home/i4eker/2026-rff_mp/pomelovsd/ExitMaze/Strategies/BFS.py)"
]
}
],
"source": [
"\n",
"from Builder.Builder import TextFileMazeBuilder\n",
"\n",
"from Strategies.BFS import BFS\n",
"from Strategies.DFS import DFS\n",
"from Strategies.AStar import AStar\n",
"\n",
"from MazeSolver.Solver import MazeSolver\n",
"\n",
"\n",
"# ============================================\n",
"# ЗАГРУЗКА ЛАБИРИНТОВ\n",
"# ============================================\n",
"\n",
"builder = TextFileMazeBuilder()\n",
"\n",
"mazes = {\n",
" \"small\": builder.build_from_file(\"Mazes/small.txt\"),\n",
" \"medium\": builder.build_from_file(\"Mazes/medium.txt\"),\n",
" \"large\": builder.build_from_file(\"Mazes/large.txt\"),\n",
" \"empty\": builder.build_from_file(\"Mazes/empty.txt\"),\n",
" \"no_exit\": builder.build_from_file(\"Mazes/no_exit.txt\"),\n",
"}\n",
"\n",
"\n",
"# ============================================\n",
"# СТРАТЕГИИ\n",
"# ============================================\n",
"\n",
"strategies = {\n",
" \"BFS\": BFS(),\n",
" \"DFS\": DFS(),\n",
" \"A*\": AStar()\n",
"}\n",
"\n",
"\n",
"# ============================================\n",
"# РЕЗУЛЬТАТЫ\n",
"# ============================================\n",
"\n",
"results = []\n",
"\n",
"\n",
"# ============================================\n",
"# BENCHMARK\n",
"# ============================================\n",
"\n",
"REPEATS = 5\n",
"\n",
"for maze_name, maze in mazes.items():\n",
"\n",
" print(f\"\\n===== LABYRINTH: {maze_name} =====\")\n",
"\n",
" for strategy_name, strategy in strategies.items():\n",
"\n",
" total_time = 0\n",
" total_visited = 0\n",
" total_path = 0\n",
"\n",
" print(f\"Testing {strategy_name}...\")\n",
"\n",
" for _ in range(REPEATS):\n",
"\n",
" solver = MazeSolver(maze, strategy)\n",
"\n",
" start_time = time.perf_counter()\n",
"\n",
" stats, path = solver.solve()\n",
"\n",
" end_time = time.perf_counter()\n",
"\n",
" elapsed_ms = (end_time - start_time) * 1000\n",
"\n",
" total_time += elapsed_ms\n",
" total_visited += stats.visited_cells\n",
" total_path += stats.path_length\n",
"\n",
" avg_time = total_time / REPEATS\n",
" avg_visited = total_visited / REPEATS\n",
" avg_path = total_path / REPEATS\n",
"\n",
" results.append({\n",
" \"maze\": maze_name,\n",
" \"strategy\": strategy_name,\n",
" \"time_ms\": round(avg_time, 3),\n",
" \"visited_cells\": int(avg_visited),\n",
" \"path_length\": int(avg_path)\n",
" })\n",
"\n",
" print(\n",
" f\"{strategy_name}: \"\n",
" f\"time={avg_time:.3f} ms | \"\n",
" f\"visited={avg_visited:.0f} | \"\n",
" f\"path={avg_path:.0f}\"\n",
" )\n",
"\n",
"\n",
"# ============================================\n",
"# СОХРАНЕНИЕ CSV\n",
"# ============================================\n",
"\n",
"csv_file = \"benchmark_results.csv\"\n",
"\n",
"with open(csv_file, \"w\", newline=\"\", encoding=\"utf-8\") as file:\n",
"\n",
" writer = csv.writer(file)\n",
"\n",
" writer.writerow([\n",
" \"maze\",\n",
" \"strategy\",\n",
" \"time_ms\",\n",
" \"visited_cells\",\n",
" \"path_length\"\n",
" ])\n",
"\n",
" for row in results:\n",
" writer.writerow([\n",
" row[\"maze\"],\n",
" row[\"strategy\"],\n",
" row[\"time_ms\"],\n",
" row[\"visited_cells\"],\n",
" row[\"path_length\"]\n",
" ])\n",
"\n",
"print(f\"\\nCSV saved: {csv_file}\")\n",
"\n",
"\n",
"# ============================================\n",
"# ВЫВОД ТАБЛИЦЫ\n",
"# ============================================\n",
"\n",
"print(\"\\nRESULTS:\\n\")\n",
"\n",
"for row in results:\n",
"\n",
" print(\n",
" f\"{row['maze']:10} | \"\n",
" f\"{row['strategy']:5} | \"\n",
" f\"{row['time_ms']:10} ms | \"\n",
" f\"{row['visited_cells']:10} visited | \"\n",
" f\"{row['path_length']:5} path\"\n",
" )\n",
"\n",
"\n",
"# ============================================\n",
"# ГРАФИК ВРЕМЕНИ\n",
"# ============================================\n",
"\n",
"for maze_name in mazes.keys():\n",
"\n",
" strategy_names = []\n",
" times = []\n",
"\n",
" for row in results:\n",
"\n",
" if row[\"maze\"] == maze_name:\n",
"\n",
" strategy_names.append(row[\"strategy\"])\n",
" times.append(row[\"time_ms\"])\n",
"\n",
" plt.figure(figsize=(8, 5))\n",
"\n",
" plt.bar(strategy_names, times)\n",
"\n",
" plt.title(f\"Execution Time — {maze_name}\")\n",
" plt.xlabel(\"Strategy\")\n",
" plt.ylabel(\"Time (ms)\")\n",
"\n",
" plt.show()\n",
"\n",
"\n",
"# ============================================\n",
"# ГРАФИК ПОСЕЩЁННЫХ КЛЕТОК\n",
"# ============================================\n",
"\n",
"for maze_name in mazes.keys():\n",
"\n",
" strategy_names = []\n",
" visited = []\n",
"\n",
" for row in results:\n",
"\n",
" if row[\"maze\"] == maze_name:\n",
"\n",
" strategy_names.append(row[\"strategy\"])\n",
" visited.append(row[\"visited_cells\"])\n",
"\n",
" plt.figure(figsize=(8, 5))\n",
"\n",
" plt.bar(strategy_names, visited)\n",
"\n",
" plt.title(f\"Visited Cells — {maze_name}\")\n",
" plt.xlabel(\"Strategy\")\n",
" plt.ylabel(\"Visited Cells\")\n",
"\n",
" plt.show()\n",
"\n",
"\n",
"# ============================================\n",
"# ГРАФИК ДЛИНЫ ПУТИ\n",
"# ============================================\n",
"\n",
"for maze_name in mazes.keys():\n",
"\n",
" strategy_names = []\n",
" path_lengths = []\n",
"\n",
" for row in results:\n",
"\n",
" if row[\"maze\"] == maze_name:\n",
"\n",
" strategy_names.append(row[\"strategy\"])\n",
" path_lengths.append(row[\"path_length\"])\n",
"\n",
" plt.figure(figsize=(8, 5))\n",
"\n",
" plt.bar(strategy_names, path_lengths)\n",
"\n",
" plt.title(f\"Path Length — {maze_name}\")\n",
" plt.xlabel(\"Strategy\")\n",
" plt.ylabel(\"Path Length\")\n",
"\n",
" plt.show()\n",
"\n",
"\n",
"print(\"\\nBenchmark completed.\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "b001bee8",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"id": "083b0af8",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"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.12.3"
}
},
"nbformat": 4,
"nbformat_minor": 5
}