- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我的 a* 算法并不总是采用最短路径。
在这张图片中,机器人必须穿过黑色方 block ,河流和树木是障碍物。黑线是它走过的路径,这显然不是最短路径,因为它不应该倾斜。
这是我的 a* 代码和我正在使用的启发式算法:
def HeuristicCostEstimate(start, goal):
(x1, y1) = start
(x2, y2) = goal
return abs(x1 - x2) + abs(y1 - y2)
def AStar(grid, start, goal):
entry = 1
openSet = []
heappush(openSet,(1, entry, start))
cameFrom = {}
currentCost = {}
cameFrom[tuple(start)] = None
currentCost[tuple(start)] = 0
while not openSet == []:
current = heappop(openSet)[2]
print(current)
if current == goal:
break
for next in grid.Neighbours(current):
newCost = currentCost[tuple(current)] + grid.Cost(current, next)
if tuple(next) not in currentCost or newCost < currentCost[tuple(next)]:
currentCost[tuple(next)] = newCost
priority = newCost + HeuristicCostEstimate(goal, next)
entry +=1
heappush(openSet,(priority, entry, next))
cameFrom[tuple(next)] = current
return cameFrom, current
感谢您的帮助!并随时要求我澄清任何事情。
编辑:通过返回 0 删除启发式解决了这个问题。这表明问题出在我的启发式上。谁知道可能是什么原因造成的?
最佳答案
A* 并不总能保证找到最短路径。虽然没有启发式 (h(n) = 0) 确实会找到最短路径(它成为 Dijkstra 算法),但这并不意味着使用任何启发式都会找到最短路径。添加启发式以加快搜索速度,但代价是在某些情况下您找不到最短路径。
要了解发生了什么,请记住启发式是对目标实际距离的估计。如果预测是完美的,则该图基本上是预先计算好的。考虑以下情况。
如果您的启发式算法低于实际成本,则最短路径会被发现。
如果启发式等于实际成本,则所有最短路径都是本质上是预先计算的,并且会找到最短路径没有任何不必要的探索。
如果启发式有时大于实际成本,则 A*不保证找到最短路径,但搜索时间可能比启发式低估要快。
您的启发式方法似乎低估了成本。也可能是您的邻居生成或成本计算器有问题。
进一步阅读:http://theory.stanford.edu/~amitp/GameProgramming/Heuristics.html
关于python - 为什么我的 a* 算法不走最短路线?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35802241/
我想递归地遍历一个目录,但我希望 python 在遇到包含超过 100 个文件的目录时从任何单个 listdir 中断。基本上,我正在搜索 (.TXT) 文件,但我想避免使用包含大型 DPX 图像序列
我正在尝试遍历列表(例如 sql 行)并为每一行触发例程。问题是传递给函数的值不会在运行时进行评估,因此根据函数执行所需的时间,它可能会使用下一行中的任何值而不是当前行。 我知道我可以在普通函数中提取
我需要以毫秒为单位的时间来处理大量事务,因此我想要正确且快速的东西。下面的工作会做得最好吗? : iMilli := int((time.Nanoseconds() % 1e6) / 1e3
我有以下目录/文件设置(已简化): Ce +---top.txt +---X0.0 | | | +---Y0.0 | | | | |
我遇到了类似的问题: Connecting to Redis To Go with PHP 基本上,我在 redis 中有这个 uri: redis://myusername:foopassword@
我阅读了下面的主题 Go: multiple value in single-value context 但我不明白这个解释在我的案例中。可能是因为我想使用 interface 在下面的情况下,我得到
我有一个模板,我想使用 text/template 评估各个字段包裹。我很难弄清楚评估应该如何工作,因为下面的代码似乎失败了。模板包是否足够强大以处理此类评估? type something stru
我编写了简单的服务器程序来从客户端接收数据。我有点不明白有时我从函数中得到错误 read tcp4 IP:PORT i/o timeoutint, err := conn.Read([]byte) 未
我只需要解码和更新 json 对象的特定值。问题是我不知道对象的完整结构。 encoding/json 包“忽略”/截断结构中未提供的字段,因此在编码时这些字段将丢失。 我想知道是否可以只解码我知道的
我正在尝试使用带有 C++ 目标的 ANTLR4 来实现 TSql 解析器。我抓取了语法文件 here .该jar用于制作相应的源文件(因冲突将TSqlParser.cpp中的NULL全部改为null
我在 win7 中使用 python 3.3.3 - 我只想列出网络目录中的所有文件。 import os for root, dirs, files in os.walk("X:\\network\
当我运行 go 脚本 ( go run example.go ) 时出现此错误 /home/travis/.gvm/gos/go1.1.2/src/pkg/github.com/user/exampl
我正在尝试通过 gmail API 发送电子邮件使用 Go但我发现文档非常有缺陷/令人困惑。这一次我看不到收据字段和电子邮件正文。 我不需要上传任何东西,所以我找到了 Simple upload ,
本人是一名专业的windows/.Net开发者,一直在慢慢学习rails/ruby/python/etc。在我有空的时候。在过去 8 年左右的时间里,我也一直在使用各种 Linux 发行版。然而,有一
我想知道是否可以使用 std http 来响应 http 请求打包并仍然保持 go 例程事件(例如运行任务密集型任务)。用例是我需要接收一个 http 请求,然后在几分钟后回调该服务 最佳答案 只需从
我想知道关于指针的最佳实践是什么。我应该在结构上还是在其字段上定义它们。我虽然定义一个指向结构本身的指针是有意义的,但这里有一个我觉得很有趣的例子。如果所有字段都是指针,为什么我不应该使用指向整个结构
我是一名优秀的程序员,十分优秀!