我有所产生的瓷砖很难保持跟踪getAdjacentTiles(..)
我已经确定我的A *实现的性能问题,下面是我不保持之前已经看到了瓷砖的跟踪了解,向每一个电话getAdjacentTiles
返回新砖( Node
的),而不是任何的瓷砖openSet
或closedSet
。 我决定使用Node对象的列表,所有创建至今的瓷砖,并传递到getAdjacentTiles
以确定它是否产生了瓷砖已被访问。
我的问题是,我似乎无法跟踪这些砖正常。 每当我的A *需要采取比约4动作更加才能到end
就胡扯出来的位置。 这我相信有我怎么想跟踪瓷砖(再次做Node
,我将不得不怀疑问题是与我的Python的知识已被访问的),我是允许做.apped(tile)
就像我在做getAdjacentTiles(...)
通过循环时allTiles
设置?
下面就来,导致我这个问题的链接
所产生的误差(有时,只有当A *路径是大于约3步长..)
File "P3.py", line 67, in aStar
openSet.remove(curNode)
KeyError: <__main__.Node instance at 0xa39edcc>
资源
#Perform an A* search to find the best path to the dirt
def aStar(self, current, end):
openSet = set()
openHeap = []
closedSet = set()
allTiles = set()
curNode = Node(0, current, self.manHatDist(current, end))
openSet.add(curNode)
allTiles.add(curNode)
openHeap.append((curNode.cost,curNode))
while openSet:
curNode = heapq.heappop(openHeap)[1]
if curNode.pos == end:
return self.getDirections(curNode)
openSet.remove(curNode)
closedSet.add(curNode)
adjNodes = self.getAdjacentNodes(curNode.pos, allTiles)
for tile in adjNodes:
t = tile
if t not in closedSet:
cost = (curNode.cost - self.manHatDist(curNode.pos, end)
+ self.euclidDist(curNode.pos, current)
+ self.manHatDist(t.pos, end))
if t not in openSet or cost < t.cost:
t.parent = curNode
t.cost = cost
openSet.add(t)
heapq.heappush(openHeap, (cost,t))
allTiles.add(t)
return []
#Get the moves made to get to this endNode
def getDirections(self, endNode):
moves = []
tmpNode = endNode
while tmpNode.parent is not None:
moves.append(tmpNode.value)
tmpNode = tmpNode.parent
moves.reverse()
return moves
#Return all possible moves from given tile as Node objects
def getAdjacentNodes(self, curPos, allTiles):
allMoves = ['North','South','East','West']
posMoves = []
for direction in allMoves:
if(self.canMove(direction, curPos)):
posMoves.append(Node(direction, self.getLocIfMove(curPos, direction)))
retNodes = []
for posLocNode in posMoves:
set = False
for tile in allTiles:
if(posLocNode.pos == tile.pos):
set = True
retNodes.append(tile)
if(not set):
retNodes.append(posLocNode)
return retNodes