-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
480 lines (430 loc) · 17 KB
/
main.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
import tkinter
import tkinter as tk
from collections import defaultdict
from tkinter import ttk
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
import matplotlib.pyplot as plt
import networkx as nx
import heapq
class Graph:
def __init__(self):
self.graph = defaultdict(list)
self.actualValues = {} #Uniform Cost and ASTAR
self.huristic = {} #BEST FIRST SEARCh
self.huristicPlusCost={} #ASTAR
self.path=[]
self.found = False
self.parentList={}
self.graphList = []
self.forwardVisited = []
self.backwardVisited = []
self.forwardParentList = {}
self.backwardParentList = {}
self.edgeCostList = []
def addDirectedEdge(self,node,edge):
self.graph[node].append(edge)
def addEdge(self,node,edge):
self.graph[node].append(edge)
self.graph[edge].append(node)
def addGraphList(self,node):
self.graphList.append(node)
def addHuristic(self,node,hursitic):
self.huristic[node]=hursitic
def addCost(self,node1,node2,cost):
concat = (node1,node2)
self.actualValues[concat] = cost
concat = (node2, node1)
self.actualValues[concat] = cost
tupple = (node1,node2,cost)
self.edgeCostList.append(tupple)
def addDirectCost(self,node1,node2,cost):
concat = (node1, node2)
self.actualValues[concat] = cost
def printGraph(self):
print(self.graph)
print(self.actualValues)
print(self.graphList)
print(self.huristic)
# ---------------------------------------------------------------------------Implementing Breadth First Search----------------------------------------------------------------------------------------------
def BFS(self,start,goal):
if len(self.path) != 0:
self.path.clear()
Que = []
visited = []
Que.append(start)
Found = False
visited.append(start)
while Que:
element = Que.pop(0)
print(element)
self.path.append(element)
if element == goal:
self.found = True
break
for i in self.graph[element]:
if i not in visited:
visited.append(i)
Que.append(i)
print(self.path)
# ---------------------------------------------------------------------------Implementing Depth First Search----------------------------------------------------------------------------------------------
def DFS(self,start,goal):
if len(self.path) != 0:
self.path.clear()
Stack = []
visited = []
Stack.append(start)
Found = False
visited.append(start)
while Stack:
element = Stack.pop()
print(element)
self.path.append(element)
if element == goal:
self.found = True
break
for i in self.graph[element]:
if i not in visited:
visited.append(i)
Stack.append(i)
print(self.path)
# ---------------------------------------------------------------------------Implementing Depth Limited Search-------------------------------------------------------------------------------------
def DLS(self, start,goal,depthLimit=None):
if len(self.path) != 0:
self.path.clear()
if depthLimit is None:
depthLimit = 3
depthLimit = depthLimit
Stack = []
visited = []
Stack.append(start)
self.dict = {}
visited.append(start)
while Stack:
element = Stack.pop()
depth=0
tempElement = element
while tempElement != start:
if tempElement in self.dict:
tempElement = self.dict[tempElement]
depth = depth + 1
if depth > depthLimit:
continue
print(element)
self.path.append(element)
if element == goal:
self.found = True
break
for i in self.graph[element]:
if i not in visited:
visited.append(i)
Stack.append(i)
self.dict[i] = element
print(self.path)
# ---------------------------------------------------------------------------Implementing Iterative Deepning Search-------------------------------------------------------------------------------------
def IterativeDepeeningSearch(self, start, goal):
if len(self.path) != 0:
self.path.clear()
check = False
i = 1
while check == False:
check = self.DLS(start, goal, i)
i = i + 1
if check == True:
break
# ---------------------------------------------------------------------------Implementing Uniform Cost Search-------------------------------------------------------------------------------------------
def UCS(self, start, goal):
if len(self.path) != 0:
self.path.clear()
que = []
heapq.heappush(que, (0, start))
visited = []
while que:
cost, node = heapq.heappop(que)
self.path.append(node)
if node == goal:
self.found = True
break
visited.append(node)
for neighbour in self.graph[node]:
if neighbour not in visited:
concat = (node, neighbour)
newCost = cost + self.actualValues[concat]
heapq.heappush(que, (newCost, neighbour))
self.parentList[neighbour] = node
print(self.path)
print(self.parentList)
#---------------------------------------------------------------------------Implementing Best First Search----------------------------------------------------------------------------------------------
def findHeuristic(self, pqueu):
min = 10000
node = ""
for i in pqueu:
for key, value in self.huristic.items():
if i == key:
if value < min:
min = value
node = i
return node
def BestFirstSearch(self, s, goal):
if len(self.path) != 0:
self.path.clear()
Que = []
Que.append(s)
while Que:
elementToPop = self.findHeuristic(Que)
for i in range(len(Que)):
if elementToPop == Que[i]:
Que.pop(i)
break
if elementToPop == goal:
self.found= True
print(elementToPop)
self.path.append(elementToPop)
break;
print(elementToPop)
self.path.append(elementToPop)
for i in self.graph[elementToPop]:
Que.append(i)
# ---------------------------------------------------------------------------Implementing A* Search----------------------------------------------------------------------------------------------
def findHeuristicAndCost(self, pqueu):
min = 10000
node = ""
for i in pqueu:
for key, value in self.huristicPlusCost.items():
if i == key:
if value < min:
min = value
node = i
return node
def ASTAR(self, s, goal):
if len(self.path) != 0:
self.path.clear()
Que = []
Que.append(s)
visited =[]
visited.append(s)
cost = 0
while Que:
elementToPop = self.findHeuristic(Que)
for i in range(len(Que)):
if elementToPop == Que[i]:
Que.pop(i)
break
if elementToPop == goal:
self.found = True
self.path.append(elementToPop)
break
self.path.append(elementToPop)
for i in self.graph[elementToPop]:
Que.append(i)
visited.append(i)
self.parentList[i] = elementToPop
tempelement = i
while tempelement!=s:
parent = self.parentList[tempelement]
concat = (parent,tempelement)
cost += self.actualValues[concat]
tempelement = parent
huristic = self.huristic[i]
totalCost = cost+huristic
self.huristicPlusCost[i] = totalCost
totalCost = 0
huristic = 0
cost=0
print(self.huristicPlusCost[i])
# ---------------------------------------------------------------------------Implementing Bi-directional Search----------------------------------------------------------------------------------------------
def biDirectionalSearch(self, start, goal):
if len(self.path) != 0:
self.path.clear()
forwardQue = []
backwardQue = []
heapq.heappush(forwardQue, (0, start))
heapq.heappush(backwardQue, (0, goal))
self.forwardVisited.append(start)
self.backwardVisited.append(goal)
while forwardQue and backwardQue:
forwardCost, forwardNode = heapq.heappop(forwardQue)
backwardCost, backwardNode = heapq.heappop(backwardQue)
self.path.append(forwardNode)
self.path.append(backwardNode)
if forwardNode in self.backwardVisited:
self.found = True
self.path.remove(forwardNode)
self.path.remove(backwardNode)
self.path.extend(self.reversePath(forwardNode))
break
elif backwardNode in self.forwardVisited:
self.found = True
self.path.remove(forwardNode)
self.path.remove(backwardNode)
self.path.extend(self.reversePath(backwardNode))
break
self.forwardVisited.append(forwardNode)
self.backwardVisited.append(backwardNode)
for neighbour in self.graph[forwardNode]:
if neighbour not in self.forwardVisited:
concat = (forwardNode, neighbour)
newCost = forwardCost + 1
heapq.heappush(forwardQue, (newCost, neighbour))
self.forwardParentList[neighbour] = forwardNode
for neighbour in self.graph[backwardNode]:
if neighbour not in self.backwardVisited:
concat = (backwardNode, neighbour)
newCost = backwardCost + 1
heapq.heappush(backwardQue, (newCost, neighbour))
self.backwardParentList[neighbour] = backwardNode
print(self.path)
def reversePath(self, node):
path = []
while node:
path.append(node)
node = self.forwardParentList.get(node)
path.reverse()
return path
def printGraphList(self):
G = nx.Graph()
G.add_edges_from(self.graphList)
if self.found == True:
path = self.path
else:
path = []
root = tk.Tk()
root.title("Graph Visualization")
weights = self.actualValues
nx.set_edge_attributes(G, values=weights, name='weight')
fig = plt.figure(figsize=(5, 5))
canvas = FigureCanvasTkAgg(fig, master=root)
canvas.get_tk_widget().pack(side=tk.TOP, fill=tk.BOTH, expand=1)
pos = nx.spring_layout(G)
nx.draw(G, pos, with_labels=True)
path_edges = [(path[i], path[i + 1]) for i in range(len(path) - 1)]
edge_labels = dict([((u, v), d['weight']) for u, v, d in G.edges(data=True)])
nx.draw_networkx_edge_labels(G, pos, edge_labels=edge_labels, font_size=10)
nx.draw_networkx_edges(G, pos, edgelist=path_edges, edge_color='r', width=2)
for node in G.nodes():
x, y = pos[node]
plt.text(x, y - 0.09, s=str(self.huristic[node]), ha="center", fontsize=14)
canvas.draw()
root.mainloop()
def printDirectedGraphList(self):
G = nx.DiGraph()
G.add_edges_from(self.graphList)
if self.found == True:
path = self.path
else:
path = []
root = tk.Tk()
root.title("Graph Visualization")
weights = self.actualValues
nx.set_edge_attributes(G, values=weights, name='weight')
fig = plt.figure(figsize=(5, 5))
canvas = FigureCanvasTkAgg(fig, master=root)
canvas.get_tk_widget().pack(side=tk.TOP, fill=tk.BOTH, expand=1)
pos = nx.spring_layout(G)
nx.draw(G, pos, with_labels=True)
path_edges = [(path[i], path[i + 1]) for i in range(len(path) - 1)]
edge_labels = dict([((u, v), d['weight']) for u, v, d in G.edges(data=True)])
nx.draw_networkx_edge_labels(G, pos, edge_labels=edge_labels, font_size=10)
nx.draw_networkx_edges(G, pos, edgelist=path_edges, edge_color='r', width=2)
for node in G.nodes():
x, y = pos[node]
plt.text(x, y - 0.08, s=str(self.huristic[node]), ha="center", fontsize=14)
canvas.draw()
root.mainloop()
def DisplayUI():
graph = Graph()
def funcNodes():
node1 = Node1.get()
node2 = Node2.get()
tuple = (node1, node2)
edge = int(Node3.get())
if ComboBox1.get() == 'Undirected':
graph.addEdge(node1,node2)
graph.addCost(node1,node2,edge)
graph.addGraphList(tuple)
graph.printGraph()
ComboBox1.configure(state="disabled")
ComboBox1.configure(style="Locked.TCombobox")
else:
graph.addDirectedEdge(node1,node2)
graph.addDirectCost(node1,node2,edge)
graph.addGraphList(tuple)
graph.printGraph()
ComboBox1.configure(state="disabled")
ComboBox1.configure(style="Locked.TCombobox")
def getStartNodes():
startNode = StartNodeEntry.get()
goalNode = GoalNodeEntry.get()
if ComboBox.get() == 'BFS':
graph.BFS(startNode,goalNode)
elif ComboBox.get() == 'DFS':
graph.BFS(startNode, goalNode)
elif ComboBox.get() == 'Unifrom Cost Search':
graph.UCS(startNode,goalNode)
elif ComboBox.get() == 'Depth Limited':
graph.DLS(startNode, goalNode)
elif ComboBox.get() == 'Iterative Deepning':
graph.IterativeDepeeningSearch(startNode, goalNode)
elif ComboBox.get() == 'Bidrectional':
graph.biDirectionalSearch(startNode, goalNode)
elif ComboBox.get() == 'Best First':
graph.BestFirstSearch(startNode, goalNode)
else:
graph.ASTAR(startNode, goalNode)
if ComboBox1.get() == 'Undirected':
graph.printGraphList()
else:
graph.printDirectedGraphList()
def addHuristicToGraph():
node = Node.get()
nodeH = int(NodeH.get())
graph.addHuristic(node, nodeH)
graph.printGraph()
master = tk.Tk()
Frame = tkinter.Frame(master)
Frame.pack()
aiSearches = tkinter.LabelFrame(Frame,text="AI Seaches")
aiSearches.grid(row=0,column=0)
labelNode1 = tk.Label(aiSearches, text="Node 1")
labelNode1.grid(row=0,column=0)
Node1 = tk.Entry(aiSearches)
Node1.grid(row=1,column=0,padx=100)
labelNode2 = tk.Label(aiSearches, text="Node 2")
labelNode2.grid(row=2,column=0,pady=10)
Node2 = tk.Entry(aiSearches)
Node2.grid(row=3,column=0,padx=100,pady=0)
labelNode3 = tk.Label(aiSearches, text="Edge")
labelNode3.grid(row=4,column=0,pady=10)
Node3 = tk.Entry(aiSearches)
Node3.grid(row=5,column=0,padx=100,pady=0)
button = tk.Button(aiSearches, text="Add Nodes", command=funcNodes)
button.grid(row=6,column=0,pady=20)
labelNode = tk.Label(aiSearches, text="Node")
labelNode.grid(row=0,column=3)
Node = tk.Entry(aiSearches)
Node.grid(row=1,column=3,padx=100)
labelNodeH = tk.Label(aiSearches, text="Node Huristic")
labelNodeH.grid(row=2,column=3)
NodeH = tk.Entry(aiSearches)
NodeH.grid(row=3,column=3,padx=100,pady=10)
button3 = tk.Button(aiSearches, text="Add Node Huristic", command=addHuristicToGraph)
button3.grid(row=4, column=3)
StartNode = tk.Label(aiSearches, text="Start Node")
StartNode.grid(row=0,column=4)
StartNodeEntry = tk.Entry(aiSearches)
StartNodeEntry.grid(row=1,column=4,padx=100)
GoalNode = tk.Label(aiSearches, text="Goal Node")
GoalNode.grid(row=2,column=4)
GoalNodeEntry = tk.Entry(aiSearches)
GoalNodeEntry.grid(row=3,column=4,padx=100,pady=10)
button3 = tk.Button(aiSearches, text="Submit", command=getStartNodes)
button3.grid(row=4, column=4)
ChooseAlgo = tk.Label(aiSearches, text="Choose Algorithm")
ChooseAlgo.grid(row=0, column=5)
Decision = tk.Label(aiSearches, text="Directed or Undirected")
Decision.grid(row=2, column=5,pady=10)
ComboBox = ttk.Combobox(aiSearches,values = ["BFS","DFS","Unifrom Cost Search","Depth Limited","Iterative Deepning","Bidrectional","Best First","A* Search"])
ComboBox.grid(row=1,column=5)
ComboBox1 = ttk.Combobox(aiSearches, values=["Directed", "Undirected"])
ComboBox1.grid(row=3, column=5)
master.mainloop()
DisplayUI()