-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.py
152 lines (116 loc) · 4.16 KB
/
test.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
import sys
import math
class node(object):
def __init__(self,input, heuristic, city,g = 0,f = 0,d =0,parent = None):
self.city = city
self.g = g
self.f = f
self.d = d
self.parent = parent
class FindRoute(object) :
def __init__( self, inputFile = None, originCity = None, destinationCity = None, heuristicFile = None) :
self.informedSearch = False
self.inputData = {}
self.heuristicData = {}
self.start = originCity
self.goal = destinationCity
if inputFile is not None :
self.parseInputData( inputFile )
if heuristicFile is not None :
self.informedSearch = True
self.parseHeuristicData( heuristicFile )
def parseInputData( self, inputFile ) :
try:
fp = open(inputFile, 'r')
lines = fp.read().replace('\r', '' ).split( '\n' )
for line in lines:
if line != 'END OF INPUT':
line_arr = line.strip().split(' ')
if line_arr[0].lower() in self.inputData:
self.inputData[line_arr[0].lower()].append((line_arr[1].lower(), int(line_arr[2])))
else:
self.inputData[line_arr[0].lower()] = []
self.inputData[line_arr[0].lower()].append((line_arr[1].lower(), int(line_arr[2])))
if line_arr[1].lower() in self.inputData:
self.inputData[line_arr[1].lower()].append((line_arr[0].lower(), int(line_arr[2])))
else:
self.inputData[line_arr[1].lower()] = []
self.inputData[line_arr[1].lower()].append((line_arr[0].lower(), int(line_arr[2])))
finally:
fp.close()
def parseHeuristicData( self, inputFile ) :
try:
fp = open(inputFile, 'r')
lines = fp.read().replace('\r', '' ).split( '\n' )
for line in lines:
if line != 'END OF INPUT':
line_arr = line.strip().split(' ')
self.heuristicData[line_arr[0].lower()] = int(line_arr[1])
finally:
fp.close()
# def catculateCost(g =0, h = 0):
# pass
# def getNeighbours(graph, city):
# pass
# def reconstructPath(camefrom, current):
# totalPath = []
# while current in camefrom.keys():
# current = camefrom[current]
# totalPath.append(current)
# return totalPath
# def distBetween(current, neighbor):
# return 0
# def heuristicCostEstimate(heuristicData, city):
# return heuristicData[city]
# def cityWithLowestFvalue(openedSet):
# lowCostNeighbour = ""
# cost = 0
# for city in openedSet:
# cost = 12
# return lowCostNeighbour
def performSearch(model, start, goal):
# closedSet = openedSet = set()
# openedSet.add(start)
# cameFrom = {}
# g_score = {}
# f_score = {}
# f_score[start] = heuristicCostEstimate(model.heuristicData, start)
# while openedSet is not None:
# current = cityWithLowestFvalue(openedSet)
# if current == goal:
# return reconstructPath(cameFrom, current)
# openedSet.remove(current)
# closedSet.add(current)
# for neighbor in getNeighbours(model.inputData, current):
# if neighbor in closedSet:
# continue
# tentativeGScore = g_score[current] + distBetween(current, neighbor)
# if neighbor not in openedSet:
# openedSet.add(neighbor)
# elif tentativeGScore >= g_score[neighbor]:
# continue
# cameFrom[neighbor] = current
# g_score[neighbor] = tentativeGScore
# f_score[neighbor] = g_score[neighbor] + heuristicCostEstimate(neighbor, goal)
print(model)
#---------#---------#---------#---------#---------#--------#
def _main() :
# Get the file name and the other arguments.
fName = sys.argv[1]
origin_city = sys.argv[2].lower()
destination_city = sys.argv[3].lower()
hName = sys.argv[4]
model = FindRoute( fName, origin_city, destination_city, hName )
performSearch(model,origin_city, destination_city)
# print("Origin City")
# for tup in model.inputData:
# print('From %s \n'%(tup))
# for des in model.inputData[tup]:
# print("%s is at %d kilometre"%(des[0], des[1]))
# print("\n\n")
# print("The heuristic destance to kassel from different cities")
# for city in model.heuristicData:
# print("%s at %d"%(city, model.heuristicData[city]))
print("hello")
if __name__ == '__main__' :
_main()