Flocking with Predation

Flocking with Predation preview image

1 collaborator

Photo Héctor Sanchez (Author)

Tags

behaviour 

Tagged by Héctor Sanchez almost 6 years ago

boid 

Tagged by Héctor Sanchez almost 6 years ago

boids 

Tagged by Héctor Sanchez almost 6 years ago

emergent 

Tagged by Héctor Sanchez almost 6 years ago

flocking 

Tagged by Héctor Sanchez almost 6 years ago

predation 

Tagged by Héctor Sanchez almost 6 years ago

Visible to everyone | Changeable by everyone
Model was written in NetLogo 5.0.4 • Viewed 1102 times • Downloaded 66 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]
patches-own[]
breed[doves dove]
breed[hawks hawk]
doves-own[flockmates nearest individualSpeed age]
hawks-own[individualSpeed]

;;SetupFunctions---------------------------------------------------------------------------------------------------

to setup
  clear-all
  set dovesSpeedVariationRange 0
  set sigmoidScale .001
  ask patches[]
  createHawks hawksPopulation
  createDoves dovesPopulation  
  reset-ticks
end 

;;Go!!!!!!!!!!!!---------------------------------------------------------------------------------------------------

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

;AttackingFunctions---------------------------------------------------------------------------------------------------

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
  ]  
end 

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]
end 

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

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

;AvoidingFunctions---------------------------------------------------------------------------------------------------

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))
end 

;;CreationFunctions---------------------------------------------------------------------------------------------------

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 
  ]
end 

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 
  ]
end 

;;MovementFunctions---------------------------------------------------------------------------------------------------

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
  ]
end 

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
  ]
end 

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]
end 

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
end 

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  
end 

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

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]]
end  

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]
end 

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]
end 

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

;;FlockingFunctions---------------------------------------------------------------------------------------------------

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)))]
end 

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
end 

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
end 

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

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

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

Attached files

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

This model does not have any ancestors.

This model does not have any descendants.