user2359494
user2359494

Reputation: 733

NetLogo releasing isolated patches from patch-set

I am growing animal territories. Animal territories may cleave parts of other animal territories during the process of expand. So instead of being one contiguous territory, the territory may include more than one cluster (i.e., unattached clusters). This is what happens in the model below. I'd like to have the territory recognize this and release whichever cluster of cells (or a single unattached cell) is smallest so that the territory remains one contiguous cluster. I'm not sure where to start with this. Any help would be great.

breed [animals animal]
breed [homeranges homerange]

animals-own 
[ 
  Name 
  orig 
  territory 
  food 
  status
]  

patches-own 
[ 
  owner 
  prey 
]  

to setup 
  clear-all
  random-seed 2234
  ask patches 
  [ 
    set owner nobody 
    set prey 2
    set pcolor scale-color (black) prey 1 4
  ] 

  let $colors [brown orange violet sky lime] 
  let $Name ["t6" "t7" "t8" "t9" "t10"]
  let $status [0 0 0 0 5]
  ask n-of 5 patches 
  [ 
    sprout-animals 1
    [ 
      set shape "circle"
      set orig patch-here  
      set territory patch-set orig 
      set status item who $status
      set size 0.3 + 0.1 * status
      set color item who $colors  
      set pcolor color 
      set Name item who $Name
      set owner self
    ] 
  ] 
  reset-ticks 
end 

to go 
  if all? animals [food >= 350] [ stop ] 
  if ticks = 70 [ stop ]
  expand
  tick 
end 

to expand ; animals procedure

  repeat 10
  [
    ask animals
    [
      let vacant no-patches
      let subord no-patches
      let target nobody
      let new-patches no-patches
      let status-of-calling-tiger status ; 
      let calling-tiger self   ; 

                               ; If territory not yet good enough:
      if food < 500 
      [ 
        ask territory
        [
          ; Add unoccupied neighbor patches as potential targets:
          set vacant (patch-set vacant neighbors with [owner = nobody])

          ; Add occupied neighbor patches as potential targets if their tiger has a lower status than me:
          set subord (patch-set subord neighbors with [owner != nobody and [status] of owner < status-of-calling-tiger])
        ]

        ask subord [ set pcolor red ] 

                                      ; Set of all potential targets:
        set new-patches (patch-set new-patches vacant subord) 

        ; Choose as target the one potential target with highest prey:
        if any? new-patches
        [
          ask new-patches
          [ifelse any? vacant
            [ifelse any? subord
              [ifelse [prey] of max-one-of vacant [prey] = [prey] of max-one-of subord [prey]
                [set target max-one-of vacant [prey]]
                [set target max-one-of new-patches [prey]]
              ]
              [set target max-one-of vacant [prey]]
            ]
            [set target max-one-of subord [prey]]
          ]
          move-to target 
          if-else member? target subord
          [ set shape "triangle" ]  ; so you can see that the target patch was from "subord"
          [ set shape "circle" ]    ; or from "vacant"
        ]

        ;ifelse any? target with [owner != nobody]
        if target != nobody
        [
          ; Add target patch to territory of the current animal:
          set territory (patch-set territory target) ; this is the territory of the calling tiger 

          let old-owner [owner] of target; this needs to be memorized

                                         ; Tell target patch that is has new owner:
          ask target [ set owner calling-tiger ]

          ; Tell the original owner of the target patch to remove the target patch from its territory:
          if old-owner != nobody ; 
          [
            ask old-owner
            [
              set territory territory with [ owner != calling-tiger ]
            ]  
          ]
        ]
        set food sum [prey] of territory
      ]
    ] 
  ] 

  ask animals
  [
    ask territory
    [
      set pcolor [color] of myself
      set plabel (word [status] of owner [status] of myself) 
    ]
    if food < 10 [die]
  ]
end 

Upvotes: 1

Views: 302

Answers (1)

Seth Tisue
Seth Tisue

Reputation: 30453

Patch Clusters Example, in the Code Examples section of NetLogo's Models Library, has code for identifying a contiguous cluster of patches. The core code is as follows:

patches-own [cluster]

to setup
  ...
  ask patches [ set cluster nobody ]
  ...
end

to grow-cluster  ;; patch procedure
  ask neighbors4 with [(cluster = nobody) and
    (pcolor = [pcolor] of myself)]
  [ set cluster [cluster] of myself
    grow-cluster ]
end

But see the rest of the example as well.

In your use case, instead of placing the "seeds" randomly for growing the clusters, you'll start growing the clusters in the patches where the animals are standing.

Upvotes: 1

Related Questions