Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def universalSearch(problem, buffer, heuristic):
- root = SearchNode(position=problem.getStartState())
- buffer.push((root, 0)) # (node, priority)
- visited_states = {str(root.position): root}
- while not buffer.isEmpty():
- curr_node = buffer.pop()[0]
- curr_pos, curr_par, curr_cost, curr_heur = curr_node.unpack()
- if problem.isGoalState(curr_pos):
- return list(reversed(curr_node.backtrack()))
- for next_position, direction, cost in problem.getSuccessors(curr_pos):
- if str(next_position) not in visited_states:
- full_cost = curr_cost + cost
- full_heur = heuristic(next_position, problem)
- new_node = SearchNode(position=next_position, parent=curr_node, transition=direction, cost=full_cost, heuristic=full_heur)
- visited_states[str(next_position)] = new_node
- buffer.push((new_node, new_node.cost + new_node.heuristic))
- else:
- # already pass
- pass
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement