ThomasC
ThomasC

Reputation: 881

How to identify a crowd of turtles in NetLogo?

Has anybody got any solutions to having a turtle identify a crowd of other turtles i.e. a group of turtles clustered together? The following won't work because the 50 turtles in-radius could be disparate (all over the place and not bedside each other):

if count turtles in-radius 20 >= 50 [show "There's a crowd"]

At the moment I'm defining a crowd as 50+ turtles standing beside each other.

Upvotes: 0

Views: 92

Answers (1)

Luke C
Luke C

Reputation: 10291

If you want contiguous connected turtles, you could modify the Patch Clusters Example from the Model Library- here's one version. With this setup:

globals [ groups>50 ]

turtles-own [ 
  my-group
]

to setup
  ca
  crt 500 [
    set my-group -99
    set shape "square"
    move-to one-of patches
  ]  
  reset-ticks
end

And these helper functions:

to identify-groups
  let group-counter 0

  loop [
    let seed one-of turtles with [ my-group = -99 ]

    if seed = nobody [
      stop
    ]
    ask seed [
      set my-group group-counter
      set group-counter group-counter + 1
      spread-group
    ]
  ]
end  

to spread-group
  set label my-group
  set color my-group * 10 + 5 
  ask ( turtles-on neighbors ) with [ my-group = -99 ] [
    set my-group [my-group] of myself
    spread-group
  ]
end

identify-groups runs a loop that identifies all turtles on contiguous patches and spreads a unique my-group value among those groups.

You can then get the unique group values and use filter to only return group numbers for which there is some threshold turtle count:

to go 
  identify-groups

  let unique-groups sort remove-duplicates [my-group] of turtles

  set groups>50 filter [ i -> count turtles with [ my-group = i ] >= 50  ] unique-groups

  print groups>50
end

Upvotes: 1

Related Questions