How can I increase speed up simulation of my least-cost path model

守給你的承諾、 提交于 2019-12-31 01:43:06

问题


By using network extension, the following code builds the least-cost path between two polygons (composed of several patches) :

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 

I applied this code with two polygons that are represented by black rectangles in the figure. By using the profiler extension, this code ran for 14 min.

For each wolf, I would like to build the least-cost path between a polygon where there is a wolf and all polygons that are situated in a radius of 3km around the wolf. Here my code:

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 ] ] ]

The problem is that my procedure "calculate-LCP" runs too slowly to define least-cost paths in buffers around wolves (I have 100 wolves in my model). How can I increase speed up simulation of my model?

Thank you very much for your help.


回答1:


You only need to call nw:set-snapshot turtles links once when you first set up the network. If any weights change, or any links or nodes are added or removed, you'll need to call it again. This should speed up your code tremendously.



来源:https://stackoverflow.com/questions/21839462/how-can-i-increase-speed-up-simulation-of-my-least-cost-path-model

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!