首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何提高最低成本路径模型的模拟速度

如何提高最低成本路径模型的模拟速度
EN

Stack Overflow用户
提问于 2014-02-18 05:19:17
回答 1查看 434关注 0票数 3

通过使用网络扩展,下面的代码在两个多边形(由几个补丁组成)之间构建成本最低的路径:

代码语言:javascript
复制
to calculate-LCP [ID-polygon-1 ID-polygon-2] 
let path []
let path-cost -1

;;;;;;;;;;;;;;;;;;;;;;;;
;; Define polygon edges
ask patches with [plabel != ID-polygon-1] [
 ask neighbors with [plabel = ID-polygon-1] [
  ask nodes-here [
    set color red ] ] ]

ask patches with [plabel != ID-polygon-2] [
 ask neighbors with [plabel = ID-polygon-2] [
  ask nodes-here [
    set color red ] ] ]

;;;;;;;;;;;;;;;;;;;;;;;;
;; Build least-cost path
ask nodes with [color = red] [
 foreach sort nodes-on patches with [ID-polygon = ID-polygon-1] [ 
  let node-on-polygon-1 ?  
  foreach sort nodes-on patches with [ID-polygon = ID-polygon-2] [ 
   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 
     set path nw:weighted-path-to node-on-polygon-2 "link-cost" ] ] ] ] ] 

;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Draw least-cost path
foreach path [ ;; trace le least-cost path 
  ask ? [ set color red
    set thickness 0.2 ] ]
end 

我将这段代码应用于图中用黑色矩形表示的两个多边形。通过使用profiler扩展,此代码运行了14分钟。

对于每一只狼,我想在有一只狼的多边形和位于狼周围3公里半径内的所有多边形之间建立成本最低的路径。下面是我的代码:

代码语言:javascript
复制
ask wolves [ 
 set my-list-of-polygons-in-buffer ( [plabel] of patches in-radius 3 ) 
 set my-list-of-polygons-in-buffer remove-duplicates my-list-of-polygons-in-buffer 
 set my-list-of-polygons-in-buffer remove [plabel] of patch-here my-list-of-polygons-in-buffer 
 set my-list-of-polygons-in-buffer remove "" my-list-of-polygons-in-buffer 

 foreach my-list-of-polygons-in-buffer [ 
 let ID-polygon-in-buffer ?

  ask patches with [plabel = ID-polygon-in-buffer] [ 

   let LCP calculate-LCP [my-ID-polygon] of myself ID-polygon-in-buffer ] ] ]

问题是,我的过程"calculate-LCP“运行太慢,无法在wolves周围的缓冲区中定义最低成本的路径(我的模型中有100条wolves )。如何提高模型的模拟速度?

非常感谢你的帮助。

EN

回答 1

Stack Overflow用户

发布于 2014-02-18 05:30:59

在第一次设置网络时,您只需调用nw:set-snapshot turtles links一次。如果任何权重发生更改,或者添加或删除了任何链接或节点,则需要再次调用它。这应该会极大地加速你的代码。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/21839462

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档