NetLogo:计算补丁集的地理中心

时间:2017-03-21 22:07:07

标签: netlogo

我在NetLogo中建模区域选择,其中海龟选择一个区域中心(" start-patch")然后根据补丁的值构建一个区域。声称新补丁后,海龟总会返回初始补丁,然后选择,移动并声明下一个最有价值的补丁。选择领土后,乌龟知道它拥有哪些补丁,补丁知道它们的所有者。

最终,领土的起始补丁实际上可能不会真正成为真正的地理中心。在乌龟选择了它的领土之后,我怎么能要求它评估领土,确定地理中心,并计算起始补丁到领土真正中心的接近程度? (注意:我不想强迫海龟在地理中心保留起始补丁 - 他们可以自由选择他们想要的任何补丁。但如果没有&#我可以强迫乌龟重新选择领土39; ta close match - 这些领域效率不高。)

以下是领土起点(黑色星星)与地理中心不相等的示例。下面是一些示例代码。有什么建议?提前致谢! Visual: example of resulting territories and how the "center" doesn't usually end up in the geographic center of a territory.

patches-own [
    benefit   ;; ranges 0.1-1 and represents food available in the patch
    owner ]   ;; patches are owned once selected for a territory

turtles-own [
    start-patch     ;; the selected center of the territory
    sum-value       ;; sum of values of patches in my territory 
    territory-list  ;; list of patches I've selected
    territory       ;; agentset of patches I've selected
    established ]   ;; true/false, true when has settled in a final territory after assessing geographic center

globals [threshold = 25]

to setup
    ask patches [ set owner nobody ]
end

to go 
    ask turtles [
        pick-center
        build-territory]
    tick
end

to pick-center
  if start-patch = 0
  [move-to best-center  ;; (calculated by a reporter elsewhere as moving windows for a cluster of high-benefit patches)
   set start-patch patch-here
   set owner self
   set territory-list (list patch-here)
   set territory (patches with [owner = myself]) 
  ]

to build-territory
     ifelse sum-value < threshold 
     [ pick-patch ]  ;; keeps picking patches for territory until I've met my threshold
     [ assess-geographic-center]  ;; once met threshold, assess real center of patch-set
end 

to pick-patch
    let _destination highest-value  ;; (this is calculated by reporters elsewhere based on benefit / travel costs to a patch)
    face _destination forward 1   
      if patch-here = _destination
        [ claim-patch _destination ]

end

to claim-patch [_patch]
     ask _patch [set owner myself]
     set sum-value sum-value + (benefit / (distance start-patch))
     set territory-list lput patch-here territory-list
     set territory (patch-set territory _patch)
     move-to start-patch
end

to assess-geographic-center
     ;; Once the territory is built, the turtle should identify the actual 
     ;; geographic center of the patch-set it has selected...how to do this?
     ;; Once it knows the center, the turtle should compare the distance to the original start-patch.
     ;; If >10 patches away, it will move to that start-patch and start over with selecting a territory....
end

2 个答案:

答案 0 :(得分:2)

你能不能只使用所有补丁的平均pxcor和pycor?类似的东西:

to setup

  ca
  reset-ticks
  let n 70

  ask one-of patches [
    set pcolor red
  ]

  while [ ( count patches with [pcolor = red ] ) < n ] [
    ask one-of patches with [ pcolor = red ] [
      ask one-of neighbors4 [set pcolor red ]

    ]
  ]

  let xmean mean [pxcor] of patches with [ pcolor = red ]
  print xmean
  let ymean mean [pycor] of patches with [ pcolor = red ]
  print ymean

  ask patch xmean ymean [ set pcolor blue
  ]

end

答案 1 :(得分:1)

按照上一个回答,这是我想出的。这是我原始代码中的最后一个步骤:

to assess-geographic-center
     let xmean mean [pxcor] of patches with [ owner = myself ]
     let ymean mean [pycor] of patches with [ owner = myself ]
     ask patch xmean ymean [ set pcolor black ]  ;; make the geographic center visible
     let geographic-center patch xmean ymean  
     let distance-away distance geographic-center  ;; the turtle is on its start-patch when assessing this distance
     ifelse distance-away <= 5   
        [ set established true ]  ;; turtle is happy if start-patch and geographic-center are approximately equal, territory "established"
        [ move-to geographic-center  ;; otherwise, turtle moves to geographic-center,
          reposition ]  ;; and follows a procedure "reposition" to makes this the new start-patch and repick the territory
end