继 How to speed up least-cost path model at large spatial extents ,我尝试在 Netlogo 中编写 A* 算法,以在较大的空间范围内增加我的最低成本路径模型。这是我的代码:
to findPath [ID-start-node ID-end-node]
let currentNodesInList [ ]
let current-node node ID-start-node
let end-node node ID-end-node
ask current-node [ set color red]
ask end-node [ set color red]
set currentNodesInList lput current-node currentNodesInList
while [not member? end-node currentNodesInList] [
ask current-node [
foreach sort nodes-on neighbors [
ask ? [set f-value [link-cost] of link ([who] of current-node) ([who] of ?) + distance end-node] ]
let next-current-node min-one-of [nodes-on neighbors] of current-node [f-value]
ask link ([who] of current-node) ([who] of next-current-node) [set color red]
set current-node next-current-node
set currentNodesInList lput current-node currentNodesInList] ]
end
当 ID-start-node 和 ID-end-node 在景观中接近时,代码似乎可以工作。但是,当 ID-start-node 和 ID-end-node 之间的距离较大时,路径不会到达 ID-end-node(见下图;但有时,代码有效)。
图中,ID-start-node和ID-end-node用红色起点表示,路径用红色绘制。
非常感谢您的帮助。
最佳答案
您可能想在 NetLogo 用户社区中查看此模型:
http://ccl.northwestern.edu/netlogo/models/community/Astardemo1
它实现了一个简单的过程(find-a-path),该过程将源补丁和目标补丁作为参数,并使用 A- 返回补丁列表(这是从源补丁到目标补丁的最短路径之一)星形最短路径寻找算法。
您也可以尝试关闭世界包装。
关于a-star - 路径未到达我的 A* 算法中的结束节点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23438674/