我目前正在开发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.pathMy 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).
更多推荐
发布评论