Advertisement
Guest User

Untitled

a guest
Apr 25th, 2019
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.17 KB | None | 0 0
  1. function reconstruct_path(cameFrom, current)
  2.     total_path := {current}
  3.     while current in cameFrom.Keys:
  4.         current := cameFrom[current]
  5.         total_path.append(current)
  6.     return total_path
  7.  
  8. function A_Star(start, goal)
  9.     // The set of nodes already evaluated
  10.     closedSet := {}
  11.  
  12.     // The set of currently discovered nodes that are not evaluated yet.
  13.     // Initially, only the start node is known.
  14.     openSet := {start}
  15.  
  16.     // For each node, which node it can most efficiently be reached from.
  17.     // If a node can be reached from many nodes, cameFrom will eventually contain the
  18.     // most efficient previous step.
  19.     cameFrom := an empty map
  20.  
  21.     // For each node, the cost of getting from the start node to that node.
  22.     gScore := map with default value of Infinity
  23.  
  24.     // The cost of going from start to start is zero.
  25.     gScore[start] := 0
  26.  
  27.     // For each node, the total cost of getting from the start node to the goal
  28.     // by passing by that node. That value is partly known, partly heuristic.
  29.     fScore := map with default value of Infinity
  30.  
  31.     // For the first node, that value is completely heuristic.
  32.     fScore[start] := heuristic_cost_estimate(start, goal)
  33.  
  34.     while openSet is not empty
  35.         current := the node in openSet having the lowest fScore[] value
  36.         if current = goal
  37.             return reconstruct_path(cameFrom, current)
  38.  
  39.         openSet.Remove(current)
  40.         closedSet.Add(current)
  41.  
  42.         for each neighbor of current
  43.             if neighbor in closedSet
  44.                 continue        // Ignore the neighbor which is already evaluated.
  45.  
  46.             // The distance from start to a neighbor
  47.             tentative_gScore := gScore[current] + dist_between(current, neighbor)
  48.  
  49.             if neighbor not in openSet  // Discover a new node
  50.                 openSet.Add(neighbor)
  51.             else if tentative_gScore >= gScore[neighbor]
  52.                 continue
  53.  
  54.             // This path is the best until now. Record it!
  55.             cameFrom[neighbor] := current
  56.             gScore[neighbor] := tentative_gScore
  57.             fScore[neighbor] := gScore[neighbor] + heuristic_cost_estimate(neighbor, goal)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement