Skip to content

Instantly share code, notes, and snippets.

@kartikkukreja
Last active August 29, 2015 14:22
Show Gist options
  • Save kartikkukreja/2e2d319588887cc84d5f to your computer and use it in GitHub Desktop.
Save kartikkukreja/2e2d319588887cc84d5f to your computer and use it in GitHub Desktop.
A* Search
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