Created
February 19, 2012 13:51
-
-
Save pfmiles/1863935 to your computer and use it in GitHub Desktop.
A* algorithm skeleton
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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