crowd move

crowd move preview image

1 collaborator

Default-person Sohrab Nafici (Author)

Tags

(This model has yet to be categorized with any tags)
Visible to everyone | Changeable by everyone
Model was written in NetLogo 3D 5.3.1 • Viewed 324 times • Downloaded 29 times • Run 0 times
Download the 'crowd move' modelDownload this modelEmbed this model

Do you have questions or comments about this model? Ask them here! (You'll first need to log in.)


## WHAT IS IT?

This is a code example showing how to make clusters of turtles that move as a unit. In this example, whenever two turtles touch, their clusters merge.

Comments and Questions

Please start the discussion about this model! (You'll first need to log in.)

Click to Run Model

turtles-own [
  ;; The easiest way to handle the clusters is to designate one
  ;; node in the cluster as the "leader".  It doesn't really matter
  ;; which node it is; the choice is arbitrary, but it gives us a way
  ;; to identify the cluster.  Every node knows who the leader of
  ;; its cluster is.
  leader
  ;; This is used to make all of the nodes in a cluster turn by
  ;; the same number of degrees.  Each leader chooses a random
  ;; amount, then everyone else uses the amount from their leader.
  turn-amount
]

to setup
  clear-all
  set-default-shape turtles "person"
  create-turtles population
    [ set color red
      setxy random-xcor random-ycor
     facexy 0 -16
      ;; initially, every node leads its own cluster of one
      set leader self ]
  reset-ticks
end 

to go
  ;; All the leaders choose turn amounts
  ask turtles with [leader = self] [
    set turn-amount random 10 - random 10
  ]
  ;; All nodes follow their leaders
  ask turtles [
    ;;rt [turn-amount] of leader
    fd 0.1
]
  ;; Here's the tricky part. We check whether any pairs
  ;; of clusters are touching, and if they are, we merge them.
  ask turtles [
    let candidates turtles in-radius 1 with [leader != [leader] of myself]
    if any? candidates [
      create-links-with candidates [ hide-link ]
      ask candidates [ merge ]
    ]
  ]
  tick
end 

;; This is a recursive procedure -- it calls itself.
;; That way the new leader propagates through the entire
;; cluster.

to merge  ;; node procedure
  ;; First this node merges.
  set leader [leader] of myself
  set heading [heading] of leader
  set color blue
  ;; Then any neighboring nodes that haven't merged yet
  ;; merge.
  ask link-neighbors with [leader != [leader] of myself]
    [ merge ]
end 


; Public Domain:
; To the extent possible under law, Uri Wilensky has waived all
; copyright and related or neighboring rights to this model.

There is only one version of this model, created over 7 years ago by Sohrab Nafici.

Attached files

File Type Description Last updated
crowd move.png preview Preview for 'crowd move' over 7 years ago, by Sohrab Nafici Download

This model does not have any ancestors.

This model does not have any descendants.