Chapter 6: Predator-Prey

In this set of exercises, you'll make another biologically-relevant simulation which may provide you with some ideas for your project.

You will also learn now to add custom traits to your agents - another item that will be extremely useful for your project..

Learning Objectives:

  • Learn how to add custom traits to your agents
  • Learn how to use more complex "if..." statements (aka "Conditionals")
  • explore how populations of organisms grow and die
  • Use data boxes to show changing data

New terms:

  • custom traits
  • remix
  • databox
  • Exponential growth

New blocks:

  • databox

Part 1: The Prey Population

Here, you'll build a prey agent that reproduces on its own.

1) Build the prey code.

Create a new project with a suitable name and set it up so that it:
  • Has a new breed called "prey".
  • When you click "setup", 100 prey are created, assigned a suitable shape and color, and scattered in spaceland.
  • When you toggle "forever", the prey 'meander' around SpaceLand
  • Also, when "forever" is toggled, there is a 1% chance that each prey gives birth to a new prey agent with the same shape and color as all prey agents

You can download a blank NetLogo template file here.

Note that we use the 'hatch' command here instead of 'create-preys'. If an agent is asked to hatch, a copy of that agent with all of its traits copied from the parent is created. You can customize the hatched agent's traits like shown below.

hatch 1 [
  set color pink
  ;; set other traits here
]

In this case, there is no need to set new traits, so the code below will work fine.

breed [preys prey]

to setup
  clear-all
  create-preys 100 [
    set color blue
    set shape "dot"
    set xcor (random 32) - 16
    set ycor (random 32) - 16
  ]
end

to forever
  ask preys [
    forward 1
    left random 11
    right random 11
    if (random 100) < 1 [
      hatch 1  ;; automatically blue dots
    ]
  ]
  
end


2) Create a databox to show the number of prey present.

Steps:
  1. Edit the interface to change the name of the databox called "Data" to "Number of Prey"
  2. Go to the "World" page and drag in a "while forever toggled..." block.
  3. Add a "set ... databox to.." block from the "interface" drawer to the "forever" block
  4. Using a "count .... within... " block from the "Detection" drawer, set the databox to show the number of prey present within a radius of 200.
  5. Copy the code you used in (4) to have the "Number of Prey" databox show the count of prey when you click "setup".

Rearrange the widgets on the screen to make some space.

Choose "Monitor" from the "Button" menu and configure it to count the preys. It's that simple!

3) Create a line graph to plot population changes over time.

Steps:
  1. Edit the interface and add a line graph as you did for Epidemic.
  2. Set up the line graph to plot "prey population".
  3. In the "World" tab, add code to the "when setup pushed" block to clear the line graph and reset the clock.
  4. In the "World" tab, add code to the "while forever toggled" block to add counts of prey agents every time step.

Add a "Plot" as you did for Epidemic and set it to plot the count of preys.

Add code to setup and forever as you did in Epidemic.

breed [preys prey]

to setup
  clear-all
  create-preys 100 [
    set color blue
    set shape "dot"
    set xcor (random 32) - 16
    set ycor (random 32) - 16
  ]
  reset-ticks
end

to forever
  ask preys [
    forward 1
    left random 11
    right random 11
    if (random 100) < 1 [
      hatch 1  ;; automatically blue dots
    ]
  ]
  tick
end


4) Check your code.

You should make sure that you see all of these things before moving on:

  • The prey agents should 'meander' around SpaceLand - move in not-straight lines.
  • Over time, the population should grow since individuals can only get born - we haven't added any way for them to die yet.
  • The number in the databox should increase as time passes.
  • The graph should show an exponential increase. Watch for about 500 time steps and you should see:
    • The line should get steeper and steeper. That is, the growth rate should keep increasing as there are more prey organisms present, so there are more organisms giving birth.
    • Exponential growth is more than just "the growth rate gets higher and higher". Specifically, it has a constant doubling time. You can check this by looking at the data either by clicking the data table (left) button or the download data (right) button . You should see that the time it takes to go from 100 to 200 agents is about the same as the time to go from 200 to 400, from 400 to 800, etc.

You can choose "Export Plot" from the "File" menu to save the data in a form that you can open in excel.

Part 2: The Predator Population

Build a predator agent that gets energy from eating prey and spends that energy moving and giving birth.

1) Create the Predator breed and give it a custom trait called "energy".

Steps:
  1. Click the "+ Add Breed" button to creat a new breed called "Predator"
  2. In the "Predator" tab, click the "Show Traits" button and edit the window that pops up to add a custom trait called "energy". Leave the Initial Value blank - we'll set that later.
  3. Click the "hide traits" button to close the window.

Add code to the top of the code so it looks like this:

breed [preys prey]
breed [predators predator]
predators-own [energy]


		

"predators-own [energy]" means that energy is a trait that only applies to predators.

2) Have the predators move, expend energy while they move and die if their energy gets to zero.

Steps:
  1. In the "World" tab, add code to the setup block to create 10 predators, give them nice colors and shapes, and scatter them. Set their energy to 100.
  2. In the "predator" tab, add a "while forever toggled" block, and add code to that to have the predators 'meander'
  3. Add code to have each meandering step reduce the predator's energy by one unit
  4. Add code to have the predator get deleted if its energy gets to zero or lower. Note that, since it is possible for energy to sometimes skip over 0, it is better to test for 'less than or equal to zero' rather than 'equal to' zero.

The complete code is:

breed [preys prey]
breed [predators predator]
predators-own [energy]

to setup
  clear-all
  
  create-preys 100 [
    set color blue
    set shape "dot"
    set xcor (random 32) - 16
    set ycor (random 32) - 16
  ]
  
  create-predators 10 [
    set color red
    set shape "triangle"
    set energy 100
    set xcor (random 32) - 16
    set ycor (random 32) - 16
  ]
  
  reset-ticks
end

to forever
  ask preys [
    forward 1
    left random 11
    right random 11
    if (random 100) < 1 [
      hatch 1  ;; automatically blue dots
    ]
  ]
  
  ask predators [
    forward 1
    left random 11
    right random 11
    set energy (energy - 1)
    if energy <= 0 [ die ]
  ]
  
  tick
end


3) Add a databox and graph of predator population.

Using the same techniques and code as you did above, add a databox that is updated with the count of predators and add a line to the graph that shows how this number changes over time.
4) Check your code so far.

If you click "setup" and then "forever", you should see:
  • Predators and prey menadering.
  • Prey reproducing exponentially.
  • At time = 100, all the predators should die. Why? They're all born with an energy of 100 and, every time step, their energy drops by 1. So, after 100 time steps, their energy should be 0 and they should die.
5) Have the predators eat the prey and get energy when they do.

Write code in the "predator" pane so that, when a predator collides with a prey, the predator's energy increases by 20 and the prey (the "collidee") is killed.

Note the code added to the "to forever" block below:

to forever
  ask preys [
    forward 1
    left random 11
    right random 11
    if (random 100) < 1 [
      hatch 1  ;; automatically blue dots
    ]
  ]

  ask predators [
    forward 1
    left random 11
    right random 11
    set energy (energy - 1)
    if energy <= 0 [ die ]
    
    ;; new code below
    if any? preys in-radius 1 [
      set energy (energy + 20)
      ask one-of preys in-radius 1 [ ;; collidee
        die 
      ]
    ]
  ]

  tick
end
		

"one of preys in-radius 1" is the NetLogo way of saying "collidee".

6) Check your code.

Be sure that your code does all of these things:
  • Pick an individual predator and watch it for a while. When it runs into a prey, the prey should disappear.
  • Since they're getting energy from eating prey, the predators should not all die at time 100. They should last a lot longer.
  • Since they're getting eaten, there shouldn't be as many prey at time 100.
  • This only works in NetLogo: pause the simulation by clicking "forever", then control-click or right-click any of the predators and inspect it. Then, click "forever" and watch as its energy drops as time passes but then see that it goes up by 20 if it eats a prey and, if it gets to 0, the predator dies.
7) Have predators give birth if they have enough energy.

Steps:
  1. In the "forever" code for the predators, add code that, when their energy is over 300, they give birth to one predator agent.
  2. Set that predator agent's color and shape the same as all the other predators.
  3. Give the newborn predator agent the same energy as all newborn predators = 100.
  4. Reduce the parent predator's energy by 150 units (to account for the energy in the newborn as well as the 'costs' of gestating and giving birth.

Be careful about where you place the various blocks to be sure that the code does what you want.

Here's just the forever code for this:

to forever
  ask preys [
    forward 1
    left random 11
    right random 11
    if (random 100) < 1 [
      hatch 1  ;; automatically blue dots
    ]
  ]

  ask predators [
    forward 1
    left random 11
    right random 11
    
    set energy (energy - 1)
    if energy <= 0 [ die ]

    if any? preys in-radius 1 [
      set energy (energy + 20)
      ask one-of preys in-radius 1 [ ;; collidee
        die
      ]
    ]
    
    if energy > 300 [
      hatch 1 [
        ;; no need to set color & shape
        ;; since they default to parent's values
        set energy 100 ;; needed or will inherit
                       ;; parent's energy level
      ]
      set energy (energy - 150)
    ]
  ]

  tick
end
		
8) Put in some temporary debugging code, part 1.

At this point, you need to test your code carefully.

Ideally, you'd like to see newborn predators appearing but that can be hard to catch. You can see that the number of predators is increasing but it's possible that, even though the number is rising, it's not rising for the right reason.

This is a good opportunity to use the "remix" button which works like the "Save As..." option in NetLogo and other programs. Remix this project with a name like "Predator Prey Test 1".

Here, you can get the computer to do some work for you. Set the code so that newborn predators are purple - or some other stand-out color - and make them really big so you can see if newborn predators appear.

Try it and be sure you see giant pink bears (or, in NetLogo, giant green triangles) = newborn predators. Once you're sure, go back to the original (pre-remix) version, put in any chages needed and leave the "Predator Prey test 1" for the history books.

Here's just the forever code for this:

to forever
  ask preys [
    forward 1
    left random 11
    right random 11
    if (random 100) < 1 [
      hatch 1  ;; automatically blue dots
    ]
  ]

  ask predators [
    forward 1
    left random 11
    right random 11
    
    set energy (energy - 1)
    if energy <= 0 [ die ]

    if any? preys in-radius 1 [
      set energy (energy + 20)
      ask one-of preys in-radius 1 [ ;; collidee
        die
      ]
    ]
    
    if energy > 300 [
      hatch 1 [
        set color green
        set size 10
        set energy 100 ;; needed or will inherit
                       ;; parent's energy level
      ]
      set energy (energy - 150)
    ]
  ]

  tick
end
		
9) Put in some temporary debugging code, part 2.

Ideally, you should be able to see the predators giving birth when they get enough energy but it's hard to catch that as it happens. In NetLogo, you can follow an individual predator and watch its energy go up and, if you're lucky, you may see it.

A much safer way is to have the computer do some work for you. You'd like a little warning when a predator is about to give birth. Set the size of the predators to change when their energy gets close to 300 and then go back when their energy is lower. You could do this with a remix as you did above if you like. 

You should see them change size just before the predator population starts to increase.

Once you're sure you see this, you can take out the changes and move on.

Here's just the forever code for this:

to forever
  ask preys [
    forward 1
    left random 11
    right random 11
    if (random 100) < 1 [
      hatch 1  ;; automatically blue dots
    ]
  ]

  ask predators [
    forward 1
    left random 11
    right random 11
    
    set energy (energy - 1)
    if energy <= 0 [ die ]

    if any? preys in-radius 1 [
      set energy (energy + 20)
      ask one-of preys in-radius 1 [ ;; collidee
        die
      ]
    ]
    
    if energy > 300 [
      hatch 1 [
        set energy 100 ;; needed or will inherit
                       ;; parent's energy level
      ]
      set energy (energy - 150)
    ]
    
    ifelse energy > 250
      [set size 5]
      [set size 1]
  ]

  tick
end
		
10) See how it works.

You should see something like this (it will vary from run to run because of the randomness of who eats who and when):
  • At first, the prey population should rise and the predators should drop slightly.
  • Eventually, as the prey population rises a lot and the predators get well-fed, the predator population should rise.
  • As the increased number predators eat up a lot of prey, the prey population should fall.
  • At this point, one of two things can happen:
    • The prey population goes all the way to zero and then, soon after, the predators all starve and their population goes to zero. At this point, both populations have gone extinct.
    • The prey population goes way down, but not quite to zero. The predators start to die of starvation and the reduced predation pressure allows the prey to come back up and the cycle repeats.

You can download this complete project here.

11) Play with the numbers and see what happens.

There are many "magic numbers" (numbers that are set to arbitrary values) in this simulation. Try changing some of these to see if you can keep the populations from "crashing":
  • The starting number of prey.
  • The starting number of predators.
  • The chance of an individual prey to reproduce.
  • The initial energy level of predators (don't forget to change it both in setup and when they're born).
  • The energy a predator gets from eating one prey.
  • The energy level required before a predator gives birth.
  • The energy cost of giving birth.
  • In NetLogo, you can also make the world bigger by right-clicking or control-clicking the world to edit it as shown at right: