Skip to content

Commit 6c1242b

Browse files
Merge pull request #2520 from Shikhar9425/patch-4
Maze.py
2 parents 5a280d6 + 24646e5 commit 6c1242b

File tree

2 files changed

+131
-0
lines changed

2 files changed

+131
-0
lines changed

AI-based AI Maze Solver/README.md

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
Package/Script Name: AI-based AI Maze Solver
2+
3+
Short Description: AI-based AI Maze Solver is a Python script that implements an AI algorithm to solve a maze automatically. The AI uses the A* (A-Star) search algorithm with a heuristic function to efficiently navigate through the maze and find the shortest path to the goal.
4+
5+
Functionalities/Script:
6+
7+
MazeSolver class: Represents the AI-based maze solver.
8+
A* algorithm: Implements the A* search algorithm with a heuristic function to find the shortest path.
9+
Heuristic function: Calculates the estimated cost (heuristic) from a cell to the goal to guide the A* search.
10+
Setup Instructions:
11+
12+
Make sure you have Python installed on your system (Python 3.6 or higher).
13+
Download the AI_maze_solver.py file from the repository or package.
14+
Explanation of Script:
15+
The AI-based AI Maze Solver script enables you to solve mazes automatically using the A* search algorithm. The A* algorithm combines the cost to reach a cell (path cost) with an estimated cost to the goal (heuristic) to efficiently explore the maze and find the shortest path.
16+
17+
Usage:
18+
19+
Create a maze representation as a 2D array, where 0 represents empty cells, 1 represents obstacles/walls, and 2 represents the starting position. For example:
20+
python
21+
Copy code
22+
maze = [
23+
[1, 1, 0, 0, 0],
24+
[0, 1, 1, 0, 1],
25+
[0, 0, 0, 0, 1],
26+
[1, 1, 0, 1, 0],
27+
[2, 0, 0, 1, 1],
28+
]
29+
Initialize the MazeSolver class with the maze.
30+
python
31+
Copy code
32+
from AI_maze_solver import MazeSolver
33+
34+
maze_solver = MazeSolver(maze)
35+
Use the find_path() method to get the shortest path from the starting position to the goal.
36+
python
37+
Copy code
38+
path = maze_solver.find_path()
39+
print("Shortest Path:", path)
40+
Output:
41+
The AI-based AI Maze Solver script will output the shortest path from the starting position to the goal in the maze.
42+
43+
Author:
44+
Shikhar9425

AI-based AI Maze Solver/maze.py

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
import heapq
2+
import random
3+
4+
class MazeSolver:
5+
def __init__(self, maze):
6+
self.maze = maze
7+
self.rows = len(maze)
8+
self.cols = len(maze[0])
9+
self.start = (0, 0)
10+
self.goal = (self.rows - 1, self.cols - 1)
11+
12+
def get_neighbors(self, node):
13+
row, col = node
14+
neighbors = []
15+
if row > 0 and not self.maze[row - 1][col]:
16+
neighbors.append((row - 1, col))
17+
if row < self.rows - 1 and not self.maze[row + 1][col]:
18+
neighbors.append((row + 1, col))
19+
if col > 0 and not self.maze[row][col - 1]:
20+
neighbors.append((row, col - 1))
21+
if col < self.cols - 1 and not self.maze[row][col + 1]:
22+
neighbors.append((row, col + 1))
23+
return neighbors
24+
25+
def heuristic(self, node):
26+
return abs(node[0] - self.goal[0]) + abs(node[1] - self.goal[1])
27+
28+
def solve(self):
29+
open_set = []
30+
closed_set = set()
31+
g_score = {self.start: 0}
32+
f_score = {self.start: self.heuristic(self.start)}
33+
heapq.heappush(open_set, (f_score[self.start], self.start))
34+
35+
while open_set:
36+
_, current = heapq.heappop(open_set)
37+
38+
if current == self.goal:
39+
path = []
40+
while current in g_score:
41+
path.append(current)
42+
current = g_score[current]
43+
return path[::-1]
44+
45+
closed_set.add(current)
46+
47+
for neighbor in self.get_neighbors(current):
48+
if neighbor in closed_set:
49+
continue
50+
tentative_g_score = g_score[current] + 1
51+
52+
if neighbor not in g_score or tentative_g_score < g_score[neighbor]:
53+
g_score[neighbor] = tentative_g_score
54+
f_score[neighbor] = tentative_g_score + self.heuristic(neighbor)
55+
heapq.heappush(open_set, (f_score[neighbor], neighbor))
56+
57+
return None
58+
59+
def generate_random_maze(rows, cols, obstacle_probability):
60+
return [[random.random() < obstacle_probability for _ in range(cols)] for _ in range(rows)]
61+
62+
def print_maze(maze):
63+
for row in maze:
64+
print("".join(["#" if cell else " " for cell in row]))
65+
66+
def main():
67+
rows = 10
68+
cols = 10
69+
obstacle_probability = 0.2
70+
71+
maze = generate_random_maze(rows, cols, obstacle_probability)
72+
print("Random Maze:")
73+
print_maze(maze)
74+
75+
maze_solver = MazeSolver(maze)
76+
path = maze_solver.solve()
77+
78+
if path:
79+
print("\nSolution Path:")
80+
for node in path:
81+
maze[node[0]][node[1]] = True # Marking the path in the maze
82+
print_maze(maze)
83+
else:
84+
print("\nNo path found!")
85+
86+
if __name__ == "__main__":
87+
main()

0 commit comments

Comments
 (0)