Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def genericSearch(problem, fringe, h=nullHeuristic):
- """
- Generic Search in search tree
- Your search algorithm needs to return a list of actions that reaches
- the goal. Make sure to implement a graph search algorithm © pyTony
- """
- start = problem.getStartState()
- closed = set()
- fringe.push(((0, 0, start), []))
- while not fringe.isEmpty():
- (costHere, _, state), directions = fringe.pop()
- if problem.isGoalState(state):
- return directions
- if state not in closed:
- closed.add(state)
- for node, direction, cost in problem.getSuccessors(state):
- fringe.push(((costHere + cost, h(node, problem), node),
- directions + [direction]))
- def depthFirstSearch(problem):
- """
- Search the deepest nodes in the search tree first
- """
- return genericSearch(problem, util.Stack())
- def breadthFirstSearch(problem):
- """
- Search the shallowest nodes in the search tree first.
- """
- return genericSearch(problem, util.Queue())
- def cost(((c, h, n), direction)):
- return c + h
- def uniformCostSearch(problem):
- """
- Search the node of least total cost first.
- """
- return genericSearch(problem, util.PriorityQueueWithFunction(cost))
- def aStarSearch(problem, heuristic=nullHeuristic):
- """
- Search the node that has the lowest combined cost and heuristic first.
- """
- return genericSearch(problem, util.PriorityQueueWithFunction(cost), heuristic)
Advertisement
Add Comment
Please, Sign In to add comment