I am trying to utilize a PriorityQueue from the queue class. However, i'm having issues putting custom objects into my PQ. I have implemented the __cmp__
function below:
def __cmp__(self, other):
return (self.priority > other.priority) - (self.priority < other.priority)
I want the PriorityQueue to be sorted by the priority field, as assigned in my init function:
def __init__(self, board, priority=0):
self.priority = priority
# Other logic
However, when I run the code to insert a State object into the PQ, I get this error: TypeError: '<' not supported between instances of 'State' and 'State'
Here is the code that runs the PQ.
if op.precond(S):
new_state = op.state_transf(S)
if not (OPEN.queue.__contains__(new_state)) and not (new_state in CLOSED):
GVALUES[Problem.hash(new_state)] = get_distance_value(op, new_state)
HEUR_VALUES[Problem.hash(new_state)] = get_AStar_value(new_state)
print("NEW STATE: " + str(new_state))
OPEN.put(new_state)
print("OPEN: " + str(OPEN.queue))
Where OPEN is the priorityQueue.
Any help would be greatly appreciated... as it should be pretty straightforward to insert a value into a PQ.
__cmp__
isn't a special method name in Python 3. Try defining__lt__
instead and see if that works. – Rubirubia