-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfind_route.py
106 lines (90 loc) · 2.98 KB
/
find_route.py
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
import sys
#from Queue import Queue
from queue import PriorityQueue
from heapq import heappush, heappop
import time
start_time = time.time()
#as PriorityQueue is not present in python 2.4 so, a class was created
# class PriorityQueue(Queue):
# def _init(self, maxsize):
# self.maxsize = maxsize
# self.queue = []
# def _put(self, item):
# return heappush(self.queue, item)
# def _get(self):
# return heappop(self.queue)
#from queue import PriorityQueue
def create_graph(filename):
graph = {}
file = open(filename, 'r')
for line in file:
if 'END OF INPUT' in line:
return graph
nodeA, nodeB, d = line.split()
graph.setdefault(nodeA, []).append((nodeB, d))
graph.setdefault(nodeB, []).append((nodeA, d))
def uniformed_cost_search(graph, start, goal):
visited = set() #keeps track of all visited nodes
path = [] #stores the path for each iteration
queue = PriorityQueue() #stores and sorts all neighbours
queue.put((0, [start]))
while queue:
#if there is no path between two nodes
if queue.empty():
print ('distance: infinity\nroute:\nnone')
print(f"--- {time.time() - start_time} seconds ---")
return
cost, path = queue.get()
node = path[len(path)-1]
if node not in visited:
visited.add(node)
if node == goal:
path.append(cost) #the shortest path is found
return path
for x in neighbors(graph, node):
if x not in visited:
total_cost = cost + int(get_cost(graph, node, x))
temporary = path[:]
temporary.append(x)
queue.put((total_cost, temporary)) #creating queue such that it has all the values for path
def neighbors(graph,node):
#finding neighbors in graph
elements = graph[node]
return [x[0] for x in elements]
def get_cost(graph, from_node, to_node):
#calc. cost of each edge
position = [x[0] for x in graph[from_node]].index(to_node)
return graph[from_node][position][1]
def display_path(graph,path):
#display in proper format
distance = path[-1]
print(f'distance: {distance}')
print ('route: ')
for x in path[:-2]:
y = path.index(x)
position = [z[0] for z in graph[x]].index(path[y+1])
cost = graph[x][position][1]
print(f'{x} to {path[y + 1]}, {cost} km')
print(f"--- {time.time() - start_time} seconds ---")
def main():
#reading all arguments
filename = sys.argv[1]
start = sys.argv[2]
goal = sys.argv[3]
#create Graph (from input file)
graph = {}
graph = create_graph(filename)
print(graph)
#checking for exceptions
if start not in graph.keys():
print ('Improper start node')
sys.exit()
if goal not in graph.keys():
print ('Improper goal node')
sys.exit()
#finding path and cost using Uniformed Cost Search
path = []
if path := uniformed_cost_search(graph, start, goal):
display_path(graph,path)
if __name__ == '__main__':
main()