laurentluce / python-algorithms

Algorithms implemented in Python

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Python3 A Star Test TypeError: '<' not supported between instances of 'Cell' and 'Cell'

HaxiSnake opened this issue · comments

commented

Hello!Thanks for your A Star introduction!
When I try to run your test_a_star_path_finding.py in python3.6,it gets this error:

Traceback (most recent call last):
File "g:/workspace_local/Project/forecast/Code/RL_NAV/astar/test_a_star_path_finding.py", line 25, in test_maze_no_walls
path = a.solve()
File "g:\workspace_local\Project\forecast\Code\RL_NAV\astar\a_star_path_finding.py", line 145, in solve
heapq.heappush(self.opened,(adj_cell.f, adj_cell))
TypeError: '<' not supported between instances of 'Cell' and 'Cell'

There may be something different between python3.6 and python2.7

I'm trying to fix it .
Hope you have time to have a look!

Thank you!


I know this is a while after your post, but just in case anyone else like me runs across it, I was able to figure a solution out.

The error is occurring when there are two choices to make that both have the same "f" value. heappush then moves on the the value itself, which is where it cannot use the "<" operator between two instances of the object "Cell". You can identify in the class "Cell" how to handle that by putting in a special method. Here is my new Cell class:

class Cell(object):
    def __init__(self, x, y, reachable):
        """Initialize new cell.

        @param reachable is cell reachable? not a wall?
        @param x cell x coordinate
        @param y cell y coordinate
        @param g cost to move from the starting cell to this cell.
        @param h estimation of the cost to move from this cell
                 to the ending cell.
        @param f f = g + h
        """
        self.reachable = reachable
        self.x = x
        self.y = y
        self.parent = None
        self.g = 0
        self.h = 0
        self.f = 0

    def __lt__(self, other):
        return self.f < other.f
commented

Thank you for your response,i have done the same with your new method after my post,and it works.Thank you again for your work!My English is bad,i‘m sorry.-_-

YenKShortestPaths.zip
i am getting this error
heapq.heappush(self.pathHeap, candidate)
TypeError: '<' not supported between instances of 'WeightedPath' and 'WeightedPath'
can u help me with this

Since Python 2.7 is counting out this year, I decided to make a fresh start with 3.7 on my new laptop. I used 2to3 and all seemed to be fine until this same problem popped up. I don't really understand your explanation but your suggestion works! This makes me a bit concerned about version 3.x. What other hidden problems lurk?

Thanks for the fix, that appears to solve the error. However my unit test cases (the ones provided with this package) fail. Either it was always wrong or this fix breaks that now.

AssertionError: Lists differ: [(0, [16 chars]2), (1, 2), (1, 3), (1, 4), (2, 4), (3, 4), (3[32 chars], 5)] != [(0, [16 chars]2), (0, 3), (0, 4), (1, 4), (2, 4), (3, 4), (3[32 chars], 5)]
First differing element 3:
(1, 2)
(0, 3)

[(0, 0),
(0, 1),
(0, 2),

  • (1, 2),
  • (1, 3),
    ? ^
  • (0, 3),
    ? ^

  • (0, 4),
    (1, 4),
    (2, 4),
    (3, 4),
    (3, 3),
    (4, 3),
    (5, 3),
    (5, 4),
    (5, 5)]


Ran 3 tests in 7.393s

FAILED (failures=1)

Code should now work for both 2.7 and >= 3.6.