-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.py
More file actions
538 lines (427 loc) · 16.9 KB
/
Copy pathmain.py
File metadata and controls
538 lines (427 loc) · 16.9 KB
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
from SearchAgent import SearchAgent
from Node import Node
from SavedGraph import *
from browser import document, window, alert
import javascript, json
#changes_made = False
def update_graph():
global any_change, colors_to_fill_inside, agent, radius, colors_for_border, node_type \
,animate, val, ctx, border, node_selected, selected_algorithm
#update_canvas_size()
def draw_weights(node1, node2):
global ctx
ctx.font = "18px Arial"
x1, y1 = agent.graph[node1].position
x2, y2 = agent.graph[node2].position
weight = agent.graph[node1].children[node2]
ctx.fillText(weight,(x1+x2)/2, (y1+y2)/2)
def draw_heuristics(node):
global ctx
# Drawing Heuristics
x, y = agent.graph[node].position
ctx.font = "13px Arial"
ctx.fillStyle = 'black'
ctx.fillText(agent.graph[node].heuristics, x, y + 14)
def draw_circle(node):
global ctx, border
if node_selected:
if selected_node_ == node:
border = 'selected'
else:
border = 'unselected'
else:
border = 'unselected'
# Drawing circle shape
x, y = agent.graph[node].position
ctx.beginPath()
ctx.strokeStyle = colors_for_border[border] # unselected -> black, selected -> red
ctx.arc(x,y, radius, 0, 2 * javascript.Math.PI) # start angle (0), end angle (2 pi)
ctx.fillStyle = colors_to_fill_inside[node_type]
node_type = 'normal' # Node type is changed. If goal node --> green, If start node --> orange, If normal node --> white
ctx.fill()
ctx.stroke()
# Drawing node name
ctx.fillStyle = 'black'
ctx.font = "20px Arial"
ctx.textBaseline = "middle"
ctx.fillText(node, x, y - 5)
draw_heuristics(node)
def draw_edges(node1, node2, flag=0):
global ctx
x1, y1 = agent.graph[node1].position
x2, y2 = agent.graph[node2].position
ctx.beginPath()
if flag == 0:
ctx.strokeStyle = 'black'
ctx.lineWidth = 1
else:
ctx.strokeStyle = 'red'
ctx.lineWidth = 5
ctx.moveTo(x1,y1)
ctx.lineTo(x2,y2)
ctx.stroke()
ctx.lineWidth = 1
if flag == 1:
draw_weights(node1, node2)
def animate_graph():
global node_type, val, selected_algorithm
if (selected_algorithm not in ['bfs', 'dfs']):
val = val[1:]
start = val[0]
for node in val[1:]:
end = node
print("start : ",start,"End : ",end)
draw_edges(start, end, 1)
start = end
for circle_node in agent.graph.keys():
if agent.start == circle_node:
node_type = 'start'
elif agent.goal == circle_node:
node_type = 'goal'
else:
node_type = 'normal'
draw_circle(circle_node)
if any_change:
ctx.clearRect(0, 0, document['canvas'].offsetWidth, document['canvas'].offsetHeight)
#alert("inside update graph")
visited = []
for node in agent.graph.keys():
if (node == agent.goal):
node_type = 'goal'
elif (node == agent.start):
node_type = 'start'
else:
node_type = 'normal'
children = agent.graph[node].children
for child_node ,weight in children.items():
if (node, child_node) in visited or (child_node, node) in visited:
continue
visited.append((node, child_node))
draw_edges(node, child_node)
draw_weights(node, child_node)
draw_circle(node)
if animate:
animate_graph()
animate = False
any_change = False
if agent.status == 'searching':
window.setTimeout(agent_search, 24)
def solve(algo):
global agent, yield_result, any_change, result, i
document["paragh"].innerText = "Solving"
agent.status = 'searching'
i = 0
result = map_algorithm[algo]()
if (result == None):
document["paragh"].innerText = "Can't find"
agent.status = 'idle'
return
update_graph()
def next_iteration():
global yield_result, val
val = next(yield_result)
def agent_search():
global any_change, start_time, animate, selected_algorithm, result, val, i, j
set_speed()
if agent.status == 'searching':
#if selected_algorithm == 'dfs':
Len = len(result)
if (i == Len):
document["paragh"].innerText = "Solved, Total Enqueings : " + str(agent.no_enqueue)
agent.status = 'idle'
return
now_time = javascript.Date.now()
if (now_time - start_time >= speed):
val = result[i]
i += 1
any_change = True
animate = True
window.setTimeout(update_graph, 10)
start_time = now_time
else:
window.setTimeout(update_graph, 10)
def graph_setup(event):
global tool, counter, any_change, node_selected, selected_node_, From, To, node_heur
def find_edge_ends(radius): # Finding edge ends to update weight of that edge
visited = []
for node in agent.graph.values():
children = node.children
x1,y1 = node.position
for child_node in children.keys():
if (node, child_node) in visited or (child_node, node) in visited:
continue
visited.append((node,child_node))
x2,y2 = agent.graph[child_node].position
mid_x, mid_y = (x1 + x2)/2, (y1 + y2)/2
if x <= mid_x + radius and x >= mid_x - radius and \
y <= mid_y + radius and y >= mid_y - radius:
return node.name ,child_node
return -1,-1
def find_node():
for node in agent.graph.values():
if x <= node.position[0] + radius and x >= node.position[0] - radius and \
y <= node.position[1] + radius and y >= node.position[1] - radius:
return node.name
return -1
# x and y co-ordinates
x = event.clientX
y = event.clientY
x = x - canvas.offsetLeft
y = y - canvas.offsetTop
node_name = find_node()
if node_name == -1:
if tool == "nodeAdd":
ctx.textAlign = "center"
# Drawing node name
ctx.fillStyle = 'black'
ctx.font = "20px Arial"
ctx.textBaseline = "middle"
node_name = chr(counter%26 + 65)
ctx.fillText(node_name, x, y - 5)
# Creating actual node in graph
agent.graph[node_name] = Node(node_name, position = (x, y))
# Drawing Heuristics
ctx.font = "13px Arial"
ctx.fillText("1", x, y + 14)
# Updating graph
any_change = True
counter += 1
elif tool == "weights":
From , To = find_edge_ends(radius)
if From != -1: # i.e if the user clicked on the proper location in between any two nodes
DialogBoxVisibility(True)
# Making Changes
update_graph()
else:
if tool == "nodeDelete":
# Setting goal and agent to none, if that respective node is deleted.
if agent.goal == node_name:
agent.goal = None
elif agent.start == node_name:
agent.start = None
# Deleting node's children
for child in agent.graph[node_name].children.keys():
del agent.graph[child].children[node_name]
# Deleting node itself
del agent.graph[node_name]
# Updating graph
any_change = True
elif tool == "edgeAdd":
if not node_selected:
node_selected = True
selected_node_ = node_name
From = node_name
# Updating graph to border out selected graph as red
any_change = True
elif node_selected:
From = selected_node_
To = node_name
if From == To:
node_selected = False
# To remove red border
any_change = True
# To avoid edge drawing redundantly over already existing edge
elif To in agent.graph[From].children.keys() or To in agent.graph[To].children.keys():
node_selected = False
# To remove red border
any_change = True
else :
x1,y1 = agent.graph[From].position
x2,y2 = agent.graph[To].position
ctx.fillStyle = 'black'
# Drawing Edge line
ctx.beginPath()
ctx.moveTo(x1,y1)
ctx.lineTo(x2,y2)
ctx.stroke()
# Drawing Default weight on edges
ctx.font = "18px Arial"
ctx.fillText(1,(x1+x2)/2, (y1+y2)/2)
# Updating edge weight in children of both nodes
agent.graph[From].children.update({To : 1})
agent.graph[To].children.update({From : 1})
node_selected = False
# To remove red border and update edges
any_change = True
elif tool == "edgeDelete":
if not node_selected:
node_selected = True
selected_node_ = node_name
From = node_name
# Updating graph to border out selected node as red
any_change = True
elif node_selected:
From = selected_node_
To = node_name
# If To Node is a child of From node then only delete
if To in agent.graph[From].children.keys():
del agent.graph[From].children[To]
del agent.graph[To].children[From]
node_selected = False
# To remove red border of selected node
any_change = True
elif tool == "heuristics":
node_heur = node_name
DialogBoxVisibility(True)
elif tool == "setgoal":
if node_name != agent.start: # To avoid setting both start and end as goal
agent.goal = node_name
# To update goal node color
any_change = True
elif tool == "setstart":
if node_name != agent.goal: # To avoid setting both start and end as goal
agent.start = node_name
# To update start node color
any_change = True
# if any_change is set to True, graph will be erased and redrawn with changes
update_graph()
def DialogBoxVisibility(value):
global any_change
if value:
document["weights-modal"].showModal()
else:
document["weights-modal"].close()
any_change = True
update_graph()
def weightsUpdate():
global any_change
validated = document["weights-form"].reportValidity()
if validated:
result = document["weights-input"].value
agent.graph[From].children.update({To: result})
agent.graph[To].children.update({From: result})
def heuristicsUpdate():
global any_change
validated = document["weights-form"].reportValidity()
if validated:
result = document["weights-input"].value
agent.graph[node_heur].heuristics = result
any_change = True
def tool_select(do):
global tool
if do == "nodeAdd":
document["paragh"].innerText = "Click to Add Node"
elif do == "nodeDelete":
document["paragh"].innerText = "Select Node to Delete"
elif do == "edgeAdd":
document["paragh"].innerText = "Select two nodes to add edge"
elif do == "edgeDelete":
document["paragh"].innerText = "Select two nodes to delete edge"
elif do == "heuristics":
document["paragh"].innerText = "Select node to modify heuristics"
elif do == "weights":
document["paragh"].innerText = "Click on respective weight to modify weights"
elif do == "setstart":
document["paragh"].innerText = "Select node as Start Node"
elif do == "setgoal":
document["paragh"].innerText = "Select node as Goal Node"
tool = do
def algo_select(algo):
global selected_algorithm
if algo == "bfs":
document["paragh"].innerText = "Selected Breadth first search"
elif algo == "dfs":
document["paragh"].innerText = "Selected Depth first search"
elif algo == "hc":
document["paragh"].innerText = "Selected Hill climbing"
elif algo == "bs":
document["paragh"].innerText = "Selected Beam search"
elif algo == "bb":
document["paragh"].innerText = "Selected Branch and bound"
elif algo == "bb-h":
document["paragh"].innerText = "Selected Branch and bound + additional heuristics"
elif algo == "astar":
document["paragh"].innerText = "Selected A Star"
selected_algorithm = algo
def saved_graph():
global any_change, agent, counter
graph = int(document['Select'].value)
if graph == 1:
#alert("inside 1")
agent.graph, c = G1()
elif graph == 2:
#alert("inside 2")
agent.graph, c = G2()
counter = c
any_change = True
update_graph()
def update_canvas_size():
inner = document['right']
canvas.setAttribute('width', str(inner.offsetWidth));
canvas.setAttribute('height',str(inner.offsetHeight));
def set_speed():
global speed
value = document['Speed'].value
if value == '2x':
speed = 200
elif value == '1.5x':
speed = 600
else:
speed = 800
#alert(speed)
canvas = document["canvas"]
ctx = canvas.getContext("2d")
window_width = window.innerWidth
window_height = window.innerHeight
canvas.setAttribute('width', str(document['right'].offsetWidth));
canvas.setAttribute('height',str(document['right'].offsetHeight));
node_type = 'normal'
border = 'unselected'
colors_to_fill_inside = {'goal' : "green", 'normal' : 'white', 'start': 'orange'}
colors_to_fill_text = {'goal' : 'white', 'normal' : 'black', 'start':'black'}
colors_for_border = {'selected' : 'red', 'unselected' : 'black'}
tool = None
selected_algorithm = None
counter = 0
node_name = chr(counter + 65)
radius = 20
# On clicking , if any node is selected
node_selected = False
# selected node
selected_node_ = None
# keeping track of change
any_change = False
animate = False
# variable to hold iterator of yield and individual yielded value
yield_result = None
result = None
val = None
node_heur = None
i = 0; j = 0
# For edge manipulation - Adding and deleting
From = None
To = None
# set speed
speed = 800
agent = SearchAgent()
map_algorithm = {'bfs' : agent.bfs, 'dfs' : agent.dfs , 'hc' : agent.hc \
,'bs' : agent.bs, 'bb' : agent.bb, 'bb-h' : agent.bb_h, 'astar' : agent.a_star }
# Selecting Tools
document['nodeAdd'].bind('click', lambda e: tool_select('nodeAdd'))
document['nodeDelete'].bind('click', lambda e: tool_select('nodeDelete'))
document['edgeAdd'].bind('click', lambda e: tool_select('edgeAdd'))
document['edgeDelete'].bind('click', lambda e: tool_select('edgeDelete'))
document['heuristics'].bind('click', lambda e: tool_select('heuristics'))
document['weights'].bind('click', lambda e: tool_select('weights'))
# For Adjusting Weights and heuristics
document["weights-close"].bind("click", lambda e: DialogBoxVisibility(False))
document["weights-update"].bind("click", lambda e: weightsUpdate() if tool == 'weights' else heuristicsUpdate())
# For setting goal and start
document['setgoal'].bind('click', lambda e: tool_select('setgoal'))
document['setstart'].bind('click', lambda e: tool_select('setstart'))
# For choosing Algorithm
document['dfs'].bind('click', lambda e: algo_select('dfs'))
document['bfs'].bind('click', lambda e: algo_select('bfs'))
document['hc'].bind('click', lambda e: algo_select('hc'))
document['bs'].bind('click', lambda e: algo_select('bs'))
document['bb'].bind('click', lambda e: algo_select('bb'))
document['bb-h'].bind('click', lambda e: algo_select('bb-h'))
document['astar'].bind('click', lambda e: algo_select('astar'))
# To solve the graph
document['solve'].bind('click', lambda e: solve(selected_algorithm))
# To set up graph -> Node creation, node deletion, edge creation, edge deletion
document['canvas'].bind('mousedown', lambda e: graph_setup(e))
document["Select"].bind('change', lambda e: saved_graph())
document["Speed"].bind('change', lambda e: set_speed())
# Keeping track of start time to use while animating the graph in proper interval
start_time = javascript.Date.now()