Advertisement
Guest User

Untitled

a guest
Mar 24th, 2019
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.03 KB | None | 0 0
  1. def universalSearch(problem, buffer, heuristic):
  2. root = SearchNode(position=problem.getStartState())
  3. buffer.push((root, 0)) # (node, priority)
  4. visited_states = {str(root.position): root}
  5.  
  6. while not buffer.isEmpty():
  7.  
  8. curr_node = buffer.pop()[0]
  9. curr_pos, curr_par, curr_cost, curr_heur = curr_node.unpack()
  10.  
  11. if problem.isGoalState(curr_pos):
  12. return list(reversed(curr_node.backtrack()))
  13.  
  14. for next_position, direction, cost in problem.getSuccessors(curr_pos):
  15.  
  16. if str(next_position) not in visited_states:
  17. full_cost = curr_cost + cost
  18. full_heur = heuristic(next_position, problem)
  19. new_node = SearchNode(position=next_position, parent=curr_node, transition=direction, cost=full_cost, heuristic=full_heur)
  20. visited_states[str(next_position)] = new_node
  21. buffer.push((new_node, new_node.cost + new_node.heuristic))
  22.  
  23. else:
  24. # already pass
  25. pass
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement