User847462
User847462

Reputation: 81

How to: calculate geographic center of patch-set?

I'm modeling territory selection in NetLogo, where turtles pick a territory center ("start-patch") and then build a territory out from there based on value of patches. Turtles always return to the start-patch after claiming a new patch, then choose, move to, and claim the next most valuable patch. After choosing a territory, the turtle knows which patches it owns, and patches know their owner.

Ultimately, the start-patch of a territory may not actually end up being the true geographic center. After a turtle has selected its territory, how might I ask it to evaluate the territory, identify the geographic center, and calculate proximity of the start-patch to the true center of the territory? (Note: I don't want to force turtles to keep the start-patch in the geographic center--they are free to choose any patches they want. But I may force turtles to re-select a territory if there isn't a close match--these territories are not very efficient otherwise.)

Here's an example of how territory start-patches (black stars) do not equal geographic centers. Some example code is below. Any suggestions?

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

Upvotes: 2

Views: 318

Answers (2)

User847462
User847462

Reputation: 81

Following the previous answer, here is what I came up with. This goes in the last procedure in my original code:

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

Upvotes: 1

Luke C
Luke C

Reputation: 10291

Could you get away with just the mean pxcor and pycor of all patches? Something like:

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

Upvotes: 3

Related Questions