I am trying to implement A* algorithm using pri_que by leveraging python library heapq. In general, all the state of will be stored as a Node instance
class Node:
def __init__(self, state, parent, action, depth):
self.path_cost = 0
self.state = state
self.parent = parent
self.action = action
self.depth = depth
The code is too long to paste here, but the error locates in the following block.
def astar_insert(self, node):
hq.heappush(self.pri_que, (node.path_cost, node))
self.visited_pri.add(str(node.state))
def astar_pop(self):
node = hq.heappop(self.pri_que)[1] # <-------------------- this line
self.visited_pri.discard(str(node.state))
return node
The full error is:
TypeError: '<' not supported between instances of 'Node' and 'Node'
I am very confused. I try to run the code and print the solution. It seems like the code works for a well before failing.
Node? How do you expect twoNodeinstances to be compared?Nodesby putting them into a heap queue. That's how the heap algorithm works. If you can't compareNodes, they can't be used in a heaptuples will never fall back to comparing theNodeinstances. But comparing on costs alone is highly unlikely to achieve that (and clearly didn't, in the OP's case).