Skip to content

Commit 8561c52

Browse files
apb7norvig
authored andcommitted
Added A*-Search in GUI (#679)
1 parent 75d3807 commit 8561c52

File tree

1 file changed

+19
-2
lines changed

1 file changed

+19
-2
lines changed

gui/romania_problem.py

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
sys.path.append(os.path.join(os.path.dirname(__file__), '..'))
66
from search import *
77
from search import breadth_first_tree_search as bfts, depth_first_tree_search as dfts, \
8-
depth_first_graph_search as dfgs, breadth_first_search as bfs, uniform_cost_search as ucs
8+
depth_first_graph_search as dfgs, breadth_first_search as bfs, uniform_cost_search as ucs, \
9+
astar_search as asts
910
from utils import Stack, FIFOQueue, PriorityQueue
1011
from copy import deepcopy
1112

@@ -512,6 +513,14 @@ def uniform_cost_search(problem):
512513
return best_first_graph_search(problem, lambda node: node.path_cost)
513514

514515

516+
def astar_search(problem, h=None):
517+
"""A* search is best-first graph search with f(n) = g(n)+h(n).
518+
You need to specify the h function when you call astar_search, or
519+
else in your Problem subclass."""
520+
h = memoize(h or problem.h, 'h')
521+
return best_first_graph_search(problem, lambda n: n.path_cost + h(n))
522+
523+
515524
# TODO:
516525
# Remove redundant code.
517526
# Make the interchangbility work between various algorithms at each step.
@@ -561,6 +570,14 @@ def on_click():
561570
display_final(final_path)
562571
next_button.config(state="disabled")
563572
counter += 1
573+
elif "A* - Search" == algo.get():
574+
node = astar_search(romania_problem)
575+
if node is not None:
576+
final_path = asts(romania_problem).solution()
577+
final_path.append(start.get())
578+
display_final(final_path)
579+
next_button.config(state="disabled")
580+
counter += 1
564581

565582

566583
def reset_map():
@@ -589,7 +606,7 @@ def main():
589606
root,
590607
algo, "Breadth-First Tree Search", "Depth-First Tree Search",
591608
"Breadth-First Search", "Depth-First Graph Search",
592-
"Uniform Cost Search")
609+
"Uniform Cost Search", "A* - Search")
593610
Label(root, text="\n Search Algorithm").pack()
594611
algorithm_menu.pack()
595612
Label(root, text="\n Start City").pack()

0 commit comments

Comments
 (0)