Flocking Benchmark
No preview image
Model was written in NetLogo 4.1pre9
•
Viewed 429 times
•
Downloaded 55 times
•
Run 1 time
Do you have questions or comments about this model? Ask them here! (You'll first need to log in.)
Comments and Questions
Please start the discussion about this model!
(You'll first need to log in.)
Click to Run Model
globals [ result ] turtles-own [ flockmates ;; agentset of nearby turtles nearest-neighbor ;; closest one of our flockmates ] to benchmark random-seed 5454 reset-timer setup repeat 700 [ go ] set result timer end to setup ca cro population [ set color yellow - 2 + random 7 ;; random shades look nice setxy (random-float world-width) (random-float world-height) rt random-float 360 ] end to go ask turtles [ flock ] tick end to flock ;; turtle procedure find-flockmates if any? flockmates [ find-nearest-neighbor ifelse distance nearest-neighbor < minimum-separation [ separate ] [ align cohere ] ] fd 1 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. report atan mean [sin heading] of flockmates mean [cos heading] of flockmates end ;;; COHERE to cohere ;; turtle procedure turn-towards average-heading-towards-flockmates 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 (my-subtract-headings new-heading heading) max-turn end to turn-away [new-heading max-turn] ;; turtle procedure turn-at-most (my-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 find the difference between two headings, we can't just ;; subtract the numbers, because 0 and 360 are the same heading. ;; For example, the difference between a heading of 5 degrees ;; and a heading of 355 degrees is 10 degrees, not 350 degrees. to-report my-subtract-headings [h1 h2] ifelse abs (h1 - h2) <= 180 [ report h1 - h2 ] [ ifelse h1 > h2 [ report h1 - h2 - 360 ] [ report h1 - h2 + 360 ] ] end
There are 3 versions of this model.
Attached files
No files
This model does not have any ancestors.
This model does not have any descendants.