如何提高我的成本最低的路径模型的仿真速度 [英] How can I increase speed up simulation of my least-cost path model

查看:40
本文介绍了如何提高我的成本最低的路径模型的仿真速度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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 

我将此代码应用于两个多边形(在图中由黑色矩形表示).通过使用事件探查器扩展,此代码运行了14分钟.

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.

对于每只狼,我想在有狼的多边形与所有围绕狼的半径3km之内的多边形之间建立成本最低的路径.这是我的代码:

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

问题是我的过程"calculate-LCP"运行太慢,无法在狼周围的缓冲区中定义成本最低的路径(我的模型中有100只狼).如何增加模型的仿真速度?

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?

非常感谢您的帮助.

推荐答案

首次设置网络时,只需调用一次nw:set-snapshot turtles links.如果权重发生变化,或者添加或删除了任何链接或节点,则需要再次调用它.这样可以极大地加快您的代码的速度.

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.

这篇关于如何提高我的成本最低的路径模型的仿真速度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆