【发布时间】:2015-04-24 08:30:16
【问题描述】:
我有一个像下面这样的迷宫:
||||||||||||||||||||||||||||||||||||
| P|
| ||||||||||||||||||||||| |||||||| |
| || | | ||||||| || |
| || | | | | |||| ||||||||| || |||||
| || | | | | || || |
| || | | | | | |||| ||| |||||| |
| | | | | | || |||||||| |
| || | | |||||||| || || |||||
| || | || ||||||||| || |
| |||||| ||||||| || |||||| |
|||||| | |||| || | |
| |||||| ||||| | || || |||||
| |||||| | ||||| || |
| |||||| ||||||||||| || || |
|||||||||| |||||| |
|+ |||||||||||||||| |
||||||||||||||||||||||||||||||||||||
目标是P找到+,子目标为
- 到
+的路径成本最低(1 跳 = 成本+1) - 搜索到的单元格数量(节点扩展)已最小化
我试图了解为什么我的 A* 启发式算法的性能比 Greedy Best First 的实现差得多。以下是每个代码的两位代码:
#Greedy Best First -- Manhattan Distance
self.heuristic = abs(goalNodeXY[1] - self.xy[1]) + abs(goalNodeXY[0] - self.xy[0])
#A* -- Manhattan Distance + Path Cost from 'startNode' to 'currentNode'
return abs(goalNodeXY[1] - self.xy[1]) + abs(goalNodeXY[0] - self.xy[0]) + self.costFromStart
在这两种算法中,我都使用heapq,根据启发式值进行优先级排序。两者的主要搜索循环是相同的:
theFrontier = []
heapq.heappush(theFrontier, (stateNode.heuristic, stateNode)) #populate frontier with 'start copy' as only available Node
#while !goal and frontier !empty
while not GOAL_STATE and theFrontier:
stateNode = heapq.heappop(theFrontier)[1] #heappop returns tuple of (weighted-idx, data)
CHECKED_NODES.append(stateNode.xy)
while stateNode.moves and not GOAL_STATE:
EXPANDED_NODES += 1
moveDirection = heapq.heappop(stateNode.moves)[1]
nextNode = Node()
nextNode.setParent(stateNode)
#this makes a call to setHeuristic
nextNode.setLocation((stateNode.xy[0] + moveDirection[0], stateNode.xy[1] + moveDirection[1]))
if nextNode.xy not in CHECKED_NODES and not isInFrontier(nextNode):
if nextNode.checkGoal(): break
nextNode.populateMoves()
heapq.heappush(theFrontier, (nextNode.heuristic,nextNode))
所以现在我们来解决这个问题。虽然 A* 找到了 最佳 路径,但这样做的成本相当高。为了找到cost:68 的最佳路径,它会扩展(导航和搜索)452 个节点来执行此操作。
虽然我使用的 Greedy Best 实现仅在 160 次扩展中找到了一条次优路径(成本:74)。
我真的很想了解我在哪里出错了。我意识到 Greedy Best First 算法可以自然地表现出这样的行为,但节点扩展的差距是如此之大,我觉得这里有一些问题必须..任何帮助将不胜感激。如果我在上面粘贴的内容在某些方面不清楚,我很乐意添加详细信息。
【问题讨论】:
-
别在意我以前的 cmets。这是完全正常的行为。我一开始以为 + 是开始。很难找到此类问题的最佳解决方案;这就是我们经常不打扰的原因。
-
我发现的一件事并没有真正解决两者之间的差异,但确实提高了整体效率是在主循环的末尾添加以下内容:
CHECKED_NODES.append(nextNode.xy)- 这似乎两种算法的扩展都减少了一半...
标签: python artificial-intelligence path-finding a-star heuristics