User847462
User847462

Reputation: 81

NetLogo: calculate perimeter of a patch-set

I'm modeling territory selection in NetLogo, and would like my turtles to calculate the perimeter of their territory once established. I've been trying to come up with ideas for how to do this, but haven't found a good means yet. Any ideas?

enter image description here

patches-own 
[ owner ] ;; patches know who owns them

turtles-own
[ territory  ;; agentset of patches I own
  food ;; food acquired in my territory
  threshold ] ;; food required, will build territory until meet this

to go
   tick
   ask turtles [ build-territory ]
end

to build-territory
   if food > threshold [ calculate-perimeter ]  ;; stop building when enough food
   pick-patch  ;; keep picking patches until meet threshold.
end

to calculate-perimeter
   ;; what could I use to add up the perimeter of the territory?
end

Thanks in advance for any suggestions!

Upvotes: 1

Views: 279

Answers (1)

Luke C
Luke C

Reputation: 10291

A modification of my last answer to you:

to setup
  ca
  ask patches with [pxcor > 0 ] [
    set pcolor white
  ]
  crt 1 
end

to go
  ask turtles [
    let blacklist patches with [ pcolor = black ]
    let northpatches patches with [ pycor > 0 ]
    let northred ( northpatches with [ member? self blacklist = false ] )
    ask northred [ set pcolor red ]
    let border northred with [ any? neighbors4 with [ pcolor != red ] ]
    ask border [
      set pcolor blue 
    ]
    print count border
  ]
end

You can designate border/perimeter patches as any of you territory patches with neighbors that are not territory. For you it might look something like:

  ask turtles [
    print count territory with [ any? neighbors4 with [owner != myself ] 
    ]
  ]

Again, I can't test it without your setup so you would have to modify.

Edited below

To count the edges of patches that are on the border, you could have them count their neighbors4 that belong to another turtle. Then, they can add them to that turtle's perimeter length. For example:

to assess-perimeter  ;;; must be called by a turtle
  print ("Assessing perimeter")
  let current-turtle who
  let temp-per-len 0
  let border-patches patches with [ owner = current-turtle and any? neighbors4 with [ owner != current-turtle ] ]
  show (word "I have " count border-patches " border patches")
  ask border-patches [   
    ;; One way to get each border patch to check each of its neighbors 
    let nobodies 4 - count neighbors4 ;; if any patches are on the edge of the world, returns the number of those edges
    let non-territory-edges count neighbors4 with [ owner != current-turtle ]
    let border-edges nobodies + non-territory-edges
    set temp-per-len temp-per-len + border-edges
  ]
  show (word "My perimeter length: " temp-per-len )
  set perimeter-length temp-per-len
end

If that is called after all turtles have chosen their entire home range, the idea is that each turtle assesses the border of its home range. Then, it has each of those border patches count its neighbors4 that have a different owner. I used "temp-per-len" as a summing variable within the loop, which is then used to set the turtles-own "perimeter-length". Full model code, including setup and definitions, here. Note- you'll have to download or copy the code, the model is too bulky to run well in the HTML format.

Also, I didn't actually count to make sure this worked perfectly- I did a quick version and crossed my fingers, but I think the idea makes sense and hopefully gets you started.

Upvotes: 1

Related Questions