Python priorityQueue.get() returns int 而不是对象
Python priorityQueue.get() returns int instead of object
我目前正在 python 开发一个 AI 系统来使用 A* 搜索算法解决 bloxorz 游戏。
当然,该算法将节点存储在优先级队列中,但是当我尝试从队列中获取()一个元素时,它 returns 是一个 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
我的节点 class 和 Node_Map class:
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()
priorityQueue.put(child.getH+count, count, child)
以上行调用 put
,参数为:item=child.getH+count
、block=count
和 timeout=child
。结果,只有 child.getH+count
被认为是 get
将检索的 'item'。尝试将所有三个对象放入一个元组中:
priorityQueue.put((child.getH+count, count, child))
这样,item
将成为元组 (child.getH+count, count, child)
,而其他两个参数 block
和 timeout
将保持其默认值(True
和 None
。
我目前正在 python 开发一个 AI 系统来使用 A* 搜索算法解决 bloxorz 游戏。
当然,该算法将节点存储在优先级队列中,但是当我尝试从队列中获取()一个元素时,它 returns 是一个 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
我的节点 class 和 Node_Map class:
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()
priorityQueue.put(child.getH+count, count, child)
以上行调用 put
,参数为:item=child.getH+count
、block=count
和 timeout=child
。结果,只有 child.getH+count
被认为是 get
将检索的 'item'。尝试将所有三个对象放入一个元组中:
priorityQueue.put((child.getH+count, count, child))
这样,item
将成为元组 (child.getH+count, count, child)
,而其他两个参数 block
和 timeout
将保持其默认值(True
和 None
。