Flocking with Predation

Flocking with Predation preview image

1 collaborator

Photo Héctor Sanchez (Author)



Tagged by Héctor Sanchez over 6 years ago


Tagged by Héctor Sanchez over 6 years ago


Tagged by Héctor Sanchez over 6 years ago


Tagged by Héctor Sanchez over 6 years ago


Tagged by Héctor Sanchez over 6 years ago


Tagged by Héctor Sanchez over 6 years ago

Visible to everyone | Changeable by everyone
Model was written in NetLogo 5.0.4 • Viewed 1244 times • Downloaded 73 times • Run 0 times
Download the 'Flocking with Predation' 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

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

Click to Run Model

globals[deadTurtles dovesSpeedVariationRange sigmoidScale]
breed[doves dove]
breed[hawks hawk]
doves-own[flockmates nearest individualSpeed age]


to setup
  set dovesSpeedVariationRange 0
  set sigmoidScale .001
  ask patches[]
  createHawks hawksPopulation
  createDoves dovesPopulation  


to go
  preyFlockOrAvoid doves hawks dovesVisionRange dovesVisionAngle dovesAvoidanceMinTurn dovesAvoidanceMaxTurn dovesNoiseInMovement dovesFlock
  predatorHunt hawks doves hawksVisionRange hawksVisionAngle hawksFocus hawksNoiseInMovement hawksKillingRange


to killIfAnyNear [breedToAttack killingRange]
  ;;If any prey is within killing range of our predator it is forced to die
  if any? breedToAttack in-radius killingRange
    ask breedToAttack in-radius killingRange[die]
    set deadTurtles deadTurtles + 1

to headProbabilisticallyTowardsMortal [potentialVictims focus]
  ;;Allows the posibility of the predator to lose focus and head towards another victim
  ifelse(random-float 1 < focus)
  [headTowardsNearestMortal potentialVictims]
  [headTowardsOneMortal potentialVictims]

to headTowardsNearestMortal [potentialVictims]
  ;;Sets heading towards the nearest prey available
  let victim min-one-of potentialVictims [distance myself]
  set heading (towards victim)

to headTowardsOneMortal [potentialVictims]
  ;;Sets heading towards one of the nearest preys available
  let victim one-of potentialVictims
  set heading (towards victim)


to runaway [avoidableBreed minTurn maxTurn]
  ;;If there is a predator we turn probabilistically within the range of (minTurn, maxTurn)
  set heading (towards min-one-of avoidableBreed [distance myself]) + (minTurn + random (maxTurn - minTurn))


to createHawks [numberOfIndividuals]
  ;;Creates hawks population
  create-hawks hawksPopulation
    set color red
    setxy random-xcor random-ycor
    if hawksPen = true[pen-down] 
    set size 3.5
    set individualSpeed hawksBaseSpeed 

to createDoves [numberOfIndividuals]
  ;;Creates doves population
  create-doves dovesPopulation 
    set color blue + random 10
    setxy random-xcor random-ycor
    if dovesPen = true[pen-down]
    set size 2
    set flockmates no-turtles
    set individualSpeed (dovesBaseSpeed + random-float dovesSpeedVariationRange)
    set age random 50 


to predatorHunt [predatorBreed preyBreed predatorVisionRange predatorVisionAngle predatorFocus predatorNoiseInMovement predatorKillingRange]
  ;;If there are preys around then hunt else wander around
  ask predatorBreed
    let preysAround (preyBreed in-cone predatorVisionRange predatorVisionAngle)
    ifelse (any? preysAround)
    [moveProbabillisticallyTowardsMortalAndKill preysAround preyBreed predatorFocus predatorNoiseInMovement predatorKillingRange]
    [wanderAround 10 predatorNoiseInMovement]
    forward individualSpeed

to preyFlockOrAvoid [preyBreed predatorBreed preyVisionRange preyVisionAngle preyMinAvoidanceTurn preyMaxAvoidanceTurn preyNoiseInMovement flockOrNotBool]
  ;;If there are predators then avoid else try to flock
  ask preyBreed
    ifelse(any? predatorBreed in-cone preyVisionRange preyVisionAngle)
    [runawayWithNoise predatorBreed preyMinAvoidanceTurn preyMaxAvoidanceTurn preyNoiseInMovement]
    [flockOrWander flockOrNotBool preyBreed preyVisionRange preyVisionAngle preyNoiseInMovement]
    forward individualSpeed

to flockOrWander [flockOrNotBool preyBreed preyVisionRange preyVisionAngle preyAngleNoiseRange]
  set flockmates other preyBreed in-cone preyVisionRange preyVisionAngle
  ifelse (flockOrNotBool and (any? flockmates))
  [flock doves dovesVisionRange dovesVisionAngle dovesBaseSpeed dovesSpeedPerFlockmate dovesNoiseInMovement]
  [wanderAround 10 preyAngleNoiseRange]

to runawayWithNoise [predatorBreed preyMinAvoidanceTurn preyMaxAvoidanceTurn preyNoiseInMovement]
  ;;Escapes from a predator with a certain amount of noise in the movement
  runaway predatorBreed preyMinAvoidanceTurn preyMaxAvoidanceTurn
  right randomAngleRange preyNoiseInMovement

to moveProbabillisticallyTowardsMortalAndKill [preysAround preyBreed predatorFocus predatorNoiseInMovement predatorKillingRange]
  ;;Heads towards one prey in a group and kills if any of them is in range
  headProbabilisticallyTowardsMortal preysAround predatorFocus
  right randomAngleRange predatorNoiseInMovement
  killIfAnyNear preyBreed predatorKillingRange  

to wanderAround [angleWanderRange angleNoiseRange]
  ;;Just take it easy and get around
  right (randomAngleRange angleWanderRange) + (randomAngleRange angleNoiseRange)

to turnWithLimit [desiredTurn maxTurn]
  ;;Performs a turn that allows a certain maximum number of degrees and no more
  ifelse (abs desiredTurn > maxTurn)
  [ifelse (desiredTurn > 0)[right maxTurn][left maxTurn]]
  [ifelse (desiredTurn > 0)[right abs desiredTurn][left abs desiredTurn]]

to-report averageFlockmateHeading
  ;;Averages the headings of the flockmates
  let x-component sum [dx] of flockmates
  let y-component sum [dy] of flockmates
  ifelse (x-component = 0 and y-component = 0)[report heading][report atan x-component y-component]

to-report averageTowardsFlockmatesHeading
  ;;Gets the average of angle between the headings of the flockmates and the current turtle
  let x-component mean [sin (towards myself + 180)] of flockmates
  let y-component mean [cos (towards myself + 180)] of flockmates
  ifelse (x-component = 0 and y-component = 0)[report heading][report atan x-component y-component]

to-report randomAngleRange[range]
  ;;Returns an angle in the range of (-range, range)
  report random range - random range


to flock [flockingBreed radius angle baseSpeed speedBonusPerFlockmate noiseInMovement]
  ;;Performs the required functions for the turtles to flock (align, cohere and separate)
  set nearest min-one-of flockmates [distance myself]
  ifelse (distance nearest < dovesDistance)[separate][align cohere]
  right randomAngleRange noiseInMovement
  ;;Turn on/off one of these behaviours
  ;if(flockSpeed = "BaseSpeed")[set individualSpeed baseSpeed + (speedBonusPerFlockmate * count flockmates)]
  ;if(flockSpeed = "MinOfFlock")[set individualSpeed ((min [individualSpeed] of flockmates) + (speedBonusPerFlockmate * count flockmates))]
  ;if(flockSpeed = "MeanOfFlock")[set individualSpeed ((mean [individualSpeed] of flockmates) + (speedBonusPerFlockmate * count flockmates))]
  ;if(flockSpeed = "MaxOfFlock")[set individualSpeed ((max [individualSpeed] of flockmates) + (speedBonusPerFlockmate * count flockmates))]
  if(flockSpeed = "BaseSpeed")[set individualSpeed baseSpeed + (speedBonusPerFlockmate * count flockmates)]
  if(flockSpeed = "MinOfFlock")[set individualSpeed ((min [individualSpeed] of flockmates) + (sigmoidFunction sigmoidScale 5 1 (count flockmates)))]
  if(flockSpeed = "MeanOfFlock")[set individualSpeed ((mean [individualSpeed] of flockmates) + (sigmoidFunction sigmoidScale 5 1 (count flockmates)))]
  if(flockSpeed = "MaxOfFlock")[set individualSpeed ((max [individualSpeed] of flockmates) + (sigmoidFunction sigmoidScale 5 1 (count flockmates)))]

to separate
  ;;Turn away from a flockmate that is too near to us
  let neighborHeading ([heading] of nearest)
  let differenceInHeadings (subtract-headings heading neighborHeading)
  turnWithLimit differenceInHeadings maxSeparateTurn

to align
  ;;Turns our turtle towards the average direction its flockmates are heading to
  let averageHeading averageFlockmateHeading
  let differenceInHeadings (subtract-headings averageHeading heading)
  turnWithLimit differenceInHeadings maxAlignTurn

to cohere
  ;;Head towards the average position of our flockmates
  let averageHeadingTowards averageTowardsFlockmatesHeading
  let differenceInHeadings (subtract-headings averageHeadingTowards heading)
  turnWithLimit differenceInHeadings maxCohereTurn

to-report sigmoidFunction [scale offset slope x]
  ;;Applies a sigmoid function to the input values
  report scale * (1 / (1 + e ^ (- offset * x + slope)))

There is only one version of this model, created over 6 years ago by Héctor Sanchez.

Attached files

File Type Description Last updated
Flocking with Predation.png preview Preview for 'Flocking with Predation' over 6 years ago, by Héctor Sanchez Download

This model does not have any ancestors.

This model does not have any descendants.