继How can I increase speed up simulation of my least-cost path model后,我试图找到一种方法来加速以较大空间尺度运行的最低成本路径模型。我的景观有100平方公里的范围(1000个补丁x 1000个补丁,1个补丁= 1个像素)。
目前,我的代码似乎工作但运行速度很慢:我的代码需要几个小时来绘制一个成本最低的路径。问题是我的模型在100天内按照两分钟的离散时间步长发展,我的风景中有1000只狼。对于每只狼和每个时间步,我的模型必须建立一个多边形(由几个补丁组成)之间的最低成本路径,其中有一只狼和所有多边形,位于狼周围3公里半径范围内。
我使用了NW-extension的新版本,我尝试通过保存已经计算的所有成本来优化我的模型,以避免重新绘制成本最低的路径。不幸的是,我的代码太慢了,我不知道如何加快它。这是我的代码,用于计算最低成本路径的成本(如果需要,我可以向某人提供更多代码)
to-report path-cost-between-polygons [ID-polygon-1 ID-polygon-2]
let path-cost -1
;; Define polygon edges
ask patches with [ID-polygon != ID-polygon-1] [
ask neighbors with [ID-polygon = ID-polygon-1] [
ask nodes-here [
set color red ] ] ]
ask patches with [ID-polygon != ID-polygon-2] [
ask neighbors with [ID-polygon = ID-polygon-2] [
ask nodes-here [
set color red ] ] ]
;; Calculate path cost
foreach sort ( (nodes-on patches with [ID-polygon = ID-polygon-1]) with [color = red] ) [
let node-on-polygon-1 ?
foreach sort ( (nodes-on patches with [ID-polygon = ID-polygon-2]) with [color = red] ) [
let node-on-polygon-2 ?
ask node-on-polygon-1 [
let cost nw:weighted-distance-to node-on-polygon-2 "link-cost"
if path-cost = -1 or cost < path-cost [
set path-cost cost ] ] ] ]
;; Save path cost between polygons
set list-ID-polygons lput (list ID-polygon-1 ID-polygon-2) list-ID-polygons
set list-path-costs-between-polygons lput path-cost list-path-costs-between-polygons
ask nodes with [color = red] [ set color white]
report path-cost
end
非常感谢你的帮助。
答案 0 :(得分:2)
我目前正在开发新版本的NW-extension,速度要快得多。不幸的是,对于加权路径,在NetLogo 5.0.6发布之前它将无法使用。
在此之前,您可以编写自己的最低成本路径代码。 A*对你的情况来说是完美的。实施肯定会有点棘手,但可行。如果我有时间,我会给它拍摄并在此发布。