From 09de0324b29938f3ddd64e2d22d9adcbc8f9a7ec Mon Sep 17 00:00:00 2001 From: Leonid Belyaev Date: Fri, 10 Dec 2021 11:47:39 -0500 Subject: [PATCH] tdfs --- code/bot/search.py | 44 ++++++++++++++++++++++++++++++++++++-------- 1 file changed, 36 insertions(+), 8 deletions(-) diff --git a/code/bot/search.py b/code/bot/search.py index 0d0e65c..e82cfb0 100644 --- a/code/bot/search.py +++ b/code/bot/search.py @@ -10,6 +10,29 @@ # self.path = path +def tree_depth_first_search(game): + frontier = [] + + start_node = (game.get_start_state(), 0, []) + + frontier.append(start_node) + + while not frontier: + current_state, current_cost, current_path = frontier.pop() + if game.is_goal(current_state): + return current_path + + for new_state, new_action, action_cost in game.get_successors(current_state): + new_node = ( + new_state, + current_cost + action_cost, + current_path + [new_action], + ) + + frontier.append(new_state) + return [] + + def breadth_first_search(game): frontier = queue.SimpleQueue() @@ -39,6 +62,7 @@ def breadth_first_search(game): # no path found return [] + def Astar_search(game, heurisitic): frontier = PriorityQueue() @@ -67,18 +91,20 @@ def Astar_search(game, heurisitic): frontier.update(new_node, new_node[1] + heurisitic(new_node[0])) # no path found - #print(str(explored)) + # print(str(explored)) return [] -#code from CS188 Berkeley for PriorityQueue with update function modified + +# code from CS188 Berkeley for PriorityQueue with update function modified class PriorityQueue: """ - Implements a priority queue data structure. Each inserted item - has a priority associated with it and the client is usually interested - in quick retrieval of the lowest-priority item in the queue. This - data structure allows O(1) access to the lowest-priority item. + Implements a priority queue data structure. Each inserted item + has a priority associated with it and the client is usually interested + in quick retrieval of the lowest-priority item in the queue. This + data structure allows O(1) access to the lowest-priority item. """ - def __init__(self): + + def __init__(self): self.heap = [] self.count = 0 @@ -109,5 +135,7 @@ def update(self, item, priority): else: self.push(item, priority) + from game import CheeseGame -Astar_search(CheeseGame(), lambda a: 0) \ No newline at end of file + +Astar_search(CheeseGame(), lambda a: 0)