mazesolving icon indicating copy to clipboard operation
mazesolving copied to clipboard

Python typeerror

Open greyfeathertx opened this issue 7 years ago • 4 comments

I don't program in Python but it seems "Node" doesn't have a method for '>' operator?

C:\Users\micha\Desktop\mazesolving-master>python solve.py -m astar perfect4k.png 4k_solve.png Loading Image Creating Maze Node Count: 2865504 Time elapsed: 32.22657132148743

Starting Solve: A-star Search Traceback (most recent call last): File "solve.py", line 89, in main() File "solve.py", line 86, in main solve(sf, args.method, args.input_file, args.output_file) File "solve.py", line 29, in solve [result, stats] = solver(maze) File "C:\Users\micha\Desktop\mazesolving-master\astar.py", line 80, in solve unvisited.insert(vnode) File "C:\Users\micha\Desktop\mazesolving-master\priority_queue.py", line 59, in insert heapq.heappush(self.pq, entry) TypeError: '<' not supported between instances of 'Node' and 'Node'

greyfeathertx avatar Jun 08 '18 21:06 greyfeathertx

I'm getting the same error as well. Anyone have any idea why we're experiencing this error?

jweberdj avatar Nov 27 '18 21:11 jweberdj

I'm facing the same problem when i use Python3. Since error is not solved yet, you can temporary use Python2.

matim999 avatar Nov 29 '18 14:11 matim999

Hi. There may be some python 2 / 3 issues! I wrote this in py2, I'm also pretty much only using Python 3 now. I'm happy for suggested fixes, but ideally they'd need to work with both versions otherwise we'd simply be breaking it for others.

mikepound avatar Dec 20 '18 13:12 mikepound

The issue is cause by how python 2 and 3 sort tuples of the type (int, CustomObject), in this case those tuples are (int, Maze.node). Tuples are normally sorted first by the first element then by second etc. For this work we actually only care about the sorting based on the first element anyway, if different nodes have the same weight it does not really matter which one comes first, but pythons 2 and 3 see that differently.

  • Python 2: If the tuples have the same first element and the second element is object it does not try to sort by the object.
  • Python 3: If the tuples have the same first element and it insist to compare the second element, which Maze.Node and it does not know how to.

Solution: Implement a comparison for Maze.Node and as we dont really care about the order it ca be as simple as:

def __lt__(self, other) -> bool:
    return True

This should be added under Node class inside the Maze class.

I do not believe that this will effect the Python 2 implementation significantly.

mppaskov avatar Feb 23 '19 05:02 mppaskov