如何加快多边形面积的计算?

时间:2014-05-26 16:23:41

标签: netlogo

以下代码允许计算大型景观(900 x 900补丁)中白色多边形的面积(ha)。但是这段代码太慢了。有没有办法加快计算速度?

 let list-ID-blocks ( [ID-polygon] of patches with [pcolor = white] )
 foreach list-ID-blocks [ 
 ask patches with [ID-polygon = ?] [ 
 set polygon-area ( count patches with [ID-polygon = ?] ) * 0.01 ] ]

提前感谢您的帮助。

1 个答案:

答案 0 :(得分:4)

问题是每个补丁都是独立计算区域的(count patches with [ ID-polygon = ? ]每次调用时都会遍历每个补丁)。相反,您可以计算每个多边形的面积一次,如下所示:

let list-ID-blocks remove-duplicates [ID-polygon] of patches with [pcolor = white]
foreach list-ID-blocks [
  let area 0.01 * count patches with [ ID-polygon = ? ]
  ask ask patches with [ ID-polygon = ? ] [ set polygon-area area ]
]

那应该快得多。如果它足够快,我就会这样做。但是,它仍然会为每个多边形查看每个补丁一次。如果多边形id只是介于0和某个数字之间的整数,那么您可以查看每个补丁一次,计算区域:

let max-id max [ ID-polygon ] of patches with [ pcolor = white ]
let areas n-values max-id [ 0 ]
ask patches with [ pcolor = white ] [
  let old-area item ID-polygon areas
  set areas replace-item ID-polygon areas (old-area + 1)
]
ask patches with [ pcolor = white ] [
  set polygon-area 0.01 * item polygon-ID areas
]