FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
aima-python/gui/romania_problem.py at master · addyg/aima-python · GitHub
addyg
aima-python
Repository navigation
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
aima-python
/
gui
/
romania_problem.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
685 lines (610 loc) · 21 KB
Breadcrumbs
aima-python
/
gui
/
romania_problem.py
Copy path
File metadata and controls
685 lines (610 loc) · 21 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
from
tkinter
import
*
import
sys
import
os
.
path
import
math
sys
.
path
.
append
(
os
.
path
.
join
(
os
.
path
.
dirname
(
__file__
),
'..'
))
from
search
import
*
from
search
import
breadth_first_tree_search
as
bfts
,
depth_first_tree_search
as
dfts
, \
depth_first_graph_search
as
dfgs
,
breadth_first_graph_search
as
bfs
,
uniform_cost_search
as
ucs
, \
astar_search
as
asts
from
utils
import
PriorityQueue
from
copy
import
deepcopy
root
=
None
city_coord
=
{}
romania_problem
=
None
algo
=
None
start
=
None
goal
=
None
counter
=
-
1
city_map
=
None
frontier
=
None
front
=
None
node
=
None
next_button
=
None
explored
=
None
def
create_map
(
root
):
"""This function draws out the required map."""
global
city_map
,
start
,
goal
romania_locations
=
romania_map
.
locations
width
=
750
height
=
670
margin
=
5
city_map
=
Canvas
(
root
,
width
=
width
,
height
=
height
)
city_map
.
pack
()
# Since lines have to be drawn between particular points, we need to list
# them separately
make_line
(
city_map
,
romania_locations
[
'Arad'
][
0
],
height
-
romania_locations
[
'Arad'
][
1
],
romania_locations
[
'Sibiu'
][
0
],
height
-
romania_locations
[
'Sibiu'
][
1
],
romania_map
.
get
(
'Arad'
,
'Sibiu'
))
make_line
(
city_map
,
romania_locations
[
'Arad'
][
0
],
height
-
romania_locations
[
'Arad'
][
1
],
romania_locations
[
'Zerind'
][
0
],
height
-
romania_locations
[
'Zerind'
][
1
],
romania_map
.
get
(
'Arad'
,
'Zerind'
))
make_line
(
city_map
,
romania_locations
[
'Arad'
][
0
],
height
-
romania_locations
[
'Arad'
][
1
],
romania_locations
[
'Timisoara'
][
0
],
height
-
romania_locations
[
'Timisoara'
][
1
],
romania_map
.
get
(
'Arad'
,
'Timisoara'
))
make_line
(
city_map
,
romania_locations
[
'Oradea'
][
0
],
height
-
romania_locations
[
'Oradea'
][
1
],
romania_locations
[
'Zerind'
][
0
],
height
-
romania_locations
[
'Zerind'
][
1
],
romania_map
.
get
(
'Oradea'
,
'Zerind'
))
make_line
(
city_map
,
romania_locations
[
'Oradea'
][
0
],
height
-
romania_locations
[
'Oradea'
][
1
],
romania_locations
[
'Sibiu'
][
0
],
height
-
romania_locations
[
'Sibiu'
][
1
],
romania_map
.
get
(
'Oradea'
,
'Sibiu'
))
make_line
(
city_map
,
romania_locations
[
'Lugoj'
][
0
],
height
-
romania_locations
[
'Lugoj'
][
1
],
romania_locations
[
'Timisoara'
][
0
],
height
-
romania_locations
[
'Timisoara'
][
1
],
romania_map
.
get
(
'Lugoj'
,
'Timisoara'
))
make_line
(
city_map
,
romania_locations
[
'Lugoj'
][
0
],
height
-
romania_locations
[
'Lugoj'
][
1
],
romania_locations
[
'Mehadia'
][
0
],
height
-
romania_locations
[
'Mehadia'
][
1
],
romania_map
.
get
(
'Lugoj'
,
'Mehadia'
))
make_line
(
city_map
,
romania_locations
[
'Drobeta'
][
0
],
height
-
romania_locations
[
'Drobeta'
][
1
],
romania_locations
[
'Mehadia'
][
0
],
height
-
romania_locations
[
'Mehadia'
][
1
],
romania_map
.
get
(
'Drobeta'
,
'Mehadia'
))
make_line
(
city_map
,
romania_locations
[
'Drobeta'
][
0
],
height
-
romania_locations
[
'Drobeta'
][
1
],
romania_locations
[
'Craiova'
][
0
],
height
-
romania_locations
[
'Craiova'
][
1
],
romania_map
.
get
(
'Drobeta'
,
'Craiova'
))
make_line
(
city_map
,
romania_locations
[
'Pitesti'
][
0
],
height
-
romania_locations
[
'Pitesti'
][
1
],
romania_locations
[
'Craiova'
][
0
],
height
-
romania_locations
[
'Craiova'
][
1
],
romania_map
.
get
(
'Pitesti'
,
'Craiova'
))
make_line
(
city_map
,
romania_locations
[
'Rimnicu'
][
0
],
height
-
romania_locations
[
'Rimnicu'
][
1
],
romania_locations
[
'Craiova'
][
0
],
height
-
romania_locations
[
'Craiova'
][
1
],
romania_map
.
get
(
'Rimnicu'
,
'Craiova'
))
make_line
(
city_map
,
romania_locations
[
'Rimnicu'
][
0
],
height
-
romania_locations
[
'Rimnicu'
][
1
],
romania_locations
[
'Sibiu'
][
0
],
height
-
romania_locations
[
'Sibiu'
][
1
],
romania_map
.
get
(
'Rimnicu'
,
'Sibiu'
))
make_line
(
city_map
,
romania_locations
[
'Rimnicu'
][
0
],
height
-
romania_locations
[
'Rimnicu'
][
1
],
romania_locations
[
'Pitesti'
][
0
],
height
-
romania_locations
[
'Pitesti'
][
1
],
romania_map
.
get
(
'Rimnicu'
,
'Pitesti'
))
make_line
(
city_map
,
romania_locations
[
'Bucharest'
][
0
],
height
-
romania_locations
[
'Bucharest'
][
1
],
romania_locations
[
'Pitesti'
][
0
],
height
-
romania_locations
[
'Pitesti'
][
1
],
romania_map
.
get
(
'Bucharest'
,
'Pitesti'
))
make_line
(
city_map
,
romania_locations
[
'Fagaras'
][
0
],
height
-
romania_locations
[
'Fagaras'
][
1
],
romania_locations
[
'Sibiu'
][
0
],
height
-
romania_locations
[
'Sibiu'
][
1
],
romania_map
.
get
(
'Fagaras'
,
'Sibiu'
))
make_line
(
city_map
,
romania_locations
[
'Fagaras'
][
0
],
height
-
romania_locations
[
'Fagaras'
][
1
],
romania_locations
[
'Bucharest'
][
0
],
height
-
romania_locations
[
'Bucharest'
][
1
],
romania_map
.
get
(
'Fagaras'
,
'Bucharest'
))
make_line
(
city_map
,
romania_locations
[
'Giurgiu'
][
0
],
height
-
romania_locations
[
'Giurgiu'
][
1
],
romania_locations
[
'Bucharest'
][
0
],
height
-
romania_locations
[
'Bucharest'
][
1
],
romania_map
.
get
(
'Giurgiu'
,
'Bucharest'
))
make_line
(
city_map
,
romania_locations
[
'Urziceni'
][
0
],
height
-
romania_locations
[
'Urziceni'
][
1
],
romania_locations
[
'Bucharest'
][
0
],
height
-
romania_locations
[
'Bucharest'
][
1
],
romania_map
.
get
(
'Urziceni'
,
'Bucharest'
))
make_line
(
city_map
,
romania_locations
[
'Urziceni'
][
0
],
height
-
romania_locations
[
'Urziceni'
][
1
],
romania_locations
[
'Hirsova'
][
0
],
height
-
romania_locations
[
'Hirsova'
][
1
],
romania_map
.
get
(
'Urziceni'
,
'Hirsova'
))
make_line
(
city_map
,
romania_locations
[
'Eforie'
][
0
],
height
-
romania_locations
[
'Eforie'
][
1
],
romania_locations
[
'Hirsova'
][
0
],
height
-
romania_locations
[
'Hirsova'
][
1
],
romania_map
.
get
(
'Eforie'
,
'Hirsova'
))
make_line
(
city_map
,
romania_locations
[
'Urziceni'
][
0
],
height
-
romania_locations
[
'Urziceni'
][
1
],
romania_locations
[
'Vaslui'
][
0
],
height
-
romania_locations
[
'Vaslui'
][
1
],
romania_map
.
get
(
'Urziceni'
,
'Vaslui'
))
make_line
(
city_map
,
romania_locations
[
'Iasi'
][
0
],
height
-
romania_locations
[
'Iasi'
][
1
],
romania_locations
[
'Vaslui'
][
0
],
height
-
romania_locations
[
'Vaslui'
][
1
],
romania_map
.
get
(
'Iasi'
,
'Vaslui'
))
make_line
(
city_map
,
romania_locations
[
'Iasi'
][
0
],
height
-
romania_locations
[
'Iasi'
][
1
],
romania_locations
[
'Neamt'
][
0
],
height
-
romania_locations
[
'Neamt'
][
1
],
romania_map
.
get
(
'Iasi'
,
'Neamt'
))
for
city
in
romania_locations
.
keys
():
make_rectangle
(
city_map
,
romania_locations
[
city
][
0
],
height
-
romania_locations
[
city
][
1
],
margin
,
city
)
make_legend
(
city_map
)
def
make_line
(
map
,
x0
,
y0
,
x1
,
y1
,
distance
):
"""This function draws out the lines joining various points."""
map
.
create_line
(
x0
,
y0
,
x1
,
y1
)
map
.
create_text
((
x0
+
x1
)
/
2
, (
y0
+
y1
)
/
2
,
text
=
distance
)
def
make_rectangle
(
map
,
x0
,
y0
,
margin
,
city_name
):
"""This function draws out rectangles for various points."""
global
city_coord
rect
=
map
.
create_rectangle
(
x0
-
margin
,
y0
-
margin
,
x0
+
margin
,
y0
+
margin
,
fill
=
"white"
)
if
"Bucharest"
in
city_name
or
"Pitesti"
in
city_name
or
"Lugoj"
in
city_name
\
or
"Mehadia"
in
city_name
or
"Drobeta"
in
city_name
:
map
.
create_text
(
x0
-
2
*
margin
,
y0
-
2
*
margin
,
text
=
city_name
,
anchor
=
E
)
else
:
map
.
create_text
(
x0
-
2
*
margin
,
y0
-
2
*
margin
,
text
=
city_name
,
anchor
=
SE
)
city_coord
.
update
({
city_name
:
rect
})
def
make_legend
(
map
):
rect1
=
map
.
create_rectangle
(
600
,
100
,
610
,
110
,
fill
=
"white"
)
text1
=
map
.
create_text
(
615
,
105
,
anchor
=
W
,
text
=
"Un-explored"
)
rect2
=
map
.
create_rectangle
(
600
,
115
,
610
,
125
,
fill
=
"orange"
)
text2
=
map
.
create_text
(
615
,
120
,
anchor
=
W
,
text
=
"Frontier"
)
rect3
=
map
.
create_rectangle
(
600
,
130
,
610
,
140
,
fill
=
"red"
)
text3
=
map
.
create_text
(
615
,
135
,
anchor
=
W
,
text
=
"Currently Exploring"
)
rect4
=
map
.
create_rectangle
(
600
,
145
,
610
,
155
,
fill
=
"grey"
)
text4
=
map
.
create_text
(
615
,
150
,
anchor
=
W
,
text
=
"Explored"
)
rect5
=
map
.
create_rectangle
(
600
,
160
,
610
,
170
,
fill
=
"dark green"
)
text5
=
map
.
create_text
(
615
,
165
,
anchor
=
W
,
text
=
"Final Solution"
)
def
tree_search
(
problem
):
"""
Search through the successors of a problem to find a goal.
The argument frontier should be an empty queue.
Don't worry about repeated paths to a state. [Figure 3.7]
This function has been changed to make it suitable for the Tkinter GUI.
"""
global
counter
,
frontier
,
node
if
counter
==
-
1
:
frontier
.
append
(
Node
(
problem
.
initial
))
display_frontier
(
frontier
)
if
counter
%
3
==
0
and
counter
>=
0
:
node
=
frontier
.
pop
()
display_current
(
node
)
if
counter
%
3
==
1
and
counter
>=
0
:
if
problem
.
goal_test
(
node
.
state
):
return
node
frontier
.
extend
(
node
.
expand
(
problem
))
display_frontier
(
frontier
)
if
counter
%
3
==
2
and
counter
>=
0
:
display_explored
(
node
)
return
None
def
graph_search
(
problem
):
"""
Search through the successors of a problem to find a goal.
The argument frontier should be an empty queue.
If two paths reach a state, only use the first one. [Figure 3.7]
This function has been changed to make it suitable for the Tkinter GUI.
"""
global
counter
,
frontier
,
node
,
explored
if
counter
==
-
1
:
frontier
.
append
(
Node
(
problem
.
initial
))
explored
=
set
()
display_frontier
(
frontier
)
if
counter
%
3
==
0
and
counter
>=
0
:
node
=
frontier
.
pop
()
display_current
(
node
)
if
counter
%
3
==
1
and
counter
>=
0
:
if
problem
.
goal_test
(
node
.
state
):
return
node
explored
.
add
(
node
.
state
)
frontier
.
extend
(
child
for
child
in
node
.
expand
(
problem
)
if
child
.
state
not
in
explored
and
child
not
in
frontier
)
display_frontier
(
frontier
)
if
counter
%
3
==
2
and
counter
>=
0
:
display_explored
(
node
)
return
None
def
display_frontier
(
queue
):
"""This function marks the frontier nodes (orange) on the map."""
global
city_map
,
city_coord
qu
=
deepcopy
(
queue
)
while
qu
:
node
=
qu
.
pop
()
for
city
in
city_coord
.
keys
():
if
node
.
state
==
city
:
city_map
.
itemconfig
(
city_coord
[
city
],
fill
=
"orange"
)
def
display_current
(
node
):
"""This function marks the currently exploring node (red) on the map."""
global
city_map
,
city_coord
city
=
node
.
state
city_map
.
itemconfig
(
city_coord
[
city
],
fill
=
"red"
)
def
display_explored
(
node
):
"""This function marks the already explored node (gray) on the map."""
global
city_map
,
city_coord
city
=
node
.
state
city_map
.
itemconfig
(
city_coord
[
city
],
fill
=
"gray"
)
def
display_final
(
cities
):
"""This function marks the final solution nodes (green) on the map."""
global
city_map
,
city_coord
for
city
in
cities
:
city_map
.
itemconfig
(
city_coord
[
city
],
fill
=
"green"
)
def
breadth_first_tree_search
(
problem
):
"""Search the shallowest nodes in the search tree first."""
global
frontier
,
counter
,
node
if
counter
==
-
1
:
frontier
=
deque
()
if
counter
==
-
1
:
frontier
.
append
(
Node
(
problem
.
initial
))
display_frontier
(
frontier
)
if
counter
%
3
==
0
and
counter
>=
0
:
node
=
frontier
.
popleft
()
display_current
(
node
)
if
counter
%
3
==
1
and
counter
>=
0
:
if
problem
.
goal_test
(
node
.
state
):
return
node
frontier
.
extend
(
node
.
expand
(
problem
))
display_frontier
(
frontier
)
if
counter
%
3
==
2
and
counter
>=
0
:
display_explored
(
node
)
return
None
def
depth_first_tree_search
(
problem
):
"""Search the deepest nodes in the search tree first."""
# This search algorithm might not work in case of repeated paths.
global
frontier
,
counter
,
node
if
counter
==
-
1
:
frontier
=
[]
# stack
if
counter
==
-
1
:
frontier
.
append
(
Node
(
problem
.
initial
))
display_frontier
(
frontier
)
if
counter
%
3
==
0
and
counter
>=
0
:
node
=
frontier
.
pop
()
display_current
(
node
)
if
counter
%
3
==
1
and
counter
>=
0
:
if
problem
.
goal_test
(
node
.
state
):
return
node
frontier
.
extend
(
node
.
expand
(
problem
))
display_frontier
(
frontier
)
if
counter
%
3
==
2
and
counter
>=
0
:
display_explored
(
node
)
return
None
def
breadth_first_graph_search
(
problem
):
"""[Figure 3.11]"""
global
frontier
,
node
,
explored
,
counter
if
counter
==
-
1
:
node
=
Node
(
problem
.
initial
)
display_current
(
node
)
if
problem
.
goal_test
(
node
.
state
):
return
node
frontier
=
deque
([
node
])
# FIFO queue
display_frontier
(
frontier
)
explored
=
set
()
if
counter
%
3
==
0
and
counter
>=
0
:
node
=
frontier
.
popleft
()
display_current
(
node
)
explored
.
add
(
node
.
state
)
if
counter
%
3
==
1
and
counter
>=
0
:
for
child
in
node
.
expand
(
problem
):
if
child
.
state
not
in
explored
and
child
not
in
frontier
:
if
problem
.
goal_test
(
child
.
state
):
return
child
frontier
.
append
(
child
)
display_frontier
(
frontier
)
if
counter
%
3
==
2
and
counter
>=
0
:
display_explored
(
node
)
return
None
def
depth_first_graph_search
(
problem
):
"""Search the deepest nodes in the search tree first."""
global
counter
,
frontier
,
node
,
explored
if
counter
==
-
1
:
frontier
=
[]
# stack
if
counter
==
-
1
:
frontier
.
append
(
Node
(
problem
.
initial
))
explored
=
set
()
display_frontier
(
frontier
)
if
counter
%
3
==
0
and
counter
>=
0
:
node
=
frontier
.
pop
()
display_current
(
node
)
if
counter
%
3
==
1
and
counter
>=
0
:
if
problem
.
goal_test
(
node
.
state
):
return
node
explored
.
add
(
node
.
state
)
frontier
.
extend
(
child
for
child
in
node
.
expand
(
problem
)
if
child
.
state
not
in
explored
and
child
not
in
frontier
)
display_frontier
(
frontier
)
if
counter
%
3
==
2
and
counter
>=
0
:
display_explored
(
node
)
return
None
def
best_first_graph_search
(
problem
,
f
):
"""Search the nodes with the lowest f scores first.
You specify the function f(node) that you want to minimize; for example,
if f is a heuristic estimate to the goal, then we have greedy best
first search; if f is node.depth then we have breadth-first search.
There is a subtlety: the line "f = memoize(f, 'f')" means that the f
values will be cached on the nodes as they are computed. So after doing
a best first search you can examine the f values of the path returned."""
global
frontier
,
node
,
explored
,
counter
if
counter
==
-
1
:
f
=
memoize
(
f
,
'f'
)
node
=
Node
(
problem
.
initial
)
display_current
(
node
)
if
problem
.
goal_test
(
node
.
state
):
return
node
frontier
=
PriorityQueue
(
'min'
,
f
)
frontier
.
append
(
node
)
display_frontier
(
frontier
)
explored
=
set
()
if
counter
%
3
==
0
and
counter
>=
0
:
node
=
frontier
.
pop
()
display_current
(
node
)
if
problem
.
goal_test
(
node
.
state
):
return
node
explored
.
add
(
node
.
state
)
if
counter
%
3
==
1
and
counter
>=
0
:
for
child
in
node
.
expand
(
problem
):
if
child
.
state
not
in
explored
and
child
not
in
frontier
:
frontier
.
append
(
child
)
elif
child
in
frontier
:
incumbent
=
frontier
[
child
]
if
f
(
child
)
<
f
(
incumbent
):
del
frontier
[
incumbent
]
frontier
.
append
(
child
)
display_frontier
(
frontier
)
if
counter
%
3
==
2
and
counter
>=
0
:
display_explored
(
node
)
return
None
def
uniform_cost_search
(
problem
):
"""[Figure 3.14]"""
return
best_first_graph_search
(
problem
,
lambda
node
:
node
.
path_cost
)
def
astar_search
(
problem
,
h
=
None
):
"""A* search is best-first graph search with f(n) = g(n)+h(n).
You need to specify the h function when you call astar_search, or
else in your Problem subclass."""
h
=
memoize
(
h
or
problem
.
h
,
'h'
)
return
best_first_graph_search
(
problem
,
lambda
n
:
n
.
path_cost
+
h
(
n
))
# TODO:
# Remove redundant code.
# Make the interchangbility work between various algorithms at each step.
def
on_click
():
"""
This function defines the action of the 'Next' button.
"""
global
algo
,
counter
,
next_button
,
romania_problem
,
start
,
goal
romania_problem
=
GraphProblem
(
start
.
get
(),
goal
.
get
(),
romania_map
)
if
"Breadth-First Tree Search"
==
algo
.
get
():
node
=
breadth_first_tree_search
(
romania_problem
)
if
node
is
not
None
:
final_path
=
bfts
(
romania_problem
).
solution
()
final_path
.
append
(
start
.
get
())
display_final
(
final_path
)
next_button
.
config
(
state
=
"disabled"
)
counter
+=
1
elif
"Depth-First Tree Search"
==
algo
.
get
():
node
=
depth_first_tree_search
(
romania_problem
)
if
node
is
not
None
:
final_path
=
dfts
(
romania_problem
).
solution
()
final_path
.
append
(
start
.
get
())
display_final
(
final_path
)
next_button
.
config
(
state
=
"disabled"
)
counter
+=
1
elif
"Breadth-First Graph Search"
==
algo
.
get
():
node
=
breadth_first_graph_search
(
romania_problem
)
if
node
is
not
None
:
final_path
=
bfs
(
romania_problem
).
solution
()
final_path
.
append
(
start
.
get
())
display_final
(
final_path
)
next_button
.
config
(
state
=
"disabled"
)
counter
+=
1
elif
"Depth-First Graph Search"
==
algo
.
get
():
node
=
depth_first_graph_search
(
romania_problem
)
if
node
is
not
None
:
final_path
=
dfgs
(
romania_problem
).
solution
()
final_path
.
append
(
start
.
get
())
display_final
(
final_path
)
next_button
.
config
(
state
=
"disabled"
)
counter
+=
1
elif
"Uniform Cost Search"
==
algo
.
get
():
node
=
uniform_cost_search
(
romania_problem
)
if
node
is
not
None
:
final_path
=
ucs
(
romania_problem
).
solution
()
final_path
.
append
(
start
.
get
())
display_final
(
final_path
)
next_button
.
config
(
state
=
"disabled"
)
counter
+=
1
elif
"A* - Search"
==
algo
.
get
():
node
=
astar_search
(
romania_problem
)
if
node
is
not
None
:
final_path
=
asts
(
romania_problem
).
solution
()
final_path
.
append
(
start
.
get
())
display_final
(
final_path
)
next_button
.
config
(
state
=
"disabled"
)
counter
+=
1
def
reset_map
():
global
counter
,
city_coord
,
city_map
,
next_button
counter
=
-
1
for
city
in
city_coord
.
keys
():
city_map
.
itemconfig
(
city_coord
[
city
],
fill
=
"white"
)
next_button
.
config
(
state
=
"normal"
)
# TODO: Add more search algorithms in the OptionMenu
def
main
():
global
algo
,
start
,
goal
,
next_button
root
=
Tk
()
root
.
title
(
"Road Map of Romania"
)
root
.
geometry
(
"950x1150"
)
algo
=
StringVar
(
root
)
start
=
StringVar
(
root
)
goal
=
StringVar
(
root
)
algo
.
set
(
"Breadth-First Tree Search"
)
start
.
set
(
'Arad'
)
goal
.
set
(
'Bucharest'
)
cities
=
sorted
(
romania_map
.
locations
.
keys
())
algorithm_menu
=
OptionMenu
(
root
,
algo
,
"Breadth-First Tree Search"
,
"Depth-First Tree Search"
,
"Breadth-First Graph Search"
,
"Depth-First Graph Search"
,
"Uniform Cost Search"
,
"A* - Search"
)
Label
(
root
,
text
=
"
\n
Search Algorithm"
).
pack
()
algorithm_menu
.
pack
()
Label
(
root
,
text
=
"
\n
Start City"
).
pack
()
start_menu
=
OptionMenu
(
root
,
start
,
*
cities
)
start_menu
.
pack
()
Label
(
root
,
text
=
"
\n
Goal City"
).
pack
()
goal_menu
=
OptionMenu
(
root
,
goal
,
*
cities
)
goal_menu
.
pack
()
frame1
=
Frame
(
root
)
next_button
=
Button
(
frame1
,
width
=
6
,
height
=
2
,
text
=
"Next"
,
command
=
on_click
,
padx
=
2
,
pady
=
2
,
relief
=
GROOVE
)
next_button
.
pack
(
side
=
RIGHT
)
reset_button
=
Button
(
frame1
,
width
=
6
,
height
=
2
,
text
=
"Reset"
,
command
=
reset_map
,
padx
=
2
,
pady
=
2
,
relief
=
GROOVE
)
reset_button
.
pack
(
side
=
RIGHT
)
frame1
.
pack
(
side
=
BOTTOM
)
create_map
(
root
)
root
.
mainloop
()
if
__name__
==
"__main__"
:
main
()
Back
|
FazBrowse Home
|
New Git URL