Skip to content

Instantly share code, notes, and snippets.

@pfmiles
Created February 19, 2012 13:51
Show Gist options
  • Save pfmiles/1863935 to your computer and use it in GitHub Desktop.
Save pfmiles/1863935 to your computer and use it in GitHub Desktop.
A* algorithm skeleton
def aStarSearch(problem, heuristic=nullHeuristic):
from sets import Set
visited = Set()
start = problem.getStartState()
from util import PriorityQueue
frontLine = PriorityQueue()
startPri = 0 + heuristic(start, problem)
frontLine.push((start, [], 0), startPri)
while not frontLine.isEmpty():
(point, actions, cost) = frontLine.pop()
visited.add(point)
if problem.isGoalState(point):
return actions
for (p, action, stepCost) in (s for s in problem.getSuccessors(point) if s[0] not in visited):
newCost = cost + stepCost
newActions = actions[:]
newActions.append(action)
newPri = newCost + heuristic(p, problem)
frontLine.push((p, newActions, newCost), newPri)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment