Мне трудно отслеживать плитки, сгенерированные getAdjacentTiles(..)
. Я определил проблему производительности моей реализации A* ниже, так как я не отслеживаю плитки, которые видел раньше, каждый вызов getAdjacentTiles
возвращает новые плитки (Node
), а не плитки в openSet
или closedSet
. Я решил использовать список объектов Node в качестве всех тайлов, созданных на данный момент, и передать его в getAdjacentTiles
, чтобы определить, был ли уже посещен созданный им тайл.
Моя проблема в том, что я не могу правильно отслеживать эти плитки. Всякий раз, когда моему A* требуется более 4 движений, чтобы добраться до места end
, он срывается. Что, я уверен, связано с тем, как я пытаюсь отслеживать плитки (опять же Node
, которые были посещены). Я должен подозревать, что проблема связана с моим знанием python. 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
openSet
на каждом шаге? - person Evgenii   schedule 02.10.2013manHatDist
стоит заглавная Н? - person Gareth Rees   schedule 03.10.2013