-
Notifications
You must be signed in to change notification settings - Fork 8
/
astarRomania.py
126 lines (93 loc) · 2.96 KB
/
astarRomania.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
#
# Created by PyCharm.
# User: Rizky Andre Wibisono
# Date: 17/03/2019
# Time: 20:59
#
import heapq
class priorityQueue:
def __init__(self):
self.cities = []
def push(self, city, cost):
heapq.heappush(self.cities, (cost, city))
def pop(self):
return heapq.heappop(self.cities)[1]
def isEmpty(self):
if (self.cities == []):
return True
else:
return False
def check(self):
print(self.cities)
class ctNode:
def __init__(self, city, distance):
self.city = str(city)
self.distance = str(distance)
romania = {}
def makedict():
file = open("romania.txt", 'r')
for string in file:
line = string.split(',')
ct1 = line[0]
ct2 = line[1]
dist = int(line[2])
romania.setdefault(ct1, []).append(ctNode(ct2, dist))
romania.setdefault(ct2, []).append(ctNode(ct1, dist))
def makehuristikdict():
h = {}
with open("romania_sld.txt", 'r') as file:
for line in file:
line = line.strip().split(",")
node = line[0].strip()
sld = int(line[1].strip())
h[node] = sld
return h
def heuristic(node, values):
return values[node]
def astar(start, end):
path = {}
distance = {}
q = priorityQueue()
h = makehuristikdict()
q.push(start, 0)
distance[start] = 0
path[start] = None
expandedList = []
while (q.isEmpty() == False):
current = q.pop()
expandedList.append(current)
if (current == end):
break
for new in romania[current]:
g_cost = distance[current] + int(new.distance)
# print(new.city, new.distance, "now : " + str(distance[current]), g_cost)
if (new.city not in distance or g_cost < distance[new.city]):
distance[new.city] = g_cost
f_cost = g_cost + heuristic(new.city, h)
q.push(new.city, f_cost)
path[new.city] = current
printoutput(start, end, path, distance, expandedList)
def printoutput(start, end, path, distance, expandedlist):
finalpath = []
i = end
while (path.get(i) != None):
finalpath.append(i)
i = path[i]
finalpath.append(start)
finalpath.reverse()
print("Program algoritma Astar untuk masalah Romania")
print("\tArad => Bucharest")
print("=======================================================")
print("Kota yg mungkin dijelajah \t\t: " + str(expandedlist))
print("Jumlah kemungkinan kota \t\t: " + str(len(expandedlist)))
print("=======================================================")
print("Kota yg dilewati dg jarak terpendek\t: " + str(finalpath))
print("Jumlah kota yang dilewati \t\t\t: " + str(len(finalpath)))
print("Total jarak \t\t\t\t\t\t: " + str(distance[end]))
def main():
src = "Arad"
dst = "Bucharest"
makedict()
astar(src, dst)
if __name__ == "__main__":
main()