Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
201 changes: 201 additions & 0 deletions AI ass-3.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,201 @@
{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"provenance": [],
"authorship_tag": "ABX9TyMcrNjIsxJ1Dc0dKOWWt71n",
"include_colab_link": true
},
"kernelspec": {
"name": "python3",
"display_name": "Python 3"
},
"language_info": {
"name": "python"
}
},
"cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "view-in-github",
"colab_type": "text"
},
"source": [
"<a href=\"https://colab.research.google.com/github/shahbazkhan1887035/MLH-Code-Collaborate-GitHub/blob/main/AI%20ass-3.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
]
},
{
"cell_type": "code",
"source": [
"import heapq\n",
"import math\n",
"import random\n",
"\n",
"import subprocess\n",
"import sys\n",
"import os\n",
"\n",
"# Define required dependencies here\n",
"required_libraries = ['numpy', 'pygame'] # Example libraries\n",
"\n",
"def install_dependencies():\n",
" for lib in required_libraries:\n",
" try:\n",
" # Check if the library is installed\n",
" __import__(lib)\n",
" except ImportError:\n",
" # Install missing libraries using pip\n",
" subprocess.check_call([sys.executable, '-m', 'pip', 'install', lib])\n",
"\n",
"def run_maze_generator():\n",
" # Run the main executable here\n",
" os.system('python main.py') # Change this to your actual command to run the maze generator\n",
"\n",
"def main():\n",
" print(\"Checking and installing dependencies...\")\n",
" install_dependencies()\n",
" print(\"Dependencies installed successfully.\")\n",
" print(\"Running the maze generator...\")\n",
" run_maze_generator()\n",
"\n",
"if __name__ == \"__main__\":\n",
" main()\n",
"\n",
"def create_maze(rows, cols):\n",
" maze = [[\"#\" for _ in range(cols)] for _ in range(rows)]\n",
"\n",
" def is_valid(x, y):\n",
" return 0 <= x < rows and 0 <= y < cols and maze[x][y] == \"#\"\n",
"\n",
" def generate(x, y):\n",
" directions = [(2, 0), (-2, 0), (0, 2), (0, -2)]\n",
" random.shuffle(directions)\n",
"\n",
" for dx, dy in directions:\n",
" nx, ny = x + dx, y + dy\n",
" if is_valid(nx, ny):\n",
" maze[nx][ny] = \".\"\n",
" maze[x + dx // 2][y + dy // 2] = \".\"\n",
" generate(nx, ny)\n",
"\n",
" start_x, start_y = 0, 0 # Define the start point\n",
" maze[start_x][start_y] = \"S\"\n",
"\n",
" goal_x, goal_y = rows - 1, cols - 1 # Define the goal point\n",
" maze[goal_x][goal_y] = \"G\"\n",
"\n",
" generate(start_x, start_y)\n",
"\n",
" return maze, (start_x, start_y), (goal_x, goal_y)\n",
"\n",
"def display_maze(maze):\n",
" for row in maze:\n",
" print(\" \".join(row))\n",
"\n",
"def heuristic(a, b):\n",
" return math.sqrt((a[0] - b[0])**2 + (a[1] - b[1])**2)\n",
"\n",
"def astar(maze, start, goal):\n",
" rows = len(maze)\n",
" cols = len(maze[0])\n",
" open_set = []\n",
" heapq.heappush(open_set, (0, start))\n",
" came_from = {}\n",
" g_score = {pos: float(\"inf\") for pos in [(i, j) for i in range(rows) for j in range(cols)]}\n",
" g_score[start] = 0\n",
" f_score = {pos: float(\"inf\") for pos in [(i, j) for i in range(rows) for j in range(cols)]}\n",
" f_score[start] = heuristic(start, goal)\n",
"\n",
" while open_set:\n",
" current = heapq.heappop(open_set)[1]\n",
" if current == goal:\n",
" path = []\n",
" while current in came_from:\n",
" path.append(current)\n",
" current = came_from[current]\n",
" return path\n",
"\n",
" for dx, dy in [(0, 1), (0, -1), (1, 0), (-1, 0)]:\n",
" nx, ny = current[0] + dx, current[1] + dy\n",
" if 0 <= nx < rows and 0 <= ny < cols and maze[nx][ny] in (\".\", \"G\"):\n",
" tentative_g_score = g_score[current] + 1\n",
" if tentative_g_score < g_score[(nx, ny)]:\n",
" came_from[(nx, ny)] = current\n",
" g_score[(nx, ny)] = tentative_g_score\n",
" f_score[(nx, ny)] = tentative_g_score + heuristic((nx, ny), goal)\n",
" if (f_score[(nx, ny)], (nx, ny)) not in open_set:\n",
" heapq.heappush(open_set, (f_score[(nx, ny)], (nx, ny)))\n",
"\n",
" return None\n",
"\n",
"def mark_path(maze, path):\n",
" for x, y in path:\n",
" if maze[x][y] != \"S\" and maze[x][y] != \"G\":\n",
" maze[x][y] = \"*\"\n",
"\n",
"def main():\n",
" print(\"Enter the size of the maze (rows and columns as two numbers separated by a space):\")\n",
" size_input = input(\"Enter rows and columns (e.g., '5 7'): \")\n",
"\n",
" try:\n",
" rows, cols = map(int, size_input.split())\n",
" except ValueError:\n",
" print(\"Please enter two numbers separated by a space.\")\n",
" return\n",
"\n",
" if rows % 2 == 0 or cols % 2 == 0:\n",
" print(\"Please enter odd numbers for rows and columns.\")\n",
" return\n",
"\n",
" maze, start, goal = create_maze(rows, cols)\n",
"\n",
" print(\"\\nMaze:\")\n",
" display_maze(maze)\n",
"\n",
" path = astar(maze, start, goal)\n",
"\n",
" if path:\n",
" mark_path(maze, path)\n",
" print(\"\\nPath from start to goal:\")\n",
" display_maze(maze)\n",
" else:\n",
" print(\"\\nNo path found from start to goal.\")\n",
"\n",
"if __name__ == \"__main__\":\n",
" main()"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "z2Uo2rWZoQpY",
"outputId": "c7800178-1101-450b-942e-c3a047c8d315"
},
"execution_count": 2,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"Checking and installing dependencies...\n",
"pygame 2.5.2 (SDL 2.28.2, Python 3.10.12)\n",
"Hello from the pygame community. https://www.pygame.org/contribute.html\n",
"Dependencies installed successfully.\n",
"Running the maze generator...\n",
"Enter the size of the maze (rows and columns as two numbers separated by a space):\n",
"Enter rows and columns (e.g., '5 7'): 3 7\n",
"\n",
"Maze:\n",
"S # . . . . .\n",
". # . # . # #\n",
". . . # . # G\n",
"\n",
"No path found from start to goal.\n"
]
}
]
}
]
}
163 changes: 163 additions & 0 deletions Untitled0.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"provenance": [],
"authorship_tag": "ABX9TyOCMfqPqf+gmy5XRWQTNWK7",
"include_colab_link": true
},
"kernelspec": {
"name": "python3",
"display_name": "Python 3"
},
"language_info": {
"name": "python"
}
},
"cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "view-in-github",
"colab_type": "text"
},
"source": [
"<a href=\"https://colab.research.google.com/github/shahbazkhan1887035/MLH-Code-Collaborate-GitHub/blob/main/Untitled0.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
]
},
{
"cell_type": "code",
"source": [
"import heapq\n",
"import math\n",
"\n",
"def create_maze(rows, cols):\n",
" maze = [[\"#\" for _ in range(cols)] for _ in range(rows)]\n",
"\n",
" def is_valid(x, y):\n",
" return 0 <= x < rows and 0 <= y < cols and maze[x][y] == \"#\"\n",
"\n",
" def generate(x, y):\n",
" directions = [(2, 0), (-2, 0), (0, 2), (0, -2)]\n",
" random.shuffle(directions)\n",
"\n",
" for dx, dy in directions:\n",
" nx, ny = x + dx, y + dy\n",
" if is_valid(nx, ny):\n",
" maze[nx][ny] = \".\"\n",
" maze[x + dx // 2][y + dy // 2] = \".\"\n",
" generate(nx, ny)\n",
"\n",
" start_x, start_y = 0, 0 # Define the start point\n",
" maze[start_x][start_y] = \"S\"\n",
"\n",
" goal_x, goal_y = rows - 1, cols - 1 # Define the goal point\n",
" maze[goal_x][goal_y] = \"G\"\n",
"\n",
" generate(start_x, start_y)\n",
"\n",
" return maze, (start_x, start_y), (goal_x, goal_y)\n",
"\n",
"def display_maze(maze):\n",
" for row in maze:\n",
" print(\" \".join(row))\n",
"\n",
"def heuristic(a, b):\n",
" return math.sqrt((a[0] - b[0])**2 + (a[1] - b[1])**2)\n",
"\n",
"def astar(maze, start, goal):\n",
" rows = len(maze)\n",
" cols = len(maze[0])\n",
" open_set = []\n",
" heapq.heappush(open_set, (0, start))\n",
" came_from = {}\n",
" g_score = {pos: float(\"inf\") for pos in [(i, j) for i in range(rows) for j in range(cols)]}\n",
" g_score[start] = 0\n",
" f_score = {pos: float(\"inf\") for pos in [(i, j) for i in range(rows) for j in range(cols)]}\n",
" f_score[start] = heuristic(start, goal)\n",
"\n",
" while open_set:\n",
" current = heapq.heappop(open_set)[1]\n",
" if current == goal:\n",
" path = []\n",
" while current in came_from:\n",
" path.append(current)\n",
" current = came_from[current]\n",
" return path\n",
"\n",
" for dx, dy in [(0, 1), (0, -1), (1, 0), (-1, 0)]:\n",
" nx, ny = current[0] + dx, current[1] + dy\n",
" if 0 <= nx < rows and 0 <= ny < cols and maze[nx][ny] in (\".\", \"G\"):\n",
" tentative_g_score = g_score[current] + 1\n",
" if tentative_g_score < g_score[(nx, ny)]:\n",
" came_from[(nx, ny)] = current\n",
" g_score[(nx, ny)] = tentative_g_score\n",
" f_score[(nx, ny)] = tentative_g_score + heuristic((nx, ny), goal)\n",
" if (f_score[(nx, ny)], (nx, ny)) not in open_set:\n",
" heapq.heappush(open_set, (f_score[(nx, ny)], (nx, ny)))\n",
"\n",
" return None\n",
"\n",
"def mark_path(maze, path):\n",
" for x, y in path:\n",
" if maze[x][y] != \"S\" and maze[x][y] != \"G\":\n",
" maze[x][y] = \"*\"\n",
"\n",
"def main():\n",
" print(\"Enter the size of the maze (rows and columns should be odd numbers):\")\n",
" rows = int(input(\"Number of rows: \"))\n",
" cols = int(input(\"Number of columns: \"))\n",
"\n",
" if rows % 2 == 0 or cols % 2 == 0:\n",
" print(\"Please enter odd numbers for rows and columns.\")\n",
" return\n",
"\n",
" maze, start, goal = create_maze(rows, cols)\n",
"\n",
" print(\"\\nMaze:\")\n",
" display_maze(maze)\n",
"\n",
" path = astar(maze, start, goal)\n",
"\n",
" if path:\n",
" mark_path(maze, path)\n",
" print(\"\\nPath from start to goal:\")\n",
" display_maze(maze)\n",
" else:\n",
" print(\"\\nNo path found from start to goal.\")\n",
"\n",
"if __name__ == \"__main__\":\n",
" main()"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "vxPKfT_2gQyP",
"outputId": "1e02fabb-458f-4a13-c80c-78b175a0f6ed"
},
"execution_count": 5,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"Enter the size of the maze (rows and columns should be odd numbers):\n",
"Number of rows: 5\n",
"Number of columns: 5\n",
"\n",
"Maze:\n",
"S . . # .\n",
"# # . # .\n",
". # . . .\n",
". # . # #\n",
". . . # G\n",
"\n",
"No path found from start to goal.\n"
]
}
]
}
]
}
Loading