Fish Schooling For Predator Avoidance

No preview image

1 collaborator

Default-person Sebastian Dobon (Author)

Tags

(This model has yet to be categorized with any tags)
Visible to everyone | Changeable by everyone
Model was written in NetLogo 6.0.3 • Viewed 551 times • Downloaded 26 times • Run 0 times
Download the 'Fish Schooling For Predator Avoidance' modelDownload this modelEmbed this model

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


Info tab cannot be displayed because of an encoding error

Comments and Questions

Fish Schooling For Predator Avoidance (Question)

I am trying to download this model but the .zip file gives me an error message when trying to unzip it. Is it possible to download this or other models to run them locally on a personal laptop? Thanks

Posted over 2 years ago

Click to Run Model

breed [ prey a-prey ]
breed [ predators predator ]

prey-own [
  schoolmates  ;; agentset of nearby prey fish
  nearest-neighbor  ;; closest one of schoolmates
  speed ;; distance to travel on tick
]

predators-own [
  prey-in-vision ;; prey within vision radius
  nearest-prey ;; shortest distance prey
  prey-in-front  ;; prey within 30 degree vision cone (used to determine bursting)
  target  ;; nearest prey within a 30 degree vision cone
  speed ;; distance to travel on tick
  burst-energy  ;; energy for predators charging at burst speed, negative if recharging
  bursting?  ;; boolean of whether or not predator is bursting towards prey
]

to setup
  ca
  ask patches [ set pcolor sky + 1 ]

  create-prey prey-population [
    set color blue
    set shape "fish"
    rt random 360
    fd (random 5) + 1 ;; distribute randomly in a cluster
    facexy 0 15
    set speed 1
  ]

  create-predators predator-population [
    set color white
    set shape "fish"
    set size 2
    setxy random-xcor random-ycor ;; distribute fully randomly
    set speed 1.6
    set burst-energy predator-burst-energy
  ]

  reset-ticks
end 

to go
  ask prey [
    school
  ]
  ask predators [
    set prey-in-vision prey in-radius predator-vision

    ifelse any? prey-in-vision
    [ chase-nearest-prey ]  ;; point towards nearest prey
    [ wander ] ;; wander if no prey in sight

    adjust-predator-speed  ;; predator will speed up when making an attack
    scare-prey  ;; prey fleeing starts at predator because of control flow (scare-right, scare-left)
    eat ;; eat prey if within neighbors
  ]
  ask turtles [ fd speed ]

  tick
end 

to eat  ;; predator procedure
  let nearby (turtle-set prey-here prey-on neighbors)
  if any? nearby and burst-energy > 0 [ ;; if there is a catch, stop to eat the fish (cannot eat again until burst recharges)
    set burst-energy 0
    set color black
    ask one-of nearby [
      die
    ]
  ]
end 

to adjust-predator-speed ;; predator procedure
  set prey-in-front prey in-cone predator-vision 90 ;; find prey in front of predator

  ifelse any? prey-in-front and burst-energy > 0 ;; burst if there is a target and have energy
  [ set bursting? true ]
  [ set bursting? false ]

  ifelse bursting?
  [ if speed <= max-predator-speed
    [ set speed speed + .3
      set burst-energy burst-energy - 1 ]
    set color white
  ]
  [ ;; else
    set speed min-predator-speed
    if burst-energy = 0 [ set burst-energy -1 - burst-recharge-time ]  ;; energy set to negative recharge time
    if burst-energy < 0 [ set burst-energy burst-energy + 1 ] ;; recharge burst
    if burst-energy = -1 [ set burst-energy predator-burst-energy ] ;; reset burst energy
  ]
end 

to scare-prey ;; predator procedure
  rt 90
  scare-right  ;; prey on right side will flee right
  rt 180
  scare-left  ;; prey on left side will flee left
  rt 90
end 

to scare-right  ;; prey on right side will flee right
  ask prey in-cone prey-vision 180 [
    if any? predators in-cone prey-vision 270 [
      rt (flee-coefficient / 100) * (subtract-headings (towards myself - 90) heading)
      set speed max-prey-speed
      set color red
    ]
  ]
end 

to scare-left ;; prey on left side will flee left
  ask prey in-cone prey-vision 180 [
    if any? predators in-cone prey-vision 270 [
      rt (flee-coefficient / 100) * (subtract-headings (towards myself + 90) heading)
      set speed max-prey-speed
      set color red
    ]
  ]
end 

to chase-nearest-prey ;; predator procedure
  set nearest-prey min-one-of prey-in-vision [distance myself]
  rt (predator-turn-coefficient / 100) * (subtract-headings (towards nearest-prey) heading)
end 

to wander ;; only predators use
  rt (random 30) - 15
end 

to school  ;; prey procedure
  find-schoolmates
  if any? schoolmates
    [
      find-nearest-neighbor
      if distance nearest-neighbor = 0.000 [ die ] ;; bug patch- removes duplicates to fix crash due to same point netlogo error
      ifelse distance nearest-neighbor < minimum-separation ;; see BOIDS paper for schooling basics (separate, align, cohere)
      [ separate ]
      [ cohere
        align ]
      adjust-prey-speed  ;; adjust speed to stay in a school, can be though of as part of "cohere"
  ]
end 

to adjust-prey-speed ;; prey procedure
  ifelse max [speed] of schoolmates > speed + .1
  [ set speed speed + .1 ] ;; speed up if any schoolmate is moving faster (potentially scared by a predator)
  [ if speed > min-prey-speed
    [ set speed speed - .2
      set color blue] ] ;; else slow down (this eventually slows the school to min-speed, sort of its own emergent phenomena)
end 

to find-schoolmates  ;; prey procedure
  set schoolmates other prey in-radius prey-vision
end 

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

;;; SEPARATE

to separate  ;; turtle procedure
  ifelse member? nearest-neighbor schoolmates in-cone minimum-separation 60 ;; if nearest is in front, slow down, else turn to avoid
  [ if speed > min-prey-speed
    [ set speed speed - .1 ]
  ]
  [ rt (separate-coefficient / 100) * (subtract-headings heading (towards nearest-neighbor)) ;; turns a fraction towards the direction facing directly away from nearest neighbor
  ]
end 

;;; ALIGN

to align  ;; turtle procedure
  rt (align-coefficient / 100) * (subtract-headings average-schoolmate-heading heading) ;; asmpytotic approach to perfect alignment
end 

to-report average-schoolmate-heading  ;; prey procedure, borrowed from orignal flocking model, used for align
  let x-component sum [dx] of schoolmates
  let y-component sum [dy] of schoolmates
  ifelse x-component = 0 and y-component = 0
    [ report heading ]
    [ report atan x-component y-component ]
end 

;;; COHERE

to cohere  ;; prey procedure
  rt (cohere-coefficient / 100) * (subtract-headings average-heading-towards-schoolmates heading) ;; asmptotic approach to center of pack, will never reach because of separate
end 

to-report average-heading-towards-schoolmates  ;; prey procedure, borrowed from original flocking model, used for cohere
  let x-component mean [sin (towards myself + 180)] of schoolmates
  let y-component mean [cos (towards myself + 180)] of schoolmates
  ifelse x-component = 0 and y-component = 0
    [ report heading ]
    [ report atan x-component y-component ]
end 

There is only one version of this model, created over 6 years ago by Sebastian Dobon.

Attached files

No files

This model does not have any ancestors.

This model does not have any descendants.