Last active
August 29, 2015 14:22
-
-
Save kartikkukreja/2e2d319588887cc84d5f to your computer and use it in GitHub Desktop.
A* Search
This file contains hidden or 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
import heapq | |
class PriorityQueue: | |
def __init__(self, priorityFunction): | |
self.priorityFunction = priorityFunction | |
self.heap = [] | |
def push(self, item): | |
heapq.heappush(self.heap, (self.priorityFunction(item), item)) | |
def pop(self): | |
(_, item) = heapq.heappop(self.heap) | |
return item | |
def empty(self): | |
return len(self.heap) == 0 | |
def astarGraphSearch(problem, heuristic): | |
# A* uses path cost from start state + heuristic estimate to a goal | |
totalCost = lambda state: len(state[1]) + heuristic(state) | |
return graphSearch(problem, PriorityQueue(totalCost)) | |
# problem is an instance of PacmanProblem | |
# food is the position of the food pellet (r,c) | |
def pacmanPathFinder(problem, food): | |
manhattanDistanceHeuristic = lambda state: abs(state[0][0]-food[0]) + abs(state[0][1]-food[1]) | |
return astarGraphSearch(problem, manhattanDistanceHeuristic) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment