Skip to content

Commit

Permalink
tdfs
Browse files Browse the repository at this point in the history
  • Loading branch information
aeblyve committed Dec 10, 2021
1 parent 5e5cd4e commit 09de032
Showing 1 changed file with 36 additions and 8 deletions.
44 changes: 36 additions & 8 deletions code/bot/search.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -39,6 +62,7 @@ def breadth_first_search(game):
# no path found
return []


def Astar_search(game, heurisitic):

frontier = PriorityQueue()
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -109,5 +135,7 @@ def update(self, item, priority):
else:
self.push(item, priority)


from game import CheeseGame
Astar_search(CheeseGame(), lambda a: 0)

Astar_search(CheeseGame(), lambda a: 0)

0 comments on commit 09de032

Please sign in to comment.