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

编程入门 行业动态 更新时间:2024-10-17 02:53:03
Python priorityQueue.get()返回int而不是object(Python priorityQueue.get() returns int instead of object)

我目前正在开发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()

I am currently developing an AI system in python to solve the bloxorz game using the A* search algorithm.

Naturally, the algorithm stores the nodes in a priority queue,but when i try to get() an element from the queue, it returns an int instead of the object. As I am very new to python, I would appreciate it if someone can clarify. My A* algorithm:

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

My Node class and 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)

上面的行调用带有参数: 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 )。

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

The above line calls put with the arguments: item=child.getH+count, block=count, and timeout=child. As a result, only child.getH+count is be considered as the 'item' that get will retrieve. Try putting all three objects into a tuple:

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

This way, item will be the tuple (child.getH+count, count, child), and the other two arguments, block, and timeout, will stay as their default values (True and None, respectively).

更多推荐

本文发布于:2023-07-27 07:35:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1287151.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:而不是   priorityQueue   Python   int   returns

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!