NetLogo Wolf Sheep Predation model

The educational technology and digital learning wiki
Jump to: navigation, search

1 Introduction

This short article summarized some technical and conceptual hightlights from Wilensky et al. Wolf Sheep Grass predation models created in NetLogo.[1]

NetLogo is a tool for agent-based modelling and simulation, which is part of what can be called computational thinking. Wilensky and Reisman [2] [3] make an interesting case for learning biology through constructing and testing such computational models.

The agent-based version is also discussed in Population dynamics between preys and predators by Moira Zellner and Pierre Bommel and they also present an oscillating version where the wolf/sheep proportion is lower.

2 The Netlogo wolf-sheep models

The Wolf Sheep predation model [4] distributed with NetLogo includes two variants:

  • A Wolf-sheep model where the wolves (or the sheep) die out
  • A Wolf-sheep-grass model that is oscillating stably over time

The model can be found in NetLogo desktop software like this:

  • Open: File -> Models library (CTRL-M)
  • Then: Sample Models -> Biology -> Wolf Sheep Predation

You can try running this simulation directly in your web browser. It worked well enough in Firefox/Ubuntu 10 and Firefox/Win10. Both machines did have a good graphics cards. The online version also allows editing the code, i.e. make changes to the inner workings of the model.

2.1 A simple wolf - sheep model

Example run: Toward a desaster.

Wilensky [4] defines the first variant like this: “wolves and sheep wander randomly around the landscape, while the wolves look for sheep to prey on. Each step costs the wolves energy, and they must eat sheep in order to replenish their energy - when they run out of energy they die. To allow the population to continue, each wolf or sheep has a fixed probability of reproducing at each time step. In this variation, we model the grass as "infinite" so that sheep always have enough to eat, and we don't explicitly model the eating or growing of grass. As such, sheep don't either gain or lose energy by eating or moving. This variation produces interesting population dynamics, but is ultimately unstable. This variation of the model is particularly well-suited to interacting species in a rich nutrient environment, such as two strains of bacteria in a petri dish (Gause, 1934).”

We ran the model with the default parameters. A model without grass is unstable over time. Since it includes probablistic functions (i.e. the wandering around), the same model using the same parameters can evolve bit differently over time. After a few hundred ticks the world is usually in a bad state and also will take a lot of CPU. E.g. in one run I had 180K sheep, 3600 wolves and no grass after 450 ticks. The wolves then grew > 100K and eat all the sheep. After 775 ticks, only grass was left.

Wolf Sheep Grass ABMS. [Wilensky, U. (1997). NetLogo Wolf Sheep Predation model. Early. source
Wolf Sheep Grass ABMS. [Wilensky, U. (1997). NetLogo Wolf Sheep Predation model. Middle. source
Wolf Sheep Grass ABMS. End. [Wilensky, U. (1997). NetLogo Wolf Sheep Predation model. source

2.2 The wolf - sheep - grass model

The second models is described by Wilensky [4] like this: “The behavior of the wolves is identical to the first variation, however this time the sheep must eat grass in order to maintain their energy - when they run out of energy they die. Once grass is eaten it will only regrow after a fixed amount of time. This variation is more complex than the first, but it is generally stable. It is a closer match to the classic Lotka Volterra population oscillation models. The classic LV models though assume the populations can take on real values, but in small populations these models underestimate extinctions and agent-based models such as the ones here, provide more realistic results. (See Wilensky & Rand, 2015; hapter 4). The construction of this model is described in two papers by Wilensky & Reisman (1998; 2006) [2] [3]

In the picture below you can see that this model has a "stable" oscillation over time. After 4500 ticks, both sheep and wolves are still around.

Wolf Sheep Grass ABMS. [Wilensky, U. (1997). NetLogo Wolf Sheep Predation model. source

2.3 Agent modelisation

Agents are programmed as turtles.

  • Both wolves and sheep can move around.
  • Both sheep and wolves have energy that depletes over time
  • if a wolf encounters a sheep he can eat it and augment his energy. The sheep can eat grass.

The following code found in the NetLogo distribution runs the simulation, we removed some peripheral stuff and expanded some comments. Since Mediawiki cannot display logo code, we use comment syntax from BASIC (i.e. the "'"),

globals [ max-sheep ]  ;' don't let sheep population grow too large
;' Sheep and wolves are both breeds of turtle.
breed [ sheep a-sheep ]  ;' sheep is its own plural, so we use "a-sheep" as the singular.
breed [ wolves wolf ]
turtles-own [ energy ]       ;' both wolves and sheep have energy
patches-own [ countdown ]    ;' used to compute regrowth of grass, starts at grass-regrowth-time.

;' Setup procedure. Called when the user presses the "setup" button (this is defined through "edit button" in the interface)
to setup
  clear-all

  ;' Check model-version switch
  ;' if we're not modeling grass, then the sheep don't need to eat to survive
  ;' otherwise the grass's state of growth and growing logic need to be set up
  ifelse model-version = "sheep-wolves-grass" [
    ask patches [
      set pcolor one-of [ green brown ]
      ifelse pcolor = green
        [ set countdown grass-regrowth-time ]
      [ set countdown random grass-regrowth-time ] ;' initialize grass regrowth clocks randomly for brown patches
    ]
  ]
  [
    ask patches [ set pcolor green ] ;' Model without grass: everything is green.
  ]

  create-sheep initial-number-sheep  ;' create the sheep, then initialize their variables
  [
    set shape  "sheep"
    set color white
    set size 1.5  ;' easier to see
    set label-color blue - 2
    set energy random (2 * sheep-gain-from-food) ;' initial energy level, different for each sheep
    setxy random-xcor random-ycor ;' position randomly on the map
  ]

  create-wolves initial-number-wolves  ;' create the wolves, then initialize their variables
  [
    set shape "wolf"
    set color black
    set size 2  ;' easier to see
    set energy random (2 * wolf-gain-from-food)
    setxy random-xcor random-ycor
  ]
  display-labels
  reset-ticks
end

;' main procedure that is called when user presses the "go" button.
to go
  ;' stop the simulation of no wolves or sheep
  if not any? turtles [ stop ]
  ;' stop the model if there are no wolves and the number of sheep gets very large
  if not any? wolves and count sheep > max-sheep [ user-message "The sheep have inherited the earth" stop ]
 ;' for each a-sheep turtle
  ask sheep [
    move
    if model-version = "sheep-wolves-grass" [ ;' in this version, sheep eat grass, grass grows and it costs sheep energy to move
      set energy energy - 1  ;' deduct energy for sheep only if running sheep-wolf-grass model version
      eat-grass  ;' sheep eat grass only if running sheep-wolf-grass model version
      death ;' sheep die from starvation only if running sheep-wolf-grass model version
    ]
    reproduce-sheep  ;' sheep reproduce at random rate governed by slider
  ]
 ;' for each wolf turtle
  ask wolves [
    move ;' firstly, move the wolf.
    set energy energy - 1  ;' wolves lose energy as they move
    eat-sheep ;' wolves eat a sheep on their patch
    death ;' wolves die if our of energy
    reproduce-wolves ;' wolves reproduce at random rate governed by slider
  ]
  if model-version = "sheep-wolves-grass" [ ask patches [ grow-grass ] ]
  ;' set grass count patches with [pcolor = green]
  tick
  display-labels
end

to move  ;' turtle procedure
  rt random 50 ;'right turn
  lt random 50 ;'left turn
  fd 1 ;' advance 1.
end

to eat-grass  ;' sheep procedure
  ;' sheep eat grass, turn the patch brown
  if pcolor = green [
    set pcolor brown
    set energy energy + sheep-gain-from-food  ;' sheep gain energy by eating
  ]
end

to reproduce-sheep  ;' sheep procedure
  if random-float 100 < sheep-reproduce [  ;' throw "dice" to see if you will reproduce
    set energy (energy / 2)                ;' divide energy between parent and offspring
    hatch 1 [ rt random-float 360 fd 1 ]   ;' hatch an offspring and move it forward 1 step
  ]
end

to reproduce-wolves  ;' wolf procedure
  if random-float 100 < wolf-reproduce [  ;' throw "dice" to see if you will reproduce
    set energy (energy / 2)               ;' divide energy between parent and offspring
    hatch 1 [ rt random-float 360 fd 1 ]  ;' hatch an offspring and move it forward 1 step
  ]
end

to eat-sheep  ;' wolf procedure
  let prey one-of sheep-here                    ;' grab a random sheep
  if prey != nobody  [                          ;' did we get one?  if so,
    ask prey [ die ]                            ;' kill it, and...
    set energy energy + wolf-gain-from-food     ;' get energy from eating
  ]
end

to death  ;' turtle procedure (i.e. both wolf nd sheep procedure)
  ;' when energy dips below zero, die
  if energy < 0 [ die ]
end

to grow-grass  ;' patch procedure
  ;' countdown on brown patches: if reach 0, grow some grass
  if pcolor = brown [
    ifelse countdown <= 0
      [ set pcolor green
        set countdown grass-regrowth-time ]
      [ set countdown countdown - 1 ]
  ]
end

to-report grass
  ifelse model-version = "sheep-wolves-grass" [
    report patches with [pcolor = green]
  ]
  [ report 0 ]
end

This code is copyright 1997 Uri Wilensky and licensed under the Creative Commons Attribution-NonCommercial-ShareAlike 3.0 License

2.4 Main parameters

The following list is idenfified in the info tab of the model:

model-version
default = sheep-wolves
whether we model sheep wolves and grass or just sheep and wolves
initial-number-sheep
default = 100
the initial size of sheep population
initial-number-wolves
default = 50
the initial size of wolf population
sheep-gain-from-food (not used in the sheep-wolves model version)
the amount of energy sheep get for every grass patch eaten
wolf-gain-from-food
default = 4
the amount of energy wolves get for every sheep eaten
sheep-reproduce
default = 0.1 (10%)
The probability of a sheep reproducing at each time step
wolf-reproduce
default = 5%
the probability of a wolf reproducing at each time step
grass-regrowth-time (not used in the sheep-wolves model version)
default = 11
how long it takes for grass to regrow once it is eaten
show-energy?
default = off
whether or not to show the energy of each animal as a number

3 Comment

Modeling simple predation systems probably could be used to teach a few subjects of computational thinking.

  • Programming concepts (variables, procedures, conditions). The Logo language is maybe not the best choice since there are easier to use "block languages" like Scratch or Blockly.
  • Systems modeling. ABMS is interesting way to study complex systems as result of simple interactions between agents. However, it is not easy to create a system that "works". However, that is true for all systems models.
  • Interface programming. Adding buttons and associating a button with a user interaction is fairly easy in NetLogo.

In our (no so informed) opinion, NetLogo is a good choice for teaching computational thinking if it is uses to address several issues, e.g. programmating plus modeling/simulation.

Of course, NetLogo simulations also can be used to teach systems thinking, globally or applied to a domain, but that is another issue. Many models distributed with NetLogo have been tested and are ready for use. E.g. read Wilensky, U. & Reisman, K. (2006). Thinking like a Wolf, a Sheep or a Firefly: Learning Biology through Constructing and Testing Computational Theories – an Embodied Modeling Approach. (preprint)

4 Bibliography

  1. Wilensky, U. (1997). NetLogo Wolf Sheep Predation model. http://ccl.northwestern.edu/netlogo/models/WolfSheepPredation. Center for Connected Learning and Computer-Based Modeling, Northwestern University, Evanston, IL.
  2. 2.0 2.1 Wilensky, U. & Reisman, K. (1998). Connected Science: Learning Biology through Constructing and Testing Computational Theories – an Embodied Modeling Approach. International Journal of Complex Systems, M. 234, pp. 1 - 12.
  3. 3.0 3.1 Wilensky, U. & Reisman, K. (2006). Thinking like a Wolf, a Sheep or a Firefly: Learning Biology through Constructing and Testing Computational Theories – an Embodied Modeling Approach. Cognition & Instruction, 24(2), pp. 171-209. http://ccl.northwestern.edu/papers/wolfsheep.pdf
  4. 4.0 4.1 4.2 Wilensky, U. (1997). NetLogo Wolf Sheep Predation model. http://ccl.northwestern.edu/netlogo/models/WolfSheepPredation. Center for Connected Learning and Computer-Based Modeling, Northwestern University, Evanston, IL.