heapq,为两个对象处理相同的值?

时间:2018-07-19 10:18:19

标签: python heap 8-puzzle

在尝试将具有相同值的对象推入堆时发现了一个问题。

    for neighbor in neighbors:
        if neighbor.get_config() not in explored and neighbor.get_config() not in frontier_set:
            print(frontier_heap)
            heapq.heappush(frontier_heap, ((neighbor.get_cost() + calculate_manhattan_dist(neighbor)), neighbor))
            frontier_set.add(neighbor.get_config())

我得到以下输出:

[(12, <__main__.PuzzleState object at 0x008B5EB0>)]

[(10, <__main__.PuzzleState object at 0x008B5E90>), (12, <__main__.PuzzleState object at 0x008B5EB0>)]

Traceback (most recent call last):
  File "c:/stuff/python/AI/P1/driver_3.py", line 343, in <module>
    print(A_star_search(PuzzleState((6,1,8,4,0,2,7,3,5))))
  File "c:/stuff/python/AI/P1/driver_3.py", line 277, in A_star_search
    heapq.heappush(frontier_heap, ((neighbor.get_cost() + calculate_manhattan_dist(neighbor)), neighbor))
TypeError: '<' not supported between instances of 'PuzzleState' and 'PuzzleState'

是否有一种方法可以根据对象的其他功能将对象推入堆中,同时仍将对象保存在堆中?

0 个答案:

没有答案