-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_final.py
724 lines (622 loc) · 41.7 KB
/
test_final.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
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
import numpy as np
from operator import itemgetter
import time
class Node():
def __init__(self,state,parent,action,depth,step_cost,path_cost,heuristic_cost):
self.state = state
self.parent = parent # parent node
self.action = action # move up, left, down, right
self.depth = depth # depth of the node in the tree
self.step_cost = step_cost # g(n), the cost to take the step
self.path_cost = path_cost # accumulated g(n), the cost to reach the current node
self.heuristic_cost = heuristic_cost # h(n), cost to reach goal state from the current node
# children node
self.move_up = None
self.move_left = None
self.move_down = None
self.move_right = None
# see if moving down is valid
def try_move_down(self):
# index of the empty tile
zero_index=[i[0] for i in np.where(self.state==0)]
if zero_index[0] == 0:
return False
else:
up_value = self.state[zero_index[0]-1,zero_index[1]] # value of the upper tile
new_state = self.state.copy()
new_state[zero_index[0],zero_index[1]] = up_value
new_state[zero_index[0]-1,zero_index[1]] = 0
return new_state,up_value
# see if moving right is valid
def try_move_right(self):
zero_index=[i[0] for i in np.where(self.state==0)]
if zero_index[1] == 0:
return False
else:
left_value = self.state[zero_index[0],zero_index[1]-1] # value of the left tile
new_state = self.state.copy()
new_state[zero_index[0],zero_index[1]] = left_value
new_state[zero_index[0],zero_index[1]-1] = 0
return new_state,left_value
# see if moving up is valid
def try_move_up(self):
zero_index=[i[0] for i in np.where(self.state==0)]
if zero_index[0] == 2:
return False
else:
lower_value = self.state[zero_index[0]+1,zero_index[1]] # value of the lower tile
new_state = self.state.copy()
new_state[zero_index[0],zero_index[1]] = lower_value
new_state[zero_index[0]+1,zero_index[1]] = 0
return new_state,lower_value
# see if moving left is valid
def try_move_left(self):
zero_index=[i[0] for i in np.where(self.state==0)]
if zero_index[1] == 2:
return False
else:
right_value = self.state[zero_index[0],zero_index[1]+1] # value of the right tile
new_state = self.state.copy()
new_state[zero_index[0],zero_index[1]] = right_value
new_state[zero_index[0],zero_index[1]+1] = 0
return new_state,right_value
# return user specified heuristic cost
def get_h_cost(self,new_state,goal_state,heuristic_function,path_cost,depth):
if heuristic_function == 'num_misplaced':
return self.h_misplaced_cost(new_state,goal_state)
elif heuristic_function == 'manhattan':
return self.h_manhattan_cost(new_state,goal_state)
# since this game is made unfair by setting the step cost as the value of the tile being moved
# to make it fair, I made all the step cost as 1
# made it a best-first-search with manhattan heuristic function
elif heuristic_function == 'fair_manhattan':
return self.h_manhattan_cost(new_state,goal_state) - path_cost + depth
# return heuristic cost: number of misplaced tiles
def h_misplaced_cost(self,new_state,goal_state):
cost = np.sum(new_state != goal_state)-1 # minus 1 to exclude the empty tile
if cost > 0:
return cost
else:
return 0 # when all tiles matches
# return heuristic cost: sum of Manhattan distance to reach the goal state
def h_manhattan_cost(self,new_state,goal_state):
current = new_state
# digit and coordinates they are supposed to be
goal_position_dic = {1:(0,0),2:(0,1),3:(0,2),8:(1,0),0:(1,1),4:(1,2),7:(2,0),6:(2,1),5:(2,2)}
sum_manhattan = 0
for i in range(3):
for j in range(3):
if current[i,j] != 0:
sum_manhattan += sum(abs(a-b) for a,b in zip((i,j), goal_position_dic[current[i,j]]))
return sum_manhattan
# once the goal node is found, trace back to the root node and print out the path
def print_path(self):
# create FILO stacks to place the trace
state_trace = [self.state]
action_trace = [self.action]
depth_trace = [self.depth]
step_cost_trace = [self.step_cost]
path_cost_trace = [self.path_cost]
heuristic_cost_trace = [self.heuristic_cost]
# add node information as tracing back up the tree
while self.parent:
self = self.parent
state_trace.append(self.state)
action_trace.append(self.action)
depth_trace.append(self.depth)
step_cost_trace.append(self.step_cost)
path_cost_trace.append(self.path_cost)
heuristic_cost_trace.append(self.heuristic_cost)
# print out the path
step_counter = 0
while state_trace:
print ('step',step_counter)
print (state_trace.pop())
print ('action=',action_trace.pop(),', depth=',str(depth_trace.pop()),\
', step cost=',str(step_cost_trace.pop()),', total_cost=',\
str(path_cost_trace.pop() + heuristic_cost_trace.pop()),'\n')
step_counter += 1
def breadth_first_search(self, goal_state):
start = time.time()
queue = [self] # queue of found but unvisited nodes, FIFO
queue_num_nodes_popped = 0 # number of nodes popped off the queue, measuring time performance
queue_max_length = 1 # max number of nodes in the queue, measuring space performance
depth_queue = [0] # queue of node depth
path_cost_queue = [0] # queue for path cost
visited = set([]) # record visited states
while queue:
# update maximum length of the queue
if len(queue) > queue_max_length:
queue_max_length = len(queue)
current_node = queue.pop(0) # select and remove the first node in the queue
queue_num_nodes_popped += 1
current_depth = depth_queue.pop(0) # select and remove the depth for current node
current_path_cost = path_cost_queue.pop(0) # # select and remove the path cost for reaching current node
visited.add(tuple(current_node.state.reshape(1,9)[0])) # avoid repeated state, which is represented as a tuple
# when the goal state is found, trace back to the root node and print out the path
if np.array_equal(current_node.state,goal_state):
current_node.print_path()
print ('Time performance:',str(queue_num_nodes_popped),'nodes popped off the queue.')
print ('Space performance:', str(queue_max_length),'nodes in the queue at its max.')
print ('Time spent: %0.2fs' % (time.time()-start))
return True
else:
# see if moving upper tile down is a valid move
if current_node.try_move_down():
new_state,up_value = current_node.try_move_down()
# check if the resulting node is already visited
if tuple(new_state.reshape(1,9)[0]) not in visited:
# create a new child node
current_node.move_down = Node(state=new_state,parent=current_node,action='down',depth=current_depth+1,\
step_cost=up_value,path_cost=current_path_cost+up_value,heuristic_cost=0)
queue.append(current_node.move_down)
depth_queue.append(current_depth+1)
path_cost_queue.append(current_path_cost+up_value)
# see if moving left tile to the right is a valid move
if current_node.try_move_right():
new_state,left_value = current_node.try_move_right()
# check if the resulting node is already visited
if tuple(new_state.reshape(1,9)[0]) not in visited:
# create a new child node
current_node.move_right = Node(state=new_state,parent=current_node,action='right',depth=current_depth+1,\
step_cost=left_value,path_cost=current_path_cost+left_value,heuristic_cost=0)
queue.append(current_node.move_right)
depth_queue.append(current_depth+1)
path_cost_queue.append(current_path_cost+left_value)
# see if moving lower tile up is a valid move
if current_node.try_move_up():
new_state,lower_value = current_node.try_move_up()
# check if the resulting node is already visited
if tuple(new_state.reshape(1,9)[0]) not in visited:
# create a new child node
current_node.move_up = Node(state=new_state,parent=current_node,action='up',depth=current_depth+1,\
step_cost=lower_value,path_cost=current_path_cost+lower_value,heuristic_cost=0)
queue.append(current_node.move_up)
depth_queue.append(current_depth+1)
path_cost_queue.append(current_path_cost+lower_value)
# see if moving right tile to the left is a valid move
if current_node.try_move_left():
new_state,right_value = current_node.try_move_left()
# check if the resulting node is already visited
if tuple(new_state.reshape(1,9)[0]) not in visited:
# create a new child node
current_node.move_left = Node(state=new_state,parent=current_node,action='left',depth=current_depth+1,\
step_cost=right_value,path_cost=current_path_cost+right_value,heuristic_cost=0)
queue.append(current_node.move_left)
depth_queue.append(current_depth+1)
path_cost_queue.append(current_path_cost+right_value)
def depth_first_search(self, goal_state):
start = time.time()
queue = [self] # queue of found but unvisited nodes, FILO
queue_num_nodes_popped = 0 # number of nodes popped off the queue, measuring time performance
queue_max_length = 1 # max number of nodes in the queue, measuring space performance
depth_queue = [0] # queue of node depth
path_cost_queue = [0] # queue for path cost
visited = set([]) # record visited states
while queue:
# update maximum length of the queue
if len(queue) > queue_max_length:
queue_max_length = len(queue)
current_node = queue.pop(0) # select and remove the first node in the queue
queue_num_nodes_popped += 1
current_depth = depth_queue.pop(0) # select and remove the depth for current node
current_path_cost = path_cost_queue.pop(0) # # select and remove the path cost for reaching current node
visited.add(tuple(current_node.state.reshape(1,9)[0])) # add state, which is represented as a tuple
# when the goal state is found, trace back to the root node and print out the path
if np.array_equal(current_node.state,goal_state):
current_node.print_path()
print ('Time performance:',str(queue_num_nodes_popped),'nodes popped off the queue.')
print ('Space performance:', str(queue_max_length),'nodes in the queue at its max.')
print ('Time spent: %0.2fs' % (time.time()-start))
return True
else:
# see if moving upper tile down is a valid move
if current_node.try_move_down():
new_state,up_value = current_node.try_move_down()
# check if the resulting node is already visited
if tuple(new_state.reshape(1,9)[0]) not in visited:
# create a new child node
current_node.move_down = Node(state=new_state,parent=current_node,action='down',depth=current_depth+1,\
step_cost=up_value,path_cost=current_path_cost+up_value,heuristic_cost=0)
queue.insert(0,current_node.move_down)
depth_queue.insert(0,current_depth+1)
path_cost_queue.insert(0,current_path_cost+up_value)
# see if moving left tile to the right is a valid move
if current_node.try_move_right():
new_state,left_value = current_node.try_move_right()
# check if the resulting node is already visited
if tuple(new_state.reshape(1,9)[0]) not in visited:
# create a new child node
current_node.move_right = Node(state=new_state,parent=current_node,action='right',depth=current_depth+1,\
step_cost=left_value,path_cost=current_path_cost+left_value,heuristic_cost=0)
queue.insert(0,current_node.move_right)
depth_queue.insert(0,current_depth+1)
path_cost_queue.insert(0,current_path_cost+left_value)
# see if moving lower tile up is a valid move
if current_node.try_move_up():
new_state,lower_value = current_node.try_move_up()
# check if the resulting node is already visited
if tuple(new_state.reshape(1,9)[0]) not in visited:
# create a new child node
current_node.move_up = Node(state=new_state,parent=current_node,action='up',depth=current_depth+1,\
step_cost=lower_value,path_cost=current_path_cost+lower_value,heuristic_cost=0)
queue.insert(0,current_node.move_up)
depth_queue.insert(0,current_depth+1)
path_cost_queue.insert(0,current_path_cost+lower_value)
# see if moving right tile to the left is a valid move
if current_node.try_move_left():
new_state,right_value = current_node.try_move_left()
# check if the resulting node is already visited
if tuple(new_state.reshape(1,9)[0]) not in visited:
# create a new child node
current_node.move_left = Node(state=new_state,parent=current_node,action='left',depth=current_depth+1,\
step_cost=right_value,path_cost=current_path_cost+right_value,heuristic_cost=0)
queue.insert(0,current_node.move_left)
depth_queue.insert(0,current_depth+1)
path_cost_queue.insert(0,current_path_cost+right_value)
def iterative_deepening_DFS(self, goal_state):
start = time.time()
queue_num_nodes_popped = 0 # number of nodes popped off the queue, measuring time performance
queue_max_length = 1 # max number of nodes in the queue, measuring space performance
# search the tree that's 40 levels in depth
for depth_limit in range(40):
#print 'depth limit',depth_limit
queue = [self] # queue of found but unvisited nodes, FILO
depth_queue = [0] # queue of node depth
path_cost_queue = [0] # queue for path cost
visited = set([]) # record visited states
while queue:
# update maximum length of the queue
if len(queue) > queue_max_length:
queue_max_length = len(queue)
current_node = queue.pop(0) # select and remove the first node in the queue
#print 'pop'
#print current_node.state
queue_num_nodes_popped += 1
current_depth = depth_queue.pop(0) # select and remove the depth for current node
#print 'depth:',current_depth,'\n'
current_path_cost = path_cost_queue.pop(0) # # select and remove the path cost for reaching current node
visited.add(tuple(current_node.state.reshape(1,9)[0])) # add state, which is represented as a tuple
# when the goal state is found, trace back to the root node and print out the path
if np.array_equal(current_node.state,goal_state):
current_node.print_path()
print ('Time performance:',str(queue_num_nodes_popped),'nodes popped off the queue.')
print ('Space performance:', str(queue_max_length),'nodes in the queue at its max.')
print ('Time spent: %0.2fs' % (time.time()-start))
return True
else:
if current_depth < depth_limit:
# see if moving upper tile down is a valid move
if current_node.try_move_down():
new_state,up_value = current_node.try_move_down()
# check if the resulting node is already visited
if tuple(new_state.reshape(1,9)[0]) not in visited:
# create a new child node
current_node.move_down = Node(state=new_state,parent=current_node,action='down',depth=current_depth+1,\
step_cost=up_value,path_cost=current_path_cost+up_value,heuristic_cost=0)
queue.insert(0,current_node.move_down)
depth_queue.insert(0,current_depth+1)
path_cost_queue.insert(0,current_path_cost+up_value)
# see if moving left tile to the right is a valid move
if current_node.try_move_right():
new_state,left_value = current_node.try_move_right()
# check if the resulting node is already visited
if tuple(new_state.reshape(1,9)[0]) not in visited:
# create a new child node
current_node.move_right = Node(state=new_state,parent=current_node,action='right',depth=current_depth+1,\
step_cost=left_value,path_cost=current_path_cost+left_value,heuristic_cost=0)
queue.insert(0,current_node.move_right)
depth_queue.insert(0,current_depth+1)
path_cost_queue.insert(0,current_path_cost+left_value)
# see if moving lower tile up is a valid move
if current_node.try_move_up():
new_state,lower_value = current_node.try_move_up()
# check if the resulting node is already visited
if tuple(new_state.reshape(1,9)[0]) not in visited:
# create a new child node
current_node.move_up = Node(state=new_state,parent=current_node,action='up',depth=current_depth+1,\
step_cost=lower_value,path_cost=current_path_cost+lower_value,heuristic_cost=0)
queue.insert(0,current_node.move_up)
depth_queue.insert(0,current_depth+1)
path_cost_queue.insert(0,current_path_cost+lower_value)
# see if moving right tile to the left is a valid move
if current_node.try_move_left():
new_state,right_value = current_node.try_move_left()
# check if the resulting node is already visited
if tuple(new_state.reshape(1,9)[0]) not in visited:
# create a new child node
current_node.move_left = Node(state=new_state,parent=current_node,action='left',depth=current_depth+1,\
step_cost=right_value,path_cost=current_path_cost+right_value,heuristic_cost=0)
queue.insert(0,current_node.move_left)
depth_queue.insert(0,current_depth+1)
path_cost_queue.insert(0,current_path_cost+right_value)
# An extension of BFS that's guided by a prioritized queue based on path cost
def uniform_cost_search(self, goal_state):
start = time.time()
queue = [(self,0)] # queue of (found but unvisited nodes, path cost), ordered by path cost(accumulated step cost)
queue_num_nodes_popped = 0 # number of nodes popped off the queue, measuring time performance
queue_max_length = 1 # max number of nodes in the queue, measuring space performance
depth_queue = [(0,0)] # queue of node depth, (depth, path cost)
path_cost_queue = [0] # queue for path cost
visited = set([]) # record visited states
while queue:
# sort queue based on path cost, in ascending order
queue = sorted(queue, key=lambda x: x[1])
# depth_queue = sorted(depth_queue, key=lambda x: x[1])
depth_queue = sorted(depth_queue, key=itemgetter(0), reverse=False)
path_cost_queue = sorted(path_cost_queue, key=lambda x: x)
# update maximum length of the queue
if len(queue) > queue_max_length:
queue_max_length = len(queue)
current_node = queue.pop(0)[0] # select and remove the first node in the queue
#print 'pop'
#print current_node.state,'\n'
queue_num_nodes_popped += 1
current_depth = depth_queue.pop(0)[0] # select and remove the depth for current node
current_path_cost = path_cost_queue.pop(0) # # select and remove the path cost for reaching current node
visited.add(tuple(current_node.state.reshape(1,9)[0])) # avoid repeated state, which is represented as a tuple
print("number of node poped "+str(queue_num_nodes_popped))
print("Current depth "+str(current_depth))
# when the goal state is found, trace back to the root node and print out the path
if np.array_equal(current_node.state,goal_state):
current_node.print_path()
print ('Time performance:',str(queue_num_nodes_popped),'nodes popped off the queue.')
print ('Space performance:', str(queue_max_length),'nodes in the queue at its max.')
print ('Time spent: %0.2fs' % (time.time()-start))
return True
else:
# see if moving upper tile down is a valid move
if current_node.try_move_down():
new_state,up_value = current_node.try_move_down()
# check if the resulting node is already visited
if tuple(new_state.reshape(1,9)[0]) not in visited:
# create a new child node
current_node.move_down = Node(state=new_state,parent=current_node,action='down',depth=current_depth+1,\
step_cost=up_value,path_cost=current_path_cost+up_value,heuristic_cost=0)
queue.append((current_node.move_down,current_path_cost+up_value))
depth_queue.append((current_depth+1,current_path_cost+up_value))
path_cost_queue.append(current_path_cost+up_value)
# see if moving left tile to the right is a valid move
if current_node.try_move_right():
new_state,left_value = current_node.try_move_right()
# check if the resulting node is already visited
if tuple(new_state.reshape(1,9)[0]) not in visited:
# create a new child node
current_node.move_right = Node(state=new_state,parent=current_node,action='right',depth=current_depth+1,\
step_cost=left_value,path_cost=current_path_cost+left_value,heuristic_cost=0)
queue.append((current_node.move_right,current_path_cost+left_value))
depth_queue.append((current_depth+1,current_path_cost+left_value))
path_cost_queue.append(current_path_cost+left_value)
# see if moving lower tile up is a valid move
if current_node.try_move_up():
new_state,lower_value = current_node.try_move_up()
# check if the resulting node is already visited
if tuple(new_state.reshape(1,9)[0]) not in visited:
# create a new child node
current_node.move_up = Node(state=new_state,parent=current_node,action='up',depth=current_depth+1,\
step_cost=lower_value,path_cost=current_path_cost+lower_value,heuristic_cost=0)
queue.append((current_node.move_up,current_path_cost+lower_value))
depth_queue.append((current_depth+1,current_path_cost+lower_value))
path_cost_queue.append(current_path_cost+lower_value)
# see if moving right tile to the left is a valid move
if current_node.try_move_left():
new_state,right_value = current_node.try_move_left()
# check if the resulting node is already visited
if tuple(new_state.reshape(1,9)[0]) not in visited:
# create a new child node
current_node.move_left = Node(state=new_state,parent=current_node,action='left',depth=current_depth+1,\
step_cost=right_value,path_cost=current_path_cost+right_value,heuristic_cost=0)
queue.append((current_node.move_left,current_path_cost+right_value))
depth_queue.append((current_depth+1,current_path_cost+right_value))
path_cost_queue.append(current_path_cost+right_value)
# search based on heuristic cost
def best_first_search(self, goal_state):
start = time.time()
queue = [(self,0)] # queue of (found but unvisited nodes, heuristic cost), ordered by heuristic cost
queue_num_nodes_popped = 0 # number of nodes popped off the queue, measuring time performance
queue_max_length = 1 # max number of nodes in the queue, measuring space performance
depth_queue = [(0,0)] # queue of node depth, (depth, heuristic cost)
path_cost_queue = [(0,0)] # queue for path cost, (path_cost, heuristic cost)
visited = set([]) # record visited states
while queue:
# sort queue based on heuristic cost, in ascending order
queue = sorted(queue, key=lambda x: x[1])
depth_queue = sorted(depth_queue, key=lambda x: x[1])
path_cost_queue = sorted(path_cost_queue, key=lambda x: x[1])
# update maximum length of the queue
if len(queue) > queue_max_length:
queue_max_length = len(queue)
current_node = queue.pop(0)[0] # select and remove the first node in the queue
#print 'pop'
#print current_node.state
#print 'heuristic_cost',current_node.heuristic_cost,'\n'
queue_num_nodes_popped += 1
current_depth = depth_queue.pop(0)[0] # select and remove the depth for current node
current_path_cost = path_cost_queue.pop(0)[0] # # select and remove the path cost for reaching current node
visited.add(tuple(current_node.state.reshape(1,9)[0])) # avoid repeated state, which is represented as a tuple
# when the goal state is found, trace back to the root node and print out the path
if np.array_equal(current_node.state,goal_state):
current_node.print_path()
print ('Time performance:',str(queue_num_nodes_popped),'nodes popped off the queue.')
print ('Space performance:', str(queue_max_length),'nodes in the queue at its max.')
print ('Time spent: %0.2fs' % (time.time()-start))
return True
else:
# see if moving upper tile down is a valid move
if current_node.try_move_down():
new_state,up_value = current_node.try_move_down()
# check if the resulting node is already visited
if tuple(new_state.reshape(1,9)[0]) not in visited:
# get heuristic cost
h_cost = self.h_misplaced_cost(new_state,goal_state)
# create a new child node
current_node.move_down = Node(state=new_state,parent=current_node,action='down',depth=current_depth+1,\
step_cost=up_value,path_cost=current_path_cost+up_value,heuristic_cost=h_cost)
queue.append((current_node.move_down,h_cost))
depth_queue.append((current_depth+1,h_cost))
path_cost_queue.append((current_path_cost+up_value,h_cost))
# see if moving left tile to the right is a valid move
if current_node.try_move_right():
new_state,left_value = current_node.try_move_right()
# check if the resulting node is already visited
if tuple(new_state.reshape(1,9)[0]) not in visited:
# get heuristic cost
h_cost = self.h_misplaced_cost(new_state,goal_state)
# create a new child node
current_node.move_right = Node(state=new_state,parent=current_node,action='right',depth=current_depth+1,\
step_cost=left_value,path_cost=current_path_cost+left_value,heuristic_cost=h_cost)
queue.append((current_node.move_right,h_cost))
depth_queue.append((current_depth+1,h_cost))
path_cost_queue.append((current_path_cost+left_value,h_cost))
# see if moving lower tile up is a valid move
if current_node.try_move_up():
new_state,lower_value = current_node.try_move_up()
# check if the resulting node is already visited
if tuple(new_state.reshape(1,9)[0]) not in visited:
# get heuristic cost
h_cost = self.h_misplaced_cost(new_state,goal_state)
# create a new child node
current_node.move_up = Node(state=new_state,parent=current_node,action='up',depth=current_depth+1,\
step_cost=lower_value,path_cost=current_path_cost+lower_value,heuristic_cost=h_cost)
queue.append((current_node.move_up,h_cost))
depth_queue.append((current_depth+1,h_cost))
path_cost_queue.append((current_path_cost+lower_value,h_cost))
# see if moving right tile to the left is a valid move
if current_node.try_move_left():
new_state,right_value = current_node.try_move_left()
# check if the resulting node is already visited
if tuple(new_state.reshape(1,9)[0]) not in visited:
# get heuristic cost
h_cost = self.h_misplaced_cost(new_state,goal_state)
# create a new child node
current_node.move_left = Node(state=new_state,parent=current_node,action='left',depth=current_depth+1,\
step_cost=right_value,path_cost=current_path_cost+right_value,heuristic_cost=h_cost)
queue.append((current_node.move_left,h_cost))
depth_queue.append((current_depth+1,h_cost))
path_cost_queue.append((current_path_cost+right_value,h_cost))
# search based on path cost + heuristic cost
def a_star_search(self,goal_state,heuristic_function):
start = time.time()
queue = [(self,0)] # queue of (found but unvisited nodes, path cost+heuristic cost), ordered by the second element
queue_num_nodes_popped = 0 # number of nodes popped off the queue, measuring time performance
queue_max_length = 1 # max number of nodes in the queue, measuring space performance
depth_queue = [(0,0)] # queue of node depth, (depth, path_cost+heuristic cost)
path_cost_queue = [(0,0)] # queue for path cost, (path_cost, path_cost+heuristic cost)
visited = set([]) # record visited states
while queue:
# sort queue based on path_cost+heuristic cost, in ascending order
queue = sorted(queue, key=lambda x: x[1])
depth_queue = sorted(depth_queue, key=lambda x: x[1])
path_cost_queue = sorted(path_cost_queue, key=lambda x: x[1])
# update maximum length of the queue
if len(queue) > queue_max_length:
queue_max_length = len(queue)
current_node = queue.pop(0)[0] # select and remove the first node in the queue
#print 'pop'
#print current_node.state
#print 'path_cost',current_node.path_cost
#print 'heuristic_cost',current_node.heuristic_cost
#print 'total_cost',current_node.path_cost+current_node.heuristic_cost,'\n'
queue_num_nodes_popped += 1
current_depth = depth_queue.pop(0)[0] # select and remove the depth for current node
current_path_cost = path_cost_queue.pop(0)[0] # # select and remove the path cost for reaching current node
visited.add(tuple(current_node.state.reshape(1,9)[0])) # avoid repeated state, which is represented as a tuple
# when the goal state is found, trace back to the root node and print out the path
if np.array_equal(current_node.state,goal_state):
current_node.print_path()
print ('Time performance:',str(queue_num_nodes_popped),'nodes popped off the queue.')
print ('Space performance:', str(queue_max_length),'nodes in the queue at its max.')
print ('Time spent: %0.2fs' % (time.time()-start))
return True
else:
# see if moving upper tile down is a valid move
if current_node.try_move_down():
new_state,up_value = current_node.try_move_down()
# check if the resulting node is already visited
if tuple(new_state.reshape(1,9)[0]) not in visited:
path_cost=current_path_cost+up_value
depth = current_depth+1
# get heuristic cost
h_cost = self.get_h_cost(new_state,goal_state,heuristic_function,path_cost,depth)
# create a new child node
total_cost = path_cost+h_cost
current_node.move_down = Node(state=new_state,parent=current_node,action='down',depth=depth,\
step_cost=up_value,path_cost=path_cost,heuristic_cost=h_cost)
queue.append((current_node.move_down, total_cost))
depth_queue.append((depth, total_cost))
path_cost_queue.append((path_cost, total_cost))
# see if moving left tile to the right is a valid move
if current_node.try_move_right():
new_state,left_value = current_node.try_move_right()
# check if the resulting node is already visited
if tuple(new_state.reshape(1,9)[0]) not in visited:
path_cost=current_path_cost+left_value
depth = current_depth+1
# get heuristic cost
h_cost = self.get_h_cost(new_state,goal_state,heuristic_function,path_cost,depth)
# create a new child node
total_cost = path_cost+h_cost
current_node.move_right = Node(state=new_state,parent=current_node,action='right',depth=depth,\
step_cost=left_value,path_cost=path_cost,heuristic_cost=h_cost)
queue.append((current_node.move_right, total_cost))
depth_queue.append((depth, total_cost))
path_cost_queue.append((path_cost, total_cost))
# see if moving lower tile up is a valid move
if current_node.try_move_up():
new_state,lower_value = current_node.try_move_up()
# check if the resulting node is already visited
if tuple(new_state.reshape(1,9)[0]) not in visited:
path_cost=current_path_cost+lower_value
depth = current_depth+1
# get heuristic cost
h_cost = self.get_h_cost(new_state,goal_state,heuristic_function,path_cost,depth)
# create a new child node
total_cost = path_cost+h_cost
current_node.move_up = Node(state=new_state,parent=current_node,action='up',depth=depth,\
step_cost=lower_value,path_cost=path_cost,heuristic_cost=h_cost)
queue.append((current_node.move_up, total_cost))
depth_queue.append((depth, total_cost))
path_cost_queue.append((path_cost, total_cost))
# see if moving right tile to the left is a valid move
if current_node.try_move_left():
new_state,right_value = current_node.try_move_left()
# check if the resulting node is already visited
if tuple(new_state.reshape(1,9)[0]) not in visited:
path_cost=current_path_cost+right_value
depth = current_depth+1
# get heuristic cost
h_cost = self.get_h_cost(new_state,goal_state,heuristic_function,path_cost,depth)
# create a new child node
total_cost = path_cost+h_cost
current_node.move_left = Node(state=new_state,parent=current_node,action='left',depth=depth,\
step_cost=right_value,path_cost=path_cost,heuristic_cost=h_cost)
queue.append((current_node.move_left, total_cost))
depth_queue.append((depth, total_cost))
path_cost_queue.append((path_cost, total_cost))
# test = np.array([1,2,3,8,6,4,7,5,0]).reshape(3,3)
# easy = np.array([1,3,4,8,6,2,7,0,5]).reshape(3,3)
# medium = np.array([2,8,1,0,4,3,7,6,5]).reshape(3,3)
# hard = np.array([5,6,7,4,0,8,3,2,1]).reshape(3,3)
initial_state = np.array([0,1,2,3,4,5,6,7,8]).reshape(3,3)
# final = np.array([5,1,0,4,6,7,3,8,2]).reshape(3,3)
goal_state = np.array([5,1,0,4,6,7,3,8,2]).reshape(3,3)
print (initial_state,'\n')
print (goal_state)
root_node = Node(state=initial_state,parent=None,action=None,depth=0,step_cost=0,path_cost=0,heuristic_cost=0)
# search level by level with queue
# root_node.breadth_first_search(goal_state)
# # search as far as possible along each branch before backtracking, using stack
# root_node.depth_first_search(goal_state)
# # combines both BFS and DFS, perform DFS one level deeper at a time, using looping stack
# root_node.iterative_deepening_DFS(goal_state)
# # search based on path cost, using priority queue
# root_node.uniform_cost_search(goal_state)
# # search based on num_misplaced heuristic cost, using priority queue
# root_node.best_first_search(goal_state)
# # A*1 search based on path cost+heuristic cost, using priority queue
root_node.a_star_search(goal_state,heuristic_function = 'num_misplaced')
# # A*2 search based on path cost+heuristic cost, using priority queue
# root_node.a_star_search(goal_state,heuristic_function = 'manhattan')
# A*3 search based on path cost+heuristic cost, using priority queue
# No.1 fast
# root_node.a_star_search(goal_state,heuristic_function = 'fair_manhattan')