-
Notifications
You must be signed in to change notification settings - Fork 8
/
romania.py
178 lines (126 loc) · 4.58 KB
/
romania.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
# Mahdi Hassanzadeh
import queue
import matplotlib.pyplot as plt
# getting heuristics from file
def getHeuristics():
heuristics = {}
f = open("heuristics.txt")
for i in f.readlines():
node_heuristic_val = i.split()
heuristics[node_heuristic_val[0]] = int(node_heuristic_val[1])
return heuristics
# getting cities location from file
def getCity():
city = {}
citiesCode = {}
f = open("cities.txt")
j = 1
for i in f.readlines():
node_city_val = i.split()
city[node_city_val[0]] = [int(node_city_val[1]), int(node_city_val[2])]
citiesCode[j] = node_city_val[0]
j += 1
return city, citiesCode
# creating cities graph from file
def createGraph():
graph = {}
file = open("citiesGraph.txt")
for i in file.readlines():
node_val = i.split()
if node_val[0] in graph and node_val[1] in graph:
c = graph.get(node_val[0])
c.append([node_val[1], node_val[2]])
graph.update({node_val[0]: c})
c = graph.get(node_val[1])
c.append([node_val[0], node_val[2]])
graph.update({node_val[1]: c})
elif node_val[0] in graph:
c = graph.get(node_val[0])
c.append([node_val[1], node_val[2]])
graph.update({node_val[0]: c})
graph[node_val[1]] = [[node_val[0], node_val[2]]]
elif node_val[1] in graph:
c = graph.get(node_val[1])
c.append([node_val[0], node_val[2]])
graph.update({node_val[1]: c})
graph[node_val[0]] = [[node_val[1], node_val[2]]]
else:
graph[node_val[0]] = [[node_val[1], node_val[2]]]
graph[node_val[1]] = [[node_val[0], node_val[2]]]
return graph
# Greedy Best First Search Algorithm
def GBFS(startNode, heuristics, graph, goalNode="Bucharest"):
priorityQueue = queue.PriorityQueue()
priorityQueue.put((heuristics[startNode], startNode))
path = []
while priorityQueue.empty() == False:
current = priorityQueue.get()[1]
path.append(current)
if current == goalNode:
break
priorityQueue = queue.PriorityQueue()
for i in graph[current]:
if i[0] not in path:
priorityQueue.put((heuristics[i[0]], i[0]))
return path
# Astar Algorithm
def Astar(startNode, heuristics, graph, goalNode="Bucharest"):
priorityQueue = queue.PriorityQueue()
distance = 0
path = []
priorityQueue.put((heuristics[startNode] + distance, [startNode, 0]))
while priorityQueue.empty() == False:
current = priorityQueue.get()[1]
path.append(current[0])
distance += int(current[1])
if current[0] == goalNode:
break
priorityQueue = queue.PriorityQueue()
for i in graph[current[0]]:
if i[0] not in path:
priorityQueue.put((heuristics[i[0]] + int(i[1]) + distance, i))
return path
# drawing map of answer
def drawMap(city, gbfs, astar, graph):
for i, j in city.items():
plt.plot(j[0], j[1], "ro")
plt.annotate(i, (j[0] + 5, j[1]))
for k in graph[i]:
n = city[k[0]]
plt.plot([j[0], n[0]], [j[1], n[1]], "gray")
for i in range(len(gbfs)):
try:
first = city[gbfs[i]]
secend = city[gbfs[i + 1]]
plt.plot([first[0], secend[0]], [first[1], secend[1]], "green")
except:
continue
for i in range(len(astar)):
try:
first = city[astar[i]]
secend = city[astar[i + 1]]
plt.plot([first[0], secend[0]], [first[1], secend[1]], "blue")
except:
continue
plt.errorbar(1, 1, label="GBFS", color="green")
plt.errorbar(1, 1, label="ASTAR", color="blue")
plt.legend(loc="lower left")
plt.show()
# running the program
def main():
heuristic = getHeuristics()
graph = createGraph()
city, citiesCode = getCity()
for i, j in citiesCode.items():
print(i, j)
while True:
inputCode = int(input("Please enter your desired city's number (0 for exit): "))
if inputCode == 0:
break
cityName = citiesCode[inputCode]
gbfs = GBFS(cityName, heuristic, graph)
astar = Astar(cityName, heuristic, graph)
print("GBFS => ", gbfs)
print("ASTAR => ", astar)
drawMap(city, gbfs, astar, graph)
main()