I have a maze as shown below:
|||||||||||||||||||||||||||||||||||| | P| | ||||||||||||||||||||||| |||||||| | | || | | ||||||| || | | || | | | | |||| ||||||||| || ||||| | || | | | | || || | | || | | | | | |||| ||| |||||| | | | | | | | || |||||||| | | || | | |||||||| || || ||||| | || | || ||||||||| || | | |||||| ||||||| || |||||| | |||||| | |||| || | | | |||||| ||||| | || || ||||| | |||||| | ||||| || | | |||||| ||||||||||| || || | |||||||||| |||||| | |+ |||||||||||||||| | ||||||||||||||||||||||||||||||||||||
The goal is to find P + with subgoals
- The path to
+ is the lowest cost (1 hop = cost + 1) - The number of cells found (nodes expanded) is minimized
I am trying to understand why my A * heuristic works much worse than the implementation that I have for Greedy Best First. Here are two bits of code for each:
#Greedy Best First -- Manhattan Distance self.heuristic = abs(goalNodeXY[1] - self.xy[1]) + abs(goalNodeXY[0] - self.xy[0])
In both algorithms, I use heapq , a priority based on a heuristic value. The main search loop is the same for both:
theFrontier = [] heapq.heappush(theFrontier, (stateNode.heuristic, stateNode)) #populate frontier with 'start copy' as only available Node #while !goal and frontier !empty while not GOAL_STATE and theFrontier: stateNode = heapq.heappop(theFrontier)[1] #heappop returns tuple of (weighted-idx, data) CHECKED_NODES.append(stateNode.xy) while stateNode.moves and not GOAL_STATE: EXPANDED_NODES += 1 moveDirection = heapq.heappop(stateNode.moves)[1] nextNode = Node() nextNode.setParent(stateNode) #this makes a call to setHeuristic nextNode.setLocation((stateNode.xy[0] + moveDirection[0], stateNode.xy[1] + moveDirection[1])) if nextNode.xy not in CHECKED_NODES and not isInFrontier(nextNode): if nextNode.checkGoal(): break nextNode.populateMoves() heapq.heappush(theFrontier, (nextNode.heuristic,nextNode))
So now we come to this problem. While A * finds the optimal path, it is quite expensive at the same time. To find the optimal cost:68 path, it expands (moves and searches) 452 nodes for this. 
While the Greedy Best implementation, I found a sub-optimal path (cost: 74) in only 160 extensions.

I'm really trying to figure out where I am going wrong. I understand that the Greedy Best First algorithms can behave so naturally, but the gap in the node extensions is so wide that I feel that something is wrong here. Any help would be appreciated. I am happy to add details if what I inserted above is somewhat unclear.