heatherhb

Manhattan Heuristic

Nov 4th, 2011
318
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.93 KB | None | 0 0
  1. def cornersHeuristic(state, problem):
  2.   """
  3.  A heuristic for the CornersProblem that you defined.
  4.  
  5.    state:   The current search state (a data structure you chose in your search problem)
  6.    problem: The CornersProblem instance for this layout.  
  7.    
  8.  This function should always return a number that is a lower bound
  9.  on the shortest path from the state to a goal of the problem; i.e.
  10.  it should be admissible.  (You need not worry about consistency for
  11.  this heuristic to receive full credit.)
  12.  """
  13.   corners = problem.corners # These are the corner coordinates
  14.   walls = problem.walls # These are the walls of the maze, as a Grid (game.py)
  15.  
  16. #Manhattan heuristic:
  17.   best = 999999
  18.   for corner in corners:
  19.     #Find manhattan value
  20.     xy1 = state.getLoc()
  21.     xy2 = corner
  22.     value = abs(xy1[0] - xy2[0]) + abs(xy1[1] - xy2[1])
  23.     if value < best:
  24.       best = value
  25.   #Return smallest manhattan value
  26.   return best
  27.  
Advertisement
Add Comment
Please, Sign In to add comment