-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
78 lines (61 loc) · 1.99 KB
/
main.py
File metadata and controls
78 lines (61 loc) · 1.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
"""
main.py
This is the main executable script for the AI Search Toolkit.
It uses argparse to parse command-line arguments and runs the
specified search algorithm on the given maze file.
Example Usage:
python main.py data/maze_medium.txt -a a_star
"""
import argparse
import time
from src.maze_problem import MazeProblem, manhattanHeuristic
import src.search as search
def main():
"""
Parses command-line arguments and executes the search.
"""
parser = argparse.ArgumentParser(
description="Solve a maze file using AI search algorithms."
)
parser.add_argument(
'maze_file',
type=str,
help="Path to the .txt maze file (e.g., data/maze_medium.txt)"
)
parser.add_argument(
'-a', '--algorithm',
type=str,
default='dfs',
choices=['dfs', 'bfs', 'ucs', 'a_star'],
help="The search algorithm to use (default: dfs)"
)
args = parser.parse_args()
print(f"Loading maze from: {args.maze_file}")
problem = MazeProblem(args.maze_file)
print(f"Solving using {args.algorithm.upper()}...")
# Select algorithm
alg_map = {
'dfs': search.depthFirstSearch,
'bfs': search.breadthFirstSearch,
'ucs': search.uniformCostSearch,
'a_star': lambda p: search.aStarSearch(p, heuristic=manhattanHeuristic)
}
algorithm = alg_map.get(args.algorithm)
if algorithm is None:
print(f"Error: Unknown algorithm '{args.algorithm}'")
return
start_time = time.time()
solution = algorithm(problem)
end_time = time.time()
# Print results
print("-" * 30)
if solution is not None:
print("🎉 Solution Found! 🎉")
print(f"Path: {' -> '.join(solution)}")
print(f"Path Length: {len(solution)} steps")
else:
print("💔 No solution found.")
print(f"Time elapsed: {end_time - start_time:.6f} seconds")
print("-" * 30)
if __name__ == "__main__":
main()