<img src="https://d5nxst8fruw4z.cloudfront.net/atrk.gif?account=iA1Pi1a8Dy00ym" style="display:none" height="1" width="1" alt="" />
Skip Navigation
Say Thanks header
You are reading an older version of this FlexBook® textbook: CK-12 Modeling and Simulation for High School Teachers: Principles, Problems, and Lesson Plans Go to the latest version.

7.5: Appendix: Wolf Sheep Predation Example

Difficulty Level: At Grade Created by: CK-12

The following is a copy of the Wolf Sheep Predation example that is part of the NETLOGO model library. Make sure that you have the right version of this example that comes with your version of NETLOGO.

globals [grass]  ;; keep track of how much grass there is
;; 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]
to setup
  ask patches [ set pcolor green ]
  ;; check GRASS? switch.
  ;; if it is true, then grass grows and the sheep eat it
  ;; if it false, then the sheep don't need to eat
  if grass? [
    ask patches [
      set countdown random grass-regrowth-time ;; initialize grass grow clocks randomly
      set pcolor one-of [green brown]
  set-default-shape sheep "sheep"
  create-sheep initial-number-sheep  ;; create the sheep, then initialize their variables
    set color white
    set size 1.5  ;; easier to see
    set label-color blue - 2
    set energy random (2 * sheep-gain-from-food)
    setxy random-xcor random-ycor
  set-default-shape wolves "wolf"
  create-wolves initial-number-wolves  ;; create the wolves, then initialize their variables
    set color black
    set size 1.5  ;; easier to see
    set energy random (2 * wolf-gain-from-food)
    setxy random-xcor random-ycor
to go
  if not any? turtles [ stop ]
  ask sheep [
    if grass? [
      set energy energy - 1  ;; deduct energy for sheep only if grass? switch is on
  ask wolves [
    set energy energy - 1  ;; wolves lose energy as they move
  if grass? [ ask patches [ grow-grass ] ]
to move  ;; turtle procedure
  rt random 50
  lt random 50
  fd 1
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
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
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
to catch-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
      set energy energy + wolf-gain-from-food ] ;; get energy from eating
to death  ;; turtle procedure
  ;; when energy dips below zero, die
  if energy < 0 [ die ]
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 ]
to update-plot
  set grass count patches with [pcolor = green]
  set-current-plot "populations"
  set-current-plot-pen "sheep"
  plot count sheep
  set-current-plot-pen "wolves"
  plot count wolves
  if grass? [
    set-current-plot-pen "grass / 4"
    plot grass / 4  ;; divide by four to keep it within similar
                    ;; range as wolf and sheep populations
to display-labels
  ask turtles [ set label "" ]
  if show-energy? [
    ask wolves [ set label round energy ]
    if grass? [ ask sheep [ set label round energy ] ]
; Copyright 1997 Uri Wilensky. All rights reserved.
; The full copyright notice is in the Information tab.

Image Attributions




Date Created:

Jul 27, 2012

Last Modified:

Apr 29, 2014
You can only attach files to section which belong to you
If you would like to associate files with this section, please make a copy first.


Please wait...
Please wait...
Image Detail
Sizes: Medium | Original

Original text