-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsearch.py
More file actions
90 lines (64 loc) · 2.45 KB
/
search.py
File metadata and controls
90 lines (64 loc) · 2.45 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
79
80
81
82
83
84
85
86
87
88
import sys
from utils.graph import Graph
from methods.dfs import DFS
from methods.bfs import BFS
from methods.gbfs import GBFS
from methods.cus1 import CUS1
from methods.Astar import ASTAR
from methods.cus2 import CUS2
def main():
if len(sys.argv) != 3: # The number of arguments includes script, file and search method
print("Usage: python search.py <graph_file> <search_method>")
sys.exit(1)
filename = sys.argv[1]
method = sys.argv[2].upper()
#load from input file
graph = Graph()
graph.load_file(filename)
#clearing redundancy in dfs,bfs,gbfs printing
search_methods_1 = {
"DFS": DFS,
"BFS": BFS,
"GBFS": GBFS,
}
if method in search_methods_1:
search_method = search_methods_1[method]()
path, visited = search_method.search(graph, graph.origin, graph.destination)
print(f"{filename} {method}")
if path is None:
print("No path found.")
else:
goal_node = path[-1]
print("Visited nodes:", visited,"FOR DEBUGGING") # Debugging
print(f"Destination:{goal_node} Nodes Visited: {len(visited)}")
print("Path:"," ".join(str(n) for n in path))
elif method == "ASTAR":
search_method= ASTAR()
path, cost = search_method.search(graph, graph.origin, graph.destination)
print(f"{filename} {method}")
goal_node = path[-1]
print("GoalNode: "f"{goal_node}", "Length: "f"{len(path)}")
print(" ".join(str(n) for n in path))
elif method == "CUS1":
search_method= CUS1()
path, visited, cost = search_method.search(graph, graph.origin, graph.destination)
print(f"{filename} {method}")
if path is None:
print("No path found.")
else:
goal_node = path[-1]
print("Visited nodes:", visited) # debugging
print("GoalNode: "f"{goal_node}", "Length: "f"{len(path)}")
print(" ".join(str(n) for n in path))
elif method == "CUS2":
search_method = CUS2()
path, cost = search_method.search(graph, graph.origin, graph.destination)
print(f"{filename} {method}")
goal_node = path[-1]
print("GoalNode: "f"{goal_node}", "Length: "f"{len(path)}")
print(" ".join(str(n) for n in path))
else:
print(f"Invalid search method: {method}")
print("Try: DFS, BFS, GBFS, ASTAR, CUS1, CUS2")
if __name__ == '__main__':
main()