Last active
August 29, 2015 14:22
-
-
Save kartikkukreja/777b23420c2a5712d536 to your computer and use it in GitHub Desktop.
Greedy 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 greedySearch(problem, heuristic): | |
return graphSearch(problem, PriorityQueue(heuristic)) | |
# 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 greedySearch(problem, manhattanDistanceHeuristic) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment