python中的*实现没有按预期的路径进行
我正试图用python编写自己的A*实现,但代码没有按预期工作。它在某种程度上适用于直对角线,但它看起来并不正确python中的*实现没有按预期的路径进行,python,a-star,Python,A Star,我正试图用python编写自己的A*实现,但代码没有按预期工作。它在某种程度上适用于直对角线,但它看起来并不正确 def compute(self): while (self.openList): self.openList.sort(key=attrgetter("f")) currentNode = self.openList[0] self.openList.remove(currentNode)
def compute(self):
while (self.openList):
self.openList.sort(key=attrgetter("f"))
currentNode = self.openList[0]
self.openList.remove(currentNode)
self.closedList.append(copy(currentNode))
if (currentNode.x == self.end[0] and currentNode.y == self.end[1]):
print("Done computing")
return self.retracePath(currentNode)
for x in range(-1, 2): # -1, 0, 1
for y in range(-1, 2):
if (currentNode.x + x < 0 or currentNode.x + x >= len(self.grid.nodes)):
continue
if (currentNode.y + y < 0 or currentNode.y + y >= len(self.grid.nodes[0])):
continue
if (x == 0 and y == 0):
continue
neighbourCell = self.grid.nodes[currentNode.x + x][currentNode.y + y]
print(neighbourCell.x, neighbourCell.y)
gCost = currentNode.g + self.calculateHeuristic(currentNode.x, currentNode.y, neighbourCell.x, neighbourCell.y) + neighbourCell.value
if (gCost < neighbourCell.g or (not self.findInOpenList(neighbourCell))):
neighbourCell.g = gCost
neighbourCell.h = self.calculateHeuristic(neighbourCell.x, neighbourCell.y, self.end[0], self.end[1])
neighbourCell.f = neighbourCell.g + neighbourCell.h
neighbourCell.parent = currentNode
if (self.findInClosedList(neighbourCell)):
continue
if (not self.findInOpenList(neighbourCell)):
self.openList.append(copy(neighbourCell))
def计算(自):
while(self.openList):
self.openList.sort(key=attrgetter(“f”))
currentNode=self.openList[0]
self.openList.remove(currentNode)
self.closedList.append(复制(当前节点))
如果(currentNode.x==self.end[0]和currentNode.y==self.end[1]):
打印(“完成计算”)
返回self.retracePath(currentNode)
对于范围(-1,2)内的x:#-1,0,1
对于范围(-1,2)内的y:
如果(currentNode.x+x<0或currentNode.x+x>=len(self.grid.nodes)):
持续
如果(currentNode.y+y<0或currentNode.y+y>=len(self.grid.nodes[0]):
持续
如果(x==0和y==0):
持续
neightourcell=self.grid.nodes[currentNode.x+x][currentNode.y+y]
打印(neighbourCell.x,neighbourCell.y)
gCost=currentNode.g+self.calculateEuristic(currentNode.x,currentNode.y,neightourcell.x,neightourcell.y)+neightourcell.value
如果(gCost
我尝试了几种调试方法,并试图重写代码的大部分。我也尝试过一些启发式函数,但我自己似乎无法解决
欢迎来到堆栈溢出。如果你问一个更好的问题,你可能会得到更好的回答。请阅读。你的启发是什么?单元格的
值是多少?@molbdnilo单元格的值用于障碍物,即障碍物的值是inf,但我目前没有使用它,只是将其设置为0。目前,我使用切比雪夫距离作为启发,但也尝试了曼哈顿距离和欧几里得距离。