-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathFlahertyJeanWorldMapNavigator.py
769 lines (731 loc) · 41 KB
/
FlahertyJeanWorldMapNavigator.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
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
###########################################################################
# World Map Navigator #
# #
# Programmed by Jean Flaherty (12-03-2016) #
# Instructor: Dean Zeller #
# #
# Search algorithms adapted from: #
# MIT OCW 6.01SC Lecture 12 by Leslie Kaelbling #
# MIT OCW 6.01SC Lecture 13 by Leslie Kaelbling #
# MIT OCW 6.034 Lecture 4 by Patrick H. Winston # #
# #
# #
# Description: The file contains code for search algorithms, animating #
# the search process, and mapping out nodes on the world #
# map. #
# #
# Objects: #
# #
# SearchNode An object that represents a node with info about #
# how the node was found by a search algorithm. #
# #
# Stack A collection data type that adds items to the end #
# of the collection and removes them from the #
# end as well. Last in first out. #
# #
# Queue A collection data type that adds items to the end #
# of the collection and removes them from the #
# front. First in first out. #
# #
# SearchAlgorithm This class defines the different types of search #
# algorithms that are implemented as an #
# enumeration. It can even be converted to a #
# string with str(). #
# #
# SearchResult A named tuple that is used for the return value #
# of the search algorithms. #
# #
# Navigator Handles defining the web of nodes, drawing a map #
# of the nodes, using search algoritms, and #
# animating the process of finding a path with #
# search algorithms. #
# #
# This program is copyright (c) 2016 Jean Flaherty and Dean Zeller. #
# All rights reserved. Permission granted to use and modify for #
# educational purposes only. Any commercial use of this code must #
# receive permission from the author(s). #
###########################################################################
from tkinter import *
import math
import datetime
from collections import namedtuple
from enum import Enum
from FlahertyJeanWorldMap import world_map
from FlahertyJeanAnimation import Animation
###########################################################################
# SearchNode: an object that represents a node with info about how the #
# node was found. #
# #
# Adapted from: MIT OCW 6.01SC Lecture 12 by Leslie Kaelbling # #
# #
# #
# Parameters: #
# action how the node relates to its parent. #
# which action was taken from the parent node. #
# state the identity or name of the node. #
# parent the parent of the search node. #
# #
###########################################################################
class SearchNode:
#######################################################################
# Initialize search node. #
#######################################################################
def __init__ (self, action, state, parent, actionCost=0):
self.action = action
self.state = state
self.parent = parent
if self.parent:
self.cost = self.parent.cost + actionCost
else:
self.cost = actionCost
#######################################################################
# Path: Returns path of the search node. #
#######################################################################
def path(self):
if self.parent == None: return [(self.action, self.state)]
else: return self.parent.path() + [(self.action, self.state)]
#######################################################################
# InPath: checks if state is in path. So we can avoid going to the #
# same node twice. #
#######################################################################
def inPath(self, state):
if self.state == state:
return True
elif self.parent == None:
return False
else:
return self.parent.inPath(state)
###########################################################################
# Stack: a collection data type that adds items to the end of the #
# collection and removes them from the end as well. Last in first out. #
# #
# Adapted from: MIT OCW 6.01SC Lecture 12 by Leslie Kaelbling # #
# # #
###########################################################################
class Stack:
def __init__(self):
self.data = []
def push(self, item):
self.data.append(item)
def pop(self):
return self.data.pop()
def empty(self):
return self.data is []
###########################################################################
# Queue: a collection data type that adds items to the end of the #
# collection and removes them from the front. First in first out. #
# #
# Adapted from: MIT OCW 6.01SC Lecture 12 by Leslie Kaelbling # #
# # # #
###########################################################################
class Queue:
def __init__(self):
self.data = []
def push(self, item):
self.data.append(item)
def pop(self):
return self.data.pop(0)
def empty(self):
return self.data is []
###########################################################################
# Priority Queue: a collection data type that adds items to the end of the#
# collection and removes the lowest cost items first. #
# #
# Adapted from: MIT OCW 6.01SC Lecture 13 by Leslie Kaelbling # #
# # #
###########################################################################
class PriorityQueue:
def __init__(self):
self.data = []
def push(self, item, cost):
self.data.append((cost, item))
def pop(self):
index = self.cheapItemIndex()
return self.data.pop(index)[1] # just return the data item
def empty(self):
return len(self.data) == 0
def cheapItemIndex(self):
result = 0; lowestCost = self.data[0][0]
for i in range(len(self.data)):
(cost, item) = self.data[i]
if cost < lowestCost:
result, lowestCost = i, cost
return result
###########################################################################
# SearchAlgorithm : This class defines the different types of search #
# algorithms that are implemented as an enumeration. It #
# can even be converted to a string with str(). #
###########################################################################
class SearchAlgorithm(Enum):
DEPTH_FIRST = 0
DEPTH_FIRST_DYNAMIC = 1
BREADTH_FIRST = 2
BREADTH_FIRST_DYNAMIC = 3
HILL_CLIMBING = 4
UNIFORM_COST = 5
UNIFORM_COST_DYNAMIC = 6
A_STAR = 7
# this allows the class to support str() converting
def __str__(self):
if self == SearchAlgorithm.DEPTH_FIRST:
return "Depth First"
elif self == SearchAlgorithm.DEPTH_FIRST_DYNAMIC:
return "Depth First Dynamic"
elif self == SearchAlgorithm.BREADTH_FIRST:
return "Breadth First"
elif self == SearchAlgorithm.BREADTH_FIRST_DYNAMIC:
return "Breadth First Dynamic"
elif self == SearchAlgorithm.HILL_CLIMBING:
return "Hill Climbing"
elif self == SearchAlgorithm.UNIFORM_COST:
return "Uniform Cost"
elif self == SearchAlgorithm.UNIFORM_COST_DYNAMIC:
return "Uniform Cost Dynamic"
elif self == SearchAlgorithm.A_STAR:
return "A*"
###########################################################################
# SearchResult: A named tuple that is used for the return value of the #
# search algorithms. #
# #
# path -- the path that the search function found. #
# log -- a list of nodes that were considered before finding the path. # #
# #
###########################################################################
SearchResult = namedtuple('SearchResult', 'path log')
###########################################################################
# SearchDepthFirst : The depth first algorithm picks one direction and #
# sticks to it until it meets a dead end. Slowly it #
# works backwards until it figures out what went wrong. #
# #
# Adapted from: MIT OCW 6.01SC Lecture 12 by Leslie Kaelbling # #
# #
# parameters: #
# initialState -- the starting point of the search #
# goalTest -- a function that takes a state and returns 1 if it is #
# the state that is desired, otherwise returns 0 #
# successors -- a dictionary with all states as a keys and and a #
# list of connecting states as the value #
# successor -- a function that takes a state and an action (0,1,2..)#
# and returns another state, the successor #
# return value: a SearchResult named tupel with path and log values #
###########################################################################
def searchDepthFirst(initialState, goalTest, successors, successor):
initialNode = SearchNode(None, initialState, None)
# if we are already there look no further
if goalTest(initialState):
return SearchResult(initialNode.path, [initialNode])
agenda = Stack()
agenda.push(initialNode)
log = []
# keep looking until there is nothing to search
while not agenda.empty():
parent = agenda.pop()
newChildStates = []
successors_count = len(successors[parent.state])
for a in range(successors_count):
newState = successor(parent.state, a)
newNode = SearchNode(a, newState, parent)
if goalTest(newState):
# found it!
log.append(newNode)
return SearchResult(newNode.path(), log)
elif newState in newChildStates: # pruning rule 2
pass
elif parent.inPath(newState): # pruning rule 1
pass
else:
log.append(newNode)
newChildStates.append(newState)
agenda.push(newNode)
return SearchResult(None, log)
###########################################################################
# SearchDepthFirstAvoidVisited : The depth first algorithm picks one #
# direction and sticks to it until it meets a dead end. Slowly it #
# works backwards until it figures out what went wrong. #
# #
# Adapted from: MIT OCW 6.01SC Lecture 12 by Leslie Kaelbling # #
# #
# parameters: #
# initialState -- the starting point of the search #
# goalTest -- a function that takes a state and returns 1 if it is #
# the state that is desired, otherwise returns 0 #
# successors -- a dictionary with all states as a keys and and a #
# list of connecting states as the value #
# successor -- a function that takes a state and an action (0,1,2..)#
# and returns another state, the successor #
# return value: a SearchResult named tupel with path and log values #
###########################################################################
def searchDepthFirstAvoidVisited(initialState, goalTest, successors, successor):
initialNode = SearchNode(None, initialState, None)
# if we are already there look no further
if goalTest(initialState):
return SearchResult(initialNode.path, [initialNode])
agenda = Stack()
agenda.push(initialNode)
visited = {initialState: True}
log = []
while not agenda.empty():
parent = agenda.pop()
successors_count = len(successors[parent.state])
for a in range(successors_count):
newState = successor(parent.state, a)
newNode = SearchNode(a, newState, parent)
if goalTest(newState):
# found it!
log.append(newNode)
return SearchResult(newNode.path(), log)
elif newState in visited: # pruning rule 1,2,3
pass
else:
log.append(newNode)
visited[newState] = True
agenda.push(newNode)
return SearchResult(None, log)
###########################################################################
# SearchBreadthFirst : The breadth first algorithm checks level by level #
# starting by searching all nodes one node away then two nodes away #
# and keeps going untill the goal is reached. #
# #
# Adapted from: MIT OCW 6.01SC Lecture 12 by Leslie Kaelbling # #
# #
# parameters: #
# initialState -- the starting point of the search #
# goalTest -- a function that takes a state and returns 1 if it is #
# the state that is desired, otherwise returns 0 #
# successors -- a dictionary with all states as a keys and and a #
# list of connecting states as the value #
# successor -- a function that takes a state and an action (0,1,2..)#
# and returns another state, the successor #
# return value: a SearchResult named tupel with path and log values #
###########################################################################
def searchBreadthFirst(initialState, goalTest, successors, successor):
initialNode = SearchNode(None, initialState, None)
# if we are already there look no further
if goalTest(initialState):
return SearchResult(initialNode.path, [initialNode])
agenda = Queue()
agenda.push(initialNode)
log = []
while not agenda.empty():
parent = agenda.pop()
newChildStates = []
successors_count = len(successors[parent.state])
for a in range(successors_count):
newState = successor(parent.state, a)
newNode = SearchNode(a, newState, parent)
if goalTest(newState):
# found it!
log.append(newNode)
return SearchResult(newNode.path(), log)
elif newState in newChildStates: # pruning rule 2
pass
elif parent.inPath(newState): # pruning rule 1
pass
else:
log.append(newNode)
newChildStates.append(newState)
agenda.push(newNode)
return SearchResult(None, log)
###########################################################################
# SearchBreadthFirstDynamicProgramming : The dynamic breadth first #
# algorithm does what the breadth first algorithm does without #
# repeating nodes that have already been checked. This speeds #
# things up immensely. #
# #
# Adapted from: MIT OCW 6.01SC Lecture 12 by Leslie Kaelbling # #
# #
# parameters: #
# initialState -- the starting point of the search #
# goalTest -- a function that takes a state and returns 1 if it is #
# the state that is desired, otherwise returns 0 #
# successors -- a dictionary with all states as a keys and and a #
# list of connecting states as the value #
# successor -- a function that takes a state and an action (0,1,2..)#
# and returns another state, the successor #
# return value: a SearchResult named tupel with path and log values #
###########################################################################
def searchBreadthFirstDynamicProgramming(initialState, goalTest, successors, successor):
agenda = Queue()
if goalTest(initialState):
return [(None, initialState)]
agenda.push(SearchNode(None, initialState, None))
visited = {initialState: True}
log = []
while not agenda.empty():
parent = agenda.pop()
successors_count = len(successors[parent.state])
for a in range(successors_count):
newState = successor(parent.state, a)
newNode = SearchNode(a, newState, parent)
if goalTest(newState):
# found it!
log.append(newNode)
return SearchResult(newNode.path(), log)
elif newState in visited: # rules 1, 2, 3
pass
else:
log.append(newNode)
visited[newState] = True
agenda.push(newNode)
return SearchResult(None, log)
###########################################################################
# SearchHillClimbing : The hill climbing algorithm compares each adjacent #
# node to see which one is closer to our goal effectively avoiding #
# searching in the wrong direction. Unfortunately sometimes it #
# reaches a local minima and assumes it can't go further. #
# #
# Adapted from: MIT OCW 6.034 Lecture 4 -- Patrick H. Winston #
# # #
# parameters: #
# initialState -- the starting point of the search #
# goalTest -- a function that takes a state and returns 1 if it is #
# the state that is desired, otherwise returns 0 #
# successors -- a dictionary with all states as a keys and and a #
# list of connecting states as the value #
# successor -- a function that takes a state and an action (0,1,2..)#
# and returns another state, the successor #
# better -- a function that takes two nodes and returnes the node #
# that is "better" or is closer to your goal. #
# return value: a SearchResult named tupel with path and log values #
###########################################################################
def searchHillClimbing(initialState, goalTest, successors, successor, better):
agenda = Stack()
if goalTest(initialState):
return [(None, initialState)]
agenda.push(SearchNode(None, initialState, None))
log = []
while not agenda.empty():
parent = agenda.pop()
newChildStates = []
successors_count = len(successors[parent.state])
closestNode = parent
for a in range(successors_count):
newState = successor(parent.state, a)
newNode = SearchNode(a, newState, parent)
if goalTest(newState):
# found it!
log.append(newNode)
return SearchResult(newNode.path(), log)
elif newState in newChildStates: # pruning rule 2
pass
elif parent.inPath(newState): # pruning rule 1
pass
else:
log.append(newNode)
closestNode = better(newNode, closestNode)
if closestNode == parent:
log.append(parent)
return SearchResult(parent.path(), log)
agenda.push(closestNode)
return SearchResult(None, log)
###########################################################################
# SearchUniformCost: The uniform cost algorithm prioritizes low cost paths#
# #
# Adapted from: MIT OCW 6.01SC Lecture 13 by Leslie Kaelbling # #
# #
# parameters: #
# initialState -- the starting point of the search #
# goalTest -- a function that takes a state and returns 1 if it is #
# the state that is desired, otherwise returns 0 #
# successors -- a dictionary with all states as a keys and and a #
# list of connecting states as the value #
# successorAndCost -- a function that takes a state and an action #
# and returnscthe successor and the cost of the action #
# return value: a SearchResult named tupel with path and log values #
###########################################################################
def searchUniformCost(initialState, goalTest, successors, successorAndCost):
startNode = SearchNode(None, initialState, None, 0)
agenda = PriorityQueue()
if goalTest(initialState):
return [(None, initialState)]
agenda.push(startNode, 0)
log = []
while not agenda.empty():
parent = agenda.pop()
successors_count = len(successors[parent.state])
if goalTest(parent.state):
log.append(parent)
return SearchResult(parent.path(), log)
for a in range(successors_count):
(newState, cost) = successorAndCost(parent.state, a)
if not parent.inPath(newState):
newNode = SearchNode(a, newState, parent, cost)
log.append(newNode)
agenda.push(newNode, newNode.cost)
return SearchResult(None, log)
###########################################################################
# SearchUniformCostDynamic: The uniform cost algorithm prioritizes low #
# cost paths. It will keep track of states that it already expanded.# #
# #
# Adapted from: MIT OCW 6.01SC Lecture 13 by Leslie Kaelbling # #
# #
# parameters: #
# initialState -- the starting point of the search #
# goalTest -- a function that takes a state and returns 1 if it is #
# the state that is desired, otherwise returns 0 #
# successors -- a dictionary with all states as a keys and and a #
# list of connecting states as the value #
# successorAndCost -- a function that takes a state and an action #
# and returnscthe successor and the cost of the action #
# return value: a SearchResult named tupel with path and log values #
###########################################################################
def searchUniformCostDynamic(initialState, goalTest, successors, successorAndCost):
startNode = SearchNode(None, initialState, None, 0)
agenda = PriorityQueue()
if goalTest(initialState):
return [(None, initialState)]
agenda.push(startNode, 0)
expanded = {}
log = []
while not agenda.empty():
parent = agenda.pop()
if not parent.state in expanded:
expanded[parent.state] = True
successors_count = len(successors[parent.state])
if goalTest(parent.state):
log.append(parent)
return SearchResult(parent.path(), log)
for a in range(successors_count):
(newState, cost) = successorAndCost(parent.state, a)
if not newState in expanded:
newNode = SearchNode(a, newState, parent, cost)
log.append(newNode)
agenda.push(newNode, newNode.cost)
return SearchResult(None, log)
###########################################################################
# SearchAStar: The uniform cost algorithm prioritizes low #
# cost paths. It will keep track of states that it already expanded.# #
# #
# Adapted from: MIT OCW 6.01SC Lecture 13 by Leslie Kaelbling # #
# #
# parameters: #
# initialState -- the starting point of the search #
# goalTest -- a function that takes a state and returns 1 if it is #
# the state that is desired, otherwise returns 0 #
# successors -- a dictionary with all states as a keys and and a #
# list of connecting states as the value #
# successorAndCost -- a function that takes a state and an action #
# and returnscthe successor and the cost of the action #
# heuristic -- a function that estimates the cheapest remaining #
# cost of the path between a node and the final node #
# return value: a SearchResult named tupel with path and log values #
###########################################################################
def searchAStar(initialState, goalTest, successors, successorAndCost, heuristic):
startNode = SearchNode(None, initialState, None, 0)
if goalTest(initialState):
return [(None, initialState)]
agenda = PriorityQueue()
agenda.push(startNode, 0)
expanded = {}
log = []
while not agenda.empty():
parent = agenda.pop()
if not parent.state in expanded:
expanded[parent.state] = True
successors_count = len(successors[parent.state])
if goalTest(parent.state):
log.append(parent)
return SearchResult(parent.path(), log)
for a in range(successors_count):
(newState, cost) = successorAndCost(parent.state, a)
if not newState in expanded:
newNode = SearchNode(a, newState, parent, cost)
log.append(newNode)
#print("S:{} H:{} C:{}".format(newState, heuristic(newState), newNode.cost))
agenda.push(newNode, newNode.cost + heuristic(newState))
return SearchResult(None, log)
###########################################################################
# Navigator : this class handles defining the web of nodes, drawing a map #
# of the nodes, using search algoritms, and animating the process #
# of finding a path with search algorithms. # #
###########################################################################
class Navigator:
# define default values
initialState = "Colorado"
finalState = "Japan"
searchAlgorithm = SearchAlgorithm.DEPTH_FIRST
# define the connections between nodes
successors = {
"Anchorage" : ["Vancouver", "Quebec"],
"Bangkok" : ["China", "Darwin"],
"Cape Horn" : ["Machu Picchu", "Rio"],
"China" : ["Bangkok", "East Russia", "Mongolia", "Mumbai"],
"Colombia" : ["Machu Picchu", "Mexico", "Rio"],
"Colorado" : ["New York", "San Francisco"],
"Darwin" : ["Bangkok","Perth", "Sydney"],
"East Russia" : ["China", "Mongolia","Moscow"],
"Egypt" : ["Morrocco", "South Africa", "Nigeria", "Israel"],
"Greenland" : ["Paris", "Quebec"],
"Israel" : ["Italy", "Egypt", "Mongolia", "Mumbai"],
"Italy" : ["Paris", "Spain", "Moscow", "Israel"],
"Japan" : ["Sydney"],
"Machu Picchu" : ["Cape Horn", "Colombia"],
"Mexico" : ["San Francisco", "Colombia"],
"Mongolia" : ["Moscow", "Israel", "China", "East Russia"],
"Morrocco" : ["Spain", "Nigeria", "Egypt"],
"Moscow" : ["Paris", "Italy", "Mongolia", "East Russia"],
"Mumbai" : ["Israel", "China"],
"New York" : ["Colorado", "Quebec"],
"Nigeria" : ["Rio", "South Africa", "Morrocco", "Egypt"],
"Paris" : ["Spain", "Greenland", "Italy", "Moscow"],
"Perth" : ["Darwin", "Sydney"],
"Quebec" : ["Anchorage", "New York", "Greenland", "Vancouver"],
"Rio" : ["Cape Horn", "Nigeria", "Colombia"],
"San Francisco" : ["Vancouver", "Colorado", "Mexico"],
"South Africa" : ["Nigeria", "Egypt"],
"Spain" : ["Italy", "Paris", "Morrocco"],
"Sydney" : ["Perth", "Darwin", "Japan"],
"Vancouver" : ["Anchorage", "San Francisco", "Quebec"]
}
# define the coordinates of the nodes
coordinates = {
"Anchorage" : (15,107),
"Bangkok" : (335,203),
"Cape Horn" : (100,320),
"China" : (315,153),
"Colombia" : (85,210),
"Colorado" : (56,149),
"Darwin" : (375,255),
"East Russia" : (370,100),
"Egypt" : (232,190),
"Greenland" : (141,105),
"Israel" : (245,167),
"Italy" : (215,141),
"Japan" : (367,155),
"Machu Picchu" : (93,260),
"Mexico" : (58,194),
"Mongolia" : (310,123),
"Morrocco" : (180,167),
"Moscow" : (255,103),
"Mumbai" : (291,180),
"New York" : (90,145),
"Nigeria" : (205,210),
"Paris" : (195,130),
"Perth" : (348,285),
"Quebec" : (105,115),
"Rio" : (133,240),
"San Francisco" : (32,160),
"South Africa" : (213,290),
"Spain" : (186,147),
"Sydney" : (395,280),
"Vancouver" : (33,130),
}
#######################################################################
# Search: applies the correct search algorithm specified by the #
# searchAlgorithm attribute. Uses initialState and finalState #
# attributes of the Navigator object to use for parameters of #
# the search algorithms. Returns search result # #
#######################################################################
def search(self):
# create the functions to use as parameters of the search algorithm
###################################################################
# goalTest: return true if state is the final state #
###################################################################
def goalTest(state):
return state==self.finalState
###################################################################
# distance: calculates the distance between node and goal #
###################################################################
def distance(state1, state2):
x1 = self.coordinates[state1][0]
y1 = self.coordinates[state1][1]
x2 = self.coordinates[state2][0]
y2 = self.coordinates[state2][1]
# calculate distance for x and y axis
dx = abs(x1 - x2)
dy = abs(y1 - y2)
# use the pythagorean theorem
return math.sqrt(dx*dx+dy*dy)
###################################################################
# better: takes two nodes and returns the node closer to goal #
###################################################################
def better(node1, node2):
# compare the two distanced and return closer node
if distance(node1.state, self.finalState) < distance(node2.state,self.finalState):
return node1
else:
return node2
###################################################################
# successor: return state or "neighbor" in specified "direction" #
# or action. #
###################################################################
def successor(state, action):
if action < len(self.successors[state]):
return self.successors[state][action]
else:
return state
def successorAndCost(state, action):
if action < len(self.successors[state]):
successor = self.successors[state][action]
cost = distance(successor, state)
return (successor, cost)
else:
return (state, 0)
def heuristic(state):
return distance(state, self.finalState)
# use the correct algorithm and return search result
if self.searchAlgorithm == SearchAlgorithm.DEPTH_FIRST:
return searchDepthFirst(self.initialState, goalTest, self.successors, successor)
elif self.searchAlgorithm == SearchAlgorithm.DEPTH_FIRST_DYNAMIC:
return searchDepthFirstAvoidVisited(self.initialState, goalTest, self.successors, successor)
elif self.searchAlgorithm == SearchAlgorithm.BREADTH_FIRST:
return searchBreadthFirst(self.initialState, goalTest, self.successors, successor)
elif self.searchAlgorithm == SearchAlgorithm.BREADTH_FIRST_DYNAMIC:
return searchBreadthFirstDynamicProgramming(self.initialState, goalTest, self.successors, successor)
elif self.searchAlgorithm == SearchAlgorithm.HILL_CLIMBING:
return searchHillClimbing(self.initialState, goalTest, self.successors, successor, better)
elif self.searchAlgorithm == SearchAlgorithm.UNIFORM_COST:
return searchUniformCost(self.initialState, goalTest, self.successors, successorAndCost)
elif self.searchAlgorithm == SearchAlgorithm.UNIFORM_COST_DYNAMIC:
return searchUniformCostDynamic(self.initialState, goalTest, self.successors, successorAndCost)
elif self.searchAlgorithm == SearchAlgorithm.A_STAR:
return searchAStar(self.initialState, goalTest, self.successors, successorAndCost, heuristic)
#######################################################################
# AnimateSearchLog: animates the process that the search algorithm #
# takes to find the path. #
#######################################################################
def animateSearchLog(self, c, log, completion, fpms=50):
animation = Animation(c)
def logAnimation(i):
self.drawSearchNode(c, log[i])
animation.timeline = logAnimation
animation.frame_count = len(log)
animation.fpms = fpms
animation.completion = completion
animation.play()
#######################################################################
# CordinatesFromPath: returns a list of coordinates that define the #
# path. #
#######################################################################
def coordinatesFromPath(self, path):
resultCoordinates = []
for (action, state) in path:
x = self.coordinates[state][0]
y = self.coordinates[state][1]
coordinate = (x,y)
resultCoordinates.append(coordinate)
return resultCoordinates
#######################################################################
# DrawSearchNode: draws the path of the search node. # #
#######################################################################
def drawSearchNode(self, c, node):
coordinates = self.coordinatesFromPath(node.path())
c.delete("node")
c.create_line(coordinates, fill="red", tag="node")
#######################################################################
# DrawNetwork: draws all nodes and the connections between them. # #
#######################################################################
def drawNetwork(self, c, tag="network"):
for key, value in self.coordinates.items():
# get the coordinate values
x = value[0]
y = value[1]
# draw a dot for the node
c.create_oval(x-1, y-1, x+1, y+1, fill="#500", outline="#500")
# loop through all successors or "neighbors"
for successor in self.successors[key]:
# get the cordinate values of the "neighbors"
x2 = self.coordinates[successor][0]
y2 = self.coordinates[successor][1]
# draw the line connecting the node with its "neighbors"
c.create_line(x, y, x2, y2, fill="#500")