HubFlocking

No preview image

1 collaborator

Uri_dolphin3 Uri Wilensky (Author)

Tags

(This model has yet to be categorized with any tags)
Model group CCL | Visible to everyone | Changeable by group members (CCL)
Model was written in NetLogo 4.0pre3 • Viewed 258 times • Downloaded 31 times • Run 0 times
Download the 'HubFlocking' modelDownload this modelEmbed this model

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


VERSION

$Id: HubFlocking.nlogo 37529 2008-01-03 20:38:02Z craig $

WHAT IS IT?

This model is a HubNet enabled variation of the flocking module. Each student controls a number of birds, identified by color. It works precisely as the Flocking model, but each student can control the parameters of their bird(s) flocking behavior. A "flock value" is also tracked for all birds, and for each student.

HOW TO USE IT

First, determine the number of birds you want each student to control in the model and set the BIRDS-PER-STUDENT slider to that value. The press the LOGIN button to allow students to enter the model. Press GO to have them start flying around. Birds will be created and removed as students connect and disconnect from the hub.

Each studentThe default settings for the sliders will produce reasonably good flocking behavior. However, you can play with them to get variations:

Three TURN-ANGLE sliders control the maximum angle a bird can turn as a result of each rule.

VISION is the distance that each bird can see 360 degrees around it.

SEPARATION is the distance each bird maintains from other birds.

THINGS TO NOTICE

Central to the model is the observation that flocks form without a leader.

There are no random numbers used in this model, except to position the birds initially. The fluid, lifelike behavior of the birds is produced entirely by deterministic rules.

Also, notice that each flock is dynamic. A flock, once together, is not guaranteed to keep all of its members. Why do you think this is?

After running the model for a while, all of the birds have approximately the same heading. Why?

Sometimes a bird breaks away from its flock. How does this happen? You may need to slow down the model or run it step by step in order to observe this phenomenon.

THINGS TO TRY

Play with the sliders to see if you can get tighter flocks, looser flocks, fewer flocks, more flocks, more or less splitting and joining of flocks, more or less rearranging of birds within flocks, etc.

You can turn off a rule entirely by setting that rule's angle slider to zero. Is one rule by itself enough to produce at least some flocking? What about two rules? What's missing from the resulting behavior when you leave out each rule?

Will running the model for a long time produce a static flock? Or will the birds never settle down to an unchanging formation? Remember, there are no random numbers used in this model.

EXTENDING THE MODEL

Currently the birds can "see" all around them. What happens if birds can only see in front of them?

Is there some way to get V-shaped flocks, like migrating geese?

What happens if you put walls around the edges of the screen that the birds can't fly into?

Can you get the birds to fly around obstacles in the middle of the screen?

What would happen if you gave the birds different velocities? For example, you could make birds that are not near other birds fly faster to catch up to the flock. Or, you could simulate the diminished air resistance that birds experience when flying together by making them fly faster when in a group.

Are there other interesting ways you can make the birds different from each other? There could be random variation in the population, or you could have distinct "species" of bird.

NETLOGO FEATURES

Notice the need for the SUBTRACT-HEADINGS primitive and special procedure for averaging groups of headings. Just subtracting the numbers, or averaging the numbers, doesn't give you the results you'd expect, because of the discontinuity where headings wrap back to 0 once they reach 360.

CREDITS AND REFERENCES

This model is inspired by the Boids simulation invented by Craig Reynolds. The algorithm we use here is roughly similar to the original Boids algorithm, but it is not the same. The exact details of the algorithm tend not to matter very much -- as long as you have alignment, separation, and cohesion, you will usually get flocking behavior resembling that produced by Reynolds' original model. Information on Boids is available at http://www.red3d.com/cwr/boids/.

Comments and Questions

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

Click to Run Model

globals [
  colors            ;; list that holds the colors used for students' turtles
  color-names       ;; list that holds the names of the colors used for students' turtles
  used-colors       ;; list that holds the shape-color pairs that are already being used
  max-possible-colors
]

breed [ students student ]
breed [ birds bird ]

students-own [
  user-id            ;; entered by student, unique identifier
  my-birdies              ;; birds controlled by this student
  max-separate-turn
  max-align-turn
  max-cohere-turn
  vision
  minimum-separation
  color-name
  trim
  speed
]

birds-own [
  flockmates         ;; agentset of nearby turtles
;  nearmates
  nearest-neighbor   ;; closest one of our flockmates
  my-student            ;; student controlling this bird
  max-separate-turn
  max-align-turn
  max-cohere-turn
  vision
  minimum-separation
  flockedness
]

to startup
  set colors      [ white   gray   brown   yellow   green   lime   turquoise
                    cyan   sky   blue   violet ]
  set color-names ["white" "gray" "brown" "yellow" "green" "lime" "turquoise"
                   "cyan" "sky" "blue" "violet" "orange" "magenta" "pink"]
  set max-possible-colors (length colors)
  set used-colors []
  create-robot-student
  hubnet-set-client-interface "COMPUTER" []
  hubnet-reset
end 

to login
  listen-clients
end 

to go
  if ( not any? birds ) [ stop ]
  ask birds [ flock measure-flock]
  ask students with [ user-id != "robo-student" ] [ update-interface ]
end 

to flock  ;; turtle procedure
  find-flockmates
  ;find-nearmates
  if any? flockmates
    [ find-nearest-neighbor
      ifelse distance nearest-neighbor < minimum-separation
        [ separate ]
        [ align
          cohere ] ]
  fd 1 ;;+ value-from my-student [ speed ]
  ;; right value-from my-student [ trim ]
end 

to reset
  ask birds [ die ]
  ask students with [ user-id != "robo-student" ]
    [ create-birds-for-student birds-per-student ]
end 

to find-nearmates
;  set nearmates other turtles in-radius 3
end 

to find-flockmates  ;; turtle procedure
  set flockmates other turtles in-radius vision
end 

to find-nearest-neighbor ;; turtle procedure
  set nearest-neighbor min-one-of flockmates [distance myself]
end 

;;; SEPARATE

to separate  ;; turtle procedure
  turn-away ([heading] of nearest-neighbor) max-separate-turn
end 

;;; ALIGN

to align  ;; turtle procedure
  turn-towards average-flockmate-heading max-align-turn
end 

to-report average-flockmate-heading  ;; turtle procedure
  ;; We can't just average the heading variables here.
  ;; For example, the average of 1 and 359 should be 0,
  ;; not 180.  So we have to use trigonometry.
  ;; Theoretically this could fail if both sums are 0
  ;; since atan 0 0 is undefined, but in practice that's
  ;; vanishingly unlikely.
  report atan sum [sin heading] of flockmates
              sum [cos heading] of flockmates
end 

;;; COHERE

to cohere  ;; turtle procedure
  turn-towards average-heading-towards-flockmates max-cohere-turn
end 

to-report average-flockmate-x ;; turtle-procedure
  report mean [ xcor ] of flockmates
end 

to-report average-flockmate-y ;; turtle-procedure
  report mean [ ycor ] of flockmates
end 

to cohere2 ;; turtle procedure
   turn-towards towardsxy average-flockmate-x average-flockmate-y max-cohere-turn
end 

to-report average-heading-towards-flockmates  ;; turtle procedure
  ;; "towards myself" gives us the heading from the other turtle
  ;; to me, but we want the heading from me to the other turtle,
  ;; so we add 180
  report atan mean [sin (towards myself + 180)] of flockmates
              mean [cos (towards myself + 180)] of flockmates
end 

;;; HELPER PROCEDURES

to turn-towards [new-heading max-turn]  ;; turtle procedure
  turn-at-most (subtract-headings new-heading heading) max-turn
end 

to turn-away [new-heading max-turn]  ;; turtle procedure
  turn-at-most (subtract-headings heading new-heading) max-turn
end 

;; turn right by "turn" degrees (or left if "turn" is negative),
;; but never turn more than "max-turn" degrees

to turn-at-most [turn max-turn]  ;; turtle procedure
  ifelse abs turn > max-turn
    [ ifelse turn > 0
        [ rt max-turn ]
        [ lt max-turn ] ]
    [ rt turn ]
end 

to measure-flock ; turtle procedure
  let heading-limit 10
  let real-flockmates flockmates with
    [ abs subtract-headings heading [heading] of myself < heading-limit ]
  ifelse any? real-flockmates  [
    set flockedness count real-flockmates
  ] [
    set flockedness 0
  ]
end 

to auto-flock
  ask birds
    [ setxy random 10 random 10
      turn-towards ( towardsxy-nowrap max-pxcor max-pycor ) 360      ]
end 

;;; CLIENT HANDLING

to listen-clients
  while [ hubnet-message-waiting? ]
  [
    hubnet-fetch-message
    ifelse hubnet-enter-message?
    [ show "new client connection"  ]
    [
      ifelse hubnet-exit-message?
      [ remove-student ]
      [ execute-command hubnet-message-tag ]
    ]
  ]
end 

to create-new-student
  create-students 1
    [ set user-id hubnet-message-source
      set-unique-color
      setup-student-vars
      create-birds-for-student birds-per-student
      hubnet-send user-id "Your birds are:" color-name
    ]
end 

to create-robot-student
  create-students 1
    [ set user-id "robo-student"
      set color red
      setup-student-vars
    ]
end 

to create-robot-bird
  show "Adding robot"
  ask students with [ user-id = "robo-student" ]
    [ create-birds-for-student 1 ]
end 

to setup-student-vars
  set hidden? true
  set max-separate-turn default-max-separate-turn
  set max-align-turn default-max-align-turn
  set max-cohere-turn default-max-cohere-turn
  set vision default-vision
  set minimum-separation default-minimum-separation
  set my-birdies []
end 

to set-unique-color
  let code 0

  set code random max-possible-colors
  while [member? (item code colors) used-colors and count students < max-possible-colors]
  [
    set code random max-possible-colors
  ]
  set color item code colors
  set color-name item code color-names
  set used-colors (lput color used-colors)
end 

to update-birds [ student ]
  ask birds with [ my-student = student ] [ update-variables-from-student student ]
end 

to update-variables-from-student [ student ]
      set hidden? false
      set my-student student            ;; student controlling this bird
      set max-separate-turn ([max-separate-turn] of student)
      set max-align-turn ([max-align-turn] of student)
      set max-cohere-turn ([max-cohere-turn] of student)
      set vision ([vision] of student)
      set minimum-separation ([minimum-separation] of student)
end 

to create-birds-for-student [ num ]
  hatch-birds num
    [ update-variables-from-student myself
      rt random 360
      fd random 50
  ]
end 

to turn-birds [ adj ] ;; turtle procedure
  ask birds with [ my-student = myself ]
    [ right adj ]
end 

to accelerate-birds [ adj ] ;; turtle procedure
  ask birds with [ my-student = myself ]
    [ fd adj ]
end 

to remove-student
  ask students with [ user-id = hubnet-message-source ]
  [
    ask birds with [ my-student = myself ] [ die ]
    set used-colors remove color used-colors
    die
  ]
end 

to execute-command [command]

  if command = "login"
  [ if (not any? students with [ user-id = hubnet-message-source ])
     [ create-new-student ]
  ]

  if command = "max-separate-turn"
  [
    ask students with [user-id = hubnet-message-source]
    [ set max-separate-turn hubnet-message
      update-birds self ]
    stop
  ]
  if command = "max-align-turn"
  [
    ask students with [user-id = hubnet-message-source]
    [ set max-align-turn hubnet-message
      update-birds self
    ]
    stop
  ]
  if command = "max-cohere-turn"
  [
    ask students with [user-id = hubnet-message-source]
      [ set max-cohere-turn hubnet-message
        update-birds self
      ]
    stop
  ]

  if command = "vision"
  [
    ask students with [user-id = hubnet-message-source]
    [ set vision hubnet-message
      update-birds self
    ]
    stop
  ]

  if command = "minimum-separation"
  [
    ask students with [user-id = hubnet-message-source]
    [ set minimum-separation hubnet-message
      update-birds self]
    stop
  ]

  if command = "left"
    [ ask students with [ user-id = hubnet-message-source ]
      ;;[ set trim max (list -10 (trim - 1)) ]
      [ turn-birds -5 ]
    ]

  if command = "right"
    [ ask students with [ user-id = hubnet-message-source ]
      ;;[ set trim min (list 10 (trim + 1)) ]
      [ turn-birds 5 ]
    ]

  if command = "faster"
    [ ask students with [ user-id = hubnet-message-source ]
      ;;[ set speed precision min (list 0.5 (speed + 0.1)) 1 ]
      [ accelerate-birds 1 ]
    ]

  if command = "slower"
    [ ask students with [ user-id = hubnet-message-source ]
      ;;[ set speed precision max (list -0.5 (speed - 0.1)) 1 ]
      [ accelerate-birds -.5 ]
    ]
end 

to update-interface
  if (not any? birds ) [ stop ]
  hubnet-send user-id "Your flock value:"
    precision ( mean [ flockedness ] of birds with [ my-student = myself ] ) 1
  hubnet-send user-id "trim" [ trim ] of self
  hubnet-send user-id "speed" [ speed ] of self
end 

There are 2 versions of this model.

Uploaded by When Description Download
Uri Wilensky over 14 years ago HubFlocking Download this version
Uri Wilensky over 14 years ago HubFlocking Download this version

Attached files

No files

This model does not have any ancestors.

This model does not have any descendants.