Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Python typeerror #14

Open
greyfeathertx opened this issue Jun 8, 2018 · 4 comments
Open

Python typeerror #14

greyfeathertx opened this issue Jun 8, 2018 · 4 comments

Comments

@greyfeathertx
Copy link

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'

@jweberdj
Copy link

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

@matim999
Copy link

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

@mikepound
Copy link
Owner

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.

@mppaskov
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants