Python priorityQueue.get()返回int而不是object

时间:2015-03-06 19:17:19

标签: python search queue

我目前正在python中开发一个AI系统来使用A *搜索算法来解决bloxorz游戏。

当然,算法将节点存储在优先级队列中,但是当我尝试从队列中获取()一个元素时,它返回一个int而不是该对象。由于我是python的新手,如果有人能澄清,我将不胜感激。 我的A *算法:

class Astar:
def __init__(self, start):
    self.path = []
    self.visitedQueue = []
    """hold visited in a queue to avoid duplicates"""
    self.priorityQueue = PriorityQueue()

    self.start = start

def Solve(self):
    """the a* algorithm"""
    StartNode = Node_Map(self.start, 0, self.start)
    count = 0
    self.priorityQueue.put(0, count, StartNode)
    while not self.path and self.priorityQueue.qsize():
        closestChild = self.priorityQueue.get()[2]
        closestChild.createChildren()
        self.visitedQueue.append(closestChild.matrix)
        for child in closestChild.children:
            if child.matrix not in self.visitedQueue:
                count += 1
                if child.getH == 0:
                    self.path = child.path
                    break
                self.priorityQueue.put(child.getH+count, count, child)
                """ put in priority queue according to f(n)=h(n)+g(n)"""
    if not self.path:
        print("goal not possible")
    return self.path

我的Node类和Node_Map类:

class Node(object):
def __init__(self, matrix, parent, start=0):
    self.children = []
    self.matrix = {}
    self.parent = parent
    self.xPos = 0
    self.yPos = 0
    self.goalX = 0
    self.goalY = 0

    if parent:
        self.path = parent.path[:]
        self.start = parent.start
        self.path.append = [matrix]

    else:
        self.path = [matrix]
        self.start = start

def getDist(self):
    """ abstract function to get our estimated distance to the goal"""
    pass

def createChildren(self):
    """absract to create children from successor actions"""
    pass


class Node_Map(Node):
def __init__(self, matrix, parent, start=0):
    super(Node_Map, self).__init__(matrix, parent, start)
    self.h = self.getH()

1 个答案:

答案 0 :(得分:0)

priorityQueue.put(child.getH+count, count, child)

上述行使用putitem=child.getH+countblock=count参数调用timeout=child。因此,只有child.getH+count被视为get将检索的“项目”。尝试将所有三个对象放入元组中:

priorityQueue.put((child.getH+count, count, child))

这样,item将成为元组(child.getH+count, count, child),其他两个参数blocktimeout将保留为默认值({{1分别是}和True