-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathapplication.py
205 lines (168 loc) · 8.52 KB
/
application.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
from __future__ import print_function
__author__ = 'Kingsley Biney'
try:
from BinaryTree import BinaryTree
from plot_utils import Plot_utils as plutils
from maps import Maps
from datetime import datetime
from data_structs import Node
except ImportError:
msg = "Unable to import some packages"
raise ImportError(msg)
'''
This class provides a high level implementation of the methods in plot_utils class
It contains three main methods:
* create_extended_map -> to create the complete graph to be traversed
* gui_taget -> provides call for gui visualization and interaction
* console_target -> provides call console visualization and interaction
Other available methods are the disp_in_order, disp_pre_order and disp_post_order
'''
class Search:
def __init__(self):
self.x_list = []
self.y_list = []
self.labels = []
self.tree = BinaryTree()
self.maps = Maps(x_list=self.x_list, y_list=self.y_list, labels=self.labels, tree=self.tree)
self.graph = None
self.number_path = None
def dis_in_order(self):
self.x_list, self.y_list, self.labels, self.tree = self.maps.original_map()
print(self.tree.displayInOrder(self.tree.root))
def dis_pre_order(self):
self.x_list, self.y_list, self.labels, self.tree = self.maps.original_map()
self.tree.displayPreOrder(self.tree.root)
def dis_post_order(self):
self.x_list, self.y_list, self.labels, self.tree = self.maps.original_map()
self.tree.displayPostOrder(self.tree.root)
# wrapper around the A* and Greedy Depth First Search method
def isa(self, searchKey=None, algorithm=None):
for i in range(len(self.labels)):
if searchKey == self.labels[i]:
x_coord_holder = self.x_list[i]
y_coord_holder = self.y_list[i]
# print('isa x: ' + str(x_coord_holder))
if algorithm == 'gbfs':
path = self.tree.gbfs(searchKey=searchKey, x=x_coord_holder, y=y_coord_holder)
elif algorithm == 'asts':
path = self.tree.asts(searchKey=searchKey, x=x_coord_holder, y=y_coord_holder)
return path
# Implement Uninformed Blind Search Algorithms on GUI
def gui_target(self, map=None, algorithm=None, goal=None):
'''
Arguments:
algorithm (string): the search algorithm code to be used
goal (string): the city (Node) being searched for (goal state)
Returns:
numpath (int): the number of Nodes visited by the algorithm in use
x_capa (int list): all the x coordinates of the Nodes in the tree
y_capa (int list): all the y coordinates of the Nodes in the tree
x_capitula (int list): all the x coordinates of the Nodes visited by algorithm in use
y_capitula (int list): all the y coordinates of the Nodes visited by algorithm in use
labella (string list): all the names of the Nodes visited by algorithm in use
'''
# clear the canvas
self.maps.reset()
# create the binary tree to be used
if map == 'original':
self.x_list, self.y_list, self.labels, self.tree = self.maps.original_map()
elif map == 'extended':
self.x_list, self.y_list, self.labels, self.tree = self.maps.extended_map()
# instantiate the plot_utils class
pltls = plutils(tree=self.tree, x_list=self.x_list, y_list=self.y_list, labels=self.labels)
if algorithm == 'ucs':
start = datetime.now().microsecond
path = self.tree.ucs(goal)
stop = datetime.now().microsecond
time_taken = stop - start
print('Nodes Traversed in uniform cost: ', end='\t')
for i in range(len(path)):
print(path[i].getKey(), end='\t')
print('\n')
numpath, x_capa, y_capa, x_capitula, y_capitula, labella = pltls.ubs_plotter(path=path, algorithm='ucs')
elif algorithm == 'dfs':
start = datetime.now().microsecond
path = self.tree.dfs(goal)
stop = datetime.now().microsecond
time_taken = stop - start
print('Nodes Traversed in depth first: ')
for i in range(len(path)):
print(path[i].getKey(), end='\t')
print('\n')
numpath, x_capa, y_capa, x_capitula, y_capitula, labella = pltls.ubs_plotter(path=path, algorithm='dfs')
elif algorithm == 'bfs':
start = datetime.now().microsecond
path = self.tree.bfs(goal)
stop = datetime.now().microsecond
time_taken = stop - start
print('Nodes Traversed in breadth first: ')
for i in range(len(path)):
print(path[i].getKey(), end='\t')
print('\n')
numpath, x_capa, y_capa, x_capitula, y_capitula, labella = pltls.ubs_plotter(path=path, algorithm='bfs')
elif algorithm == 'gbfs':
start = datetime.now().microsecond
path = self.isa(searchKey=goal, algorithm=algorithm)
stop = datetime.now().microsecond
time_taken = stop - start
print('Nodes Traversed in greedy best first: ')
for i in range(len(path)):
print(path[i].getKey(), end='\t')
print('\n')
numpath, x_capa, y_capa, x_capitula, y_capitula, labella = pltls.ubs_plotter(path=path, algorithm='gbfs')
elif algorithm == 'asts':
start = datetime.now().microsecond
path = self.isa(searchKey=goal, algorithm=algorithm)
stop = datetime.now().microsecond
time_taken = stop - start
print('Nodes Traversed in a star best first: ')
for i in range(len(path)):
print(path[i].getKey(), end='\t')
print('\n')
numpath, x_capa, y_capa, x_capitula, y_capitula, labella = pltls.ubs_plotter(path=path, algorithm='asts')
# return the necessary variables
return time_taken, numpath, x_capa, y_capa, x_capitula, y_capitula, labella
# Implement Uninformed Blind Search Algorithms on console
def console_target(self):
print('Implemented algorithm codes: ucs, dfs, bfs, gbfs, asts')
map = raw_input('Map Selection: enter "original" or "extended": ').lower()
# clear the canvas
self.maps.reset()
# create the binary tree to be used
if map == 'original':
self.x_list, self.y_list, self.labels, self.tree = self.maps.original_map()
elif map == 'extended':
self.x_list, self.y_list, self.labels, self.tree = self.maps.extended_map()
# instantiate the plutils class
pltls = plutils(tree=self.tree, x_list=self.x_list, y_list=self.y_list, labels=self.labels, consoleMode=True)
while True:
goal = raw_input('Enter the name of the city: ')
algorithm = raw_input('Enter search algorithm code: ').lower()
if algorithm == 'ucs':
path = self.tree.ucs(goal)
num_path, _, _, _, _, _ = pltls.ubs_plotter(path=path, algorithm=algorithm)
print('number of Nodes visited: {}'.format(num_path))
elif algorithm == 'bfs':
path = self.tree.bfs(goal)
num_path = len(path)
print('number of Nodes visited: {}'.format(num_path))
pltls.ubs_plotter(path=path, algorithm=algorithm)
elif algorithm == 'dfs':
path = self.tree.dfs(goal)
num_path = len(path)
print('number of Nodes visited: {}'.format(num_path))
pltls.ubs_plotter(path=path, algorithm=algorithm)
elif algorithm == 'gbfs':
path = self.isa(searchKey=goal, algorithm=algorithm)
num_path = len(path)
print(path)
print('number of Nodes visited: {}'.format(num_path))
pltls.ubs_plotter(path=path, algorithm=algorithm)
elif algorithm == 'asts':
path = self.isa(searchKey=goal, algorithm=algorithm)
num_path = len(path)
print(path)
print('number of Nodes visited: {}'.format(num_path))
pltls.ubs_plotter(path=path, algorithm=algorithm)
else:
raise 'Invalid argument entered'