-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinformedSearch.py
executable file
·392 lines (310 loc) · 10.3 KB
/
informedSearch.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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
import heapq
import Problem
from timeit import default_timer as timer
"""
heuristic for the 1 problem
"""
def heuristicOneProb(state,prob):
estCost = 0
if state.loaded:
if state.vLoc == prob.dest:
estCost = prob.dest
else:
#at 0 or source
estCost = abs(prob.dest - state.vLoc) + prob.dest
else:
estCost = abs(prob.src - state.vLoc) + abs(prob.src - prob.dest) + prob.dest
return estCost
"""
heuristic for the 1 problem but 2 dimensions
"""
def heuristicOneProb2D(state,prob):
estCost = 0
if state.loaded:
if state.vLoc == prob.dest:
estCost = prob.eudCalc((0,0), prob.dest)
else:
#at 0 or source
estCost = prob.eudCalc(state.vLoc,prob.dest) + prob.eudCalc((0,0), prob.dest)
else:
estCost = prob.eudCalc(state.vLoc, prob.src) + prob.eudCalc(prob.src,prob.dest) + prob.eudCalc((0,0), prob.dest)
return estCost
"""
heuristic for M=K=1, N=2, Y=2
This is too big to write out, several if statements too many conditions t consider
Need a better heuristic while the problem scales up
"""
#
# def heuristicMK1NY2(state,prob):
# estCost = 0
# if state.loaded[0]:
# if state.vLoc == prob.dest[0]:
# if state.pLoc[1] == prob.dest[1]:
# estCost = coordinate.eudCal(prob.dest[0],(0,0))
# else:
# estCost = coordinate.eudCalc(prob.dest[0],prob.src[1]) + coordinate.eudCalc(prob.src[1],prob.dest[1]) + coordinate.eudCalc(prob.dest[1],(0,0))
# elif state.vLoc == prob.dest[1]:
# if state.pLoc[0] == prob.dest[0]:
# estCost = coordinate.eudCalc(prob.dest[1],prob.src[1]) + coordinate.eudCalc(prob.src[1],prob.dest[1]) + coordinate.eudCalc(prob.dest[1],(0,0)])
# else:
# if coordinate.eudCalc(prob.dest[1],prob.src[0]) < coordinate.eudCalc(prob.dest[1],prob.src[1]):
# coordinate.eudCalc(prob.dest[1],prob.src[0]) + coordinate.eudCalc(prob.src[0],prob.dest[0]) + coordinate.eudCalc(prob.dest[0],prob.src[1]) + coordinate.eudCalc(prob.src[1],prob.dest[1]) + coordinate.eudCalc(prob.dest[1],(0,0))
# else:
# coordinate.eudCalc(prob.dest[1],prob.src[1]) + coordinate.eudCalc(prob.src[1],prob.dest[1]) + coordinate.eudCalc(prob.dest[1],prob.src[0]) + coordinate.eudCalc(prob.src[0],prob.dest[0]) + coordinate.eudCalc(prob.dest[0],(0,0))
# elif state.vLoc == prob.src[0]:
# else:
# elif state.loaded[1]:
# elif state.loaded[0] and state.loaded[1]:
# else:
#
#
# return estCost
"""
Alternative to brute force heuristic for M=N=1, N=Y=2
perhpas consdier the distance to get each individual package to their goal
break down the problem into a 1 problem
"""
def heuristicMN1KY2(state, prob):
estCost = 0
for i in range(0,len(state.pLoc),1):
if state.pLoc[i] is not prob.dest:
estCost = estCost + Problem.coordinate.eudCalc(state.pLoc[i],prob.dest[i])
return estCost
"""
Greedy search
"""
def greedySearch(state):
heap = []
heap.extend(Problem.getSuccessors(state))
heapq.heapify(heap)
while len(heap) > 0:
currentState = heapq.heappop(heap)
if Problem.isGoal(currentState):
return currentState
else:
heap.extend(Problem.getSuccessors(currentState))
return None
"""
A* search
"""
def aStarSearch(state, prob):
heap = []
newNodes = []
#newNodes.extend(prob.getSuccessors(state))
seqPath = []
#for vState in newNodes:
#print ("Values are: ",type(vState))
#heapq.heappush(heap,(heuristicOneProb(vState,prob) + vState.distance), vState)
heapq.heappush(heap,(0,state))
maxSize = len(heap)
nodesCreated = len(heap)
print ("Entering Loop with" )
print ("Heap Length: ", maxSize)
print ("Nodes Created", nodesCreated)
while len(heap) > 0:
currentState = heapq.heappop(heap)[1]
# print ("within currentState is: ", type(currentState))
seqPath.append(currentState)
if prob.isGoal(currentState):
return (seqPath, nodesCreated, maxSize)
else:
newNodes.extend(prob.getSuccessors(currentState))
for vState in newNodes:
heapq.heappush(heap,(heuristicOneProb(vState, prob) + vState.distance, vState))
nodesCreated += 1
newNodes.remove(vState)
if len(heap) > maxSize:
maxSize = len(heap)
#print("Heap is: ", heap)
return (seqPath,nodesCreated, maxSize)
"""
A* search TEST
"""
def aStarSearchWithRef(state, prob):
heap = []
newNodes = []
#newNodes.extend(prob.getSuccessors(state))
#seqPath = []
#for vState in newNodes:
#print ("Values are: ",type(vState))
#heapq.heappush(heap,(heuristicOneProb(vState,prob) + vState.distance), vState)
heapq.heappush(heap,(0,state))
maxSize = len(heap)
nodesCreated = len(heap)
print ("Entering Loop with" )
print ("Heap Length: ", maxSize)
print ("Nodes Created", nodesCreated)
while len(heap) > 0:
currentState = heapq.heappop(heap)[1]
# print ("within currentState is: ", type(currentState))
#seqPath.append(currentState)
if prob.isOneProbGoal(currentState):
return (currentState, nodesCreated, maxSize)
else:
newNodes.extend(prob.getSuccessorsOneAStar(currentState))
for vState in newNodes:
heapq.heappush(heap,(heuristicOneProb(vState, prob) + vState.distance, vState))
nodesCreated += 1
newNodes.remove(vState)
if len(heap) > maxSize:
maxSize = len(heap)
#print("Heap is: ", heap)
return (None,nodesCreated, maxSize)
"""
aStarGeneral
A* search using our general successor This should handle a wider range of problems
:param state: the start state of the current search is a search object.
:param prob: the problem object for this search
:return: returns the goal state(with references to its parents), the number of nodes created, the largest size of the heap and the largest distance travelled by one truck in a tuple
"""
def aStarGeneral(state, prob):
#heap to sort states
heap = []
#temporary holder for new States
newNodes = []
#creating a heap out of the heap list
heapq.heappush(heap, (0, state))
maxSize = len(heap)
nodesCreated = len(heap)
while len(heap) > 0:
currentState = heapq.heappop(heap)[1]
#finds the maximum distance traveled and the sum of distances
maxdist = 0
sumdist = 0
for dists in currentState.distance:
sumdist += dists
if dists > maxdist:
maxdist = dists
if prob.isGoal(currentState):
return (currentState, nodesCreated, maxSize, maxdist)
else:
newNodes.extend(prob.getSuccessorsGeneral(currentState))
for vState in newNodes:
heapq.heappush(heap, (heuristicMN1KY2(vState, prob) + sumdist + 10*maxdist, vState))
nodesCreated += 1
newNodes.remove(vState)
if len(heap) > maxSize:
maxSize = len(heap)
return (None, nodesCreated, maxSize, maxdist)
"""
The tests for the informed search
"""
def runTests():
bannr = "\n********************************\n"
print ("TESTING INFORMED SEARCH",bannr)
src = 0.5
dst = 1.0
tstProb = Problem.Problem(dst,src, 1, 1, 1)
tstState = Problem.ProblemStateWithRef(0,src,False,0, None)
#Validating input
print ("Environment is: ",bannr)
print ("Source: ", src,'\n',
"Dest : ", dst,'\n',
tstProb.toString(),'\n',
tstState.toString(),'\n')
#Testing the A*
print ("Test A*",bannr)
"""
aStar = aStarSearch(tstState, tstProb)
print ("A* is a: ", type(aStar))
print ("And in that is: ", aStar)
n = 0
for state in aStar[0]:
try:
print ("vLoc",n,": ",state.vLoc)
print ("pLoc",n,": ",state.pLoc)
print ("load",n,": ",state.loaded,bannr)
n += 1
except TypeError as tplsSuk:
print (tplsSuk)
print("The number of nodes created is ", aStar[1])
print("The largest size the heap gets is ", aStar[2])
"""
"""
This will need some syntax work
"""
aStar1 = aStarSearchWithRef(tstState,tstProb)
numNodespath = 0
temp = aStar1[0]
print("Printing Path from goal to start")
while temp is not None:
numNodespath += 1
#print in here
print(temp.toString())
print(bannr)
temp = temp.parentState
print("Depth of Search was ", numNodespath)
print("Number of Nodes created ", aStar1[1])
print("Maximum size of the heap ", aStar1[2])
#Testing 2D#######
#print ("Testing 2D A*",bannr)
#Testing the general case
print("??????????????????????????????????????????????????????????????????????????????????")
dests = [Problem.coordinate(0.5, 0.5), Problem.coordinate(1.0, 1.0)]
srcs = [Problem.coordinate(1.5, 1.5), Problem.coordinate(0.7, 0.6)]
genProb = Problem.Problem(dests, srcs, 1, 1, 2)
loades = [False, False]
dists = [0]
genState = Problem.ProblemStateGeneral([Problem.coordinate(0, 0)], list(srcs), [loades], dists, None)
genStar = aStarGeneral(genState, genProb)
numNodespath = 0
temp = genStar[0]
print("Printing Path from goal to start")
while temp is not None:
numNodespath += 1
# print in here
print(temp.toString())
print(bannr)
temp = temp.parentState
print("Depth of Search was ", numNodespath)
print("Number of Nodes created ", genStar[1])
print("Maximum size of the heap ", genStar[2])
print("Max distance by one truck ", genStar[3])
# print("Final State of the Problem ", tstProb.toString())
def timedTest():
testProb = Problem.Problem(0.5, 1.0, 1, 1, 1)
testState = Problem.ProblemStateWithRef(0, 1.0, False, 0, None)
#timing out the 1 problem
start = timer()
res = aStarSearchWithRef(testState, testProb)
end = timer()
print(" Result of A* on MNKY = 1 is ", end - start)
src = Problem.coordinate(0.5, 0.5)
dest = Problem.coordinate(1.0, 1.0)
tDProb = Problem.Problem(dest, src, 1, 1, 1)
tDStart = Problem.ProblemStateWithRef(Problem.coordinate(0,0), src, False, 0, None)
#timing the one problem on 2D
start = timer()
#res = aStarSearchWithRefTD(tDStart, tDProb )
end = timer()
#Add code to time for the general search.
def showcase():
bannr = '\n*****************************\n'
print("A* MNKY=1",bannr)
#Initialize environment
src = 0.5
dst = 1.0
tstProb = Problem.Problem(dst,src, 1, 1, 1)
tstState = Problem.ProblemStateWithRef(0,src,False,0, None)
aStar1 = aStarSearchWithRef(tstState,tstProb)
numNodespath = 0
temp = aStar1[0]
#Showcase environment.
print ("Environment is: ",bannr)
print ("Source: ", src,'\n',
"Dest : ", dst,'\n',
tstProb.toString(),'\n',
tstState.toString(),'\n')
print("Number of Nodes created ", aStar1[1])
print("Maximum size of the heap ", aStar1[2])
#Check resultant depth
showPath = input("Show path knowing it could be pretty deep? (y/n)")
if (showPath is 'y'):
while temp is not None:
numNodespath += 1
print(temp.toString())
#print(bannr)
temp = temp.parentState
print("Depth of Search was ", numNodespath)
print (bannr, "And now some time stats!",bannr)
timedTest()