Predator Prey Game

Predator Prey Game preview image

1 collaborator

Uri_dolphin3 Uri Wilensky (Author)

Tags

(This model has yet to be categorized with any tags)
Model group CCL | Visible to everyone | Changeable by group members (CCL)
Model was written in NetLogo 5.0beta1 • Viewed 3412 times • Downloaded 207 times • Run 0 times
Download the 'Predator Prey Game' modelDownload this modelEmbed this model

Do you have questions or comments about this model? Ask them here! (You'll first need to log in.)


WHAT IS IT?

This model simulates a predator-prey relationship. The population consists of wolf packs (predators) and sheep herds (prey), some controlled by students via HubNet clients and some androids controlled by the computer. The wolves gain energy from consuming sheep, and the sheep gain energy from consuming grass (a primary producer). The model allows students to examine simple population dynamics like those modeled through the Lotka-Volterra equations in a participatory way.

HOW IT WORKS

Predators and prey move around the world, searching for food. Prey eat plants (green areas), while predators eat prey. All animals gain energy by finding food and expend energy in the search for food. Prey also lose energy when predators catch them. Each predator agent can be thought of as a pack of wolves and each prey can be thought of a herd of sheep. Prey can be caught by predators a few at a time before they ultimately die out. In other words, a prey agent does not die the very first time they run into a predator. The grass is also a limited resource; it regrows at a fixed rate. Android predators and prey can wander around the world at random, while student predators and prey are directed through the HubNet clients.

If an animal's energy gets too low it dies. If a student controls the animal, that student is given the option to "recreate" as a predator or prey. Until the student "recreates" they cannot interact with the rest of the system, i.e. they cannot be eaten, they cannot eat, and they cannot move. By recreating the students have some control over the populations in the world and can dramatically shift the population dynamics. If the energy of an android goes below zero the android merely dies. However, androids do have the ability to reproduce, which student agents do not. Every tick each prey has a PREY-REPRODUCE chance of creating a new prey, and each predator has a PREDATOR-REPRODUCE chance of creating a new predator.

HOW TO USE IT

To start the game first press the SETUP button then press the GO button and ask the students to log in. You may also want to create android predators or prey using the ADD-PREDATORS and ADD-PREY buttons. You can control the movement of the androids using the WANDER? switch (which determines whether they move at all) and the ANDROID-DELAY slider (which determines how many seconds elapse between android movements).

The total number of PREDATORS and PREY, including both students and androids are displayed in the corresponding monitors. You can control the amount of energy each animal gains from consuming food by using the PREDATOR-GAIN-FROM-FOOD and the PREY-GAIN-FROM-FOOD sliders. PLANT-REGROWTH-RATE brown areas regrow grass at every tick in the model. Thus modifying this slider controls the amount of grass that is available for the sheep to consume.

If you wish to change all the students to either predators or prey press the MAKE-STUDENTS-PREDATORS or MAKE-STUDENTS-PREY buttons. These buttons do not affect androids.

To start the game over press the SETUP button, this will return all students to initial energy levels and place them at a new random position. It will kill all the androids and clear the plots.

AVERAGE ENERGY plots the average energy of all predators in red, and all prey in green, this includes androids. POPULATION plots the total of each type of animal.

Client Information

After logging in, the client interface will appear for the students, and if GO is pressed in NetLogo they will be assigned a role as predator or prey at random. Their character will consist of an animal indicator, a wolf if they are a predator or a sheep if they are prey, and an icon. The icon is their unique identifier so it's easy to distinguish themselves from other animals. A description of the icon will appear in the YOU ARE A: monitor. If the student does not like the shape and/or color of their icon they can hit the CHANGE APPEARANCE button at any time to change to another random icon.

The student controls the movement of their turtle with the UP, DOWN, LEFT, and RIGHT buttons (they can also use the hot-keys as short cuts) and the STEP-SIZE slider. Clicking any of the directional buttons (or using the hot-keys) will cause their turtle to move in the respective direction a distance of STEP-SIZE.

ENERGY for the appropriate animal is displayed in the monitor of same name. If the energy is zero or less, then the agent dies. The ROLE monitor will display "dead", the animal indicator shape will appear as a sad face, and the STATUS monitor will indicate that the student should recreate as a predator or prey. After s/he selects one s/he cannot switch roles until the animal dies again.

THINGS TO NOTICE

Predators may find it better to stay together. Prey, however, may learn to stay more scattered, so as to not become easy prey to a large group of predators.

At first students may find it advantageous to be predators but if many students become predators, the balance will shift and it may be more advantageous to "recreate" as a prey.

THINGS TO TRY

Try changing the amount of energy gained from food for both predators and prey, at what point does it cease to be advantageous to be a predator?

Try to balance out the system so that no predators or prey go hungry.

Change the PLANT-REGROWTH-RATE slider. How does this affect the balance between the predators and prey?

EXTENDING THE MODEL

Try making the androids move toward food rather than moving randomly.

There is no cost for changing the step-size for a student agent. Is there a way a larger step-size could "cost" the agent something?

NETLOGO FEATURES

Because so much information must be conveyed through the appearance of each turtle, each player is actually made up of two turtles; one turtle's shape is either a wolf or a sheep to indicate whether it is a predator or prey. Behind the first turtle is an icon turtle that helps students distinguish themselves from other players (and androids). Thus though each student is viewed as one agent, they are actually made up of two turtles.

RELATED MODELS

The various Wolf-Sheep models are non-HubNet versions of a similar model. Also see the Bug Hunter model and the Guppy model for other HubNet models that explore population dynamics.

HOW TO CITE

If you mention this model in a publication, we ask that you include these citations for the model itself and for the NetLogo software:

COPYRIGHT AND LICENSE

Copyright 2002 Uri Wilensky.

CC BY-NC-SA 3.0

This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike 3.0 License. To view a copy of this license, visit http://creativecommons.org/licenses/by-nc-sa/3.0/ or send a letter to Creative Commons, 559 Nathan Abbott Way, Stanford, California 94305, USA.

Commercial licenses are also available. To inquire about commercial licenses, please contact Uri Wilensky at uri@northwestern.edu.

This activity and associated models and materials were created as part of the projects: PARTICIPATORY SIMULATIONS: NETWORK-BASED DESIGN FOR SYSTEMS LEARNING IN CLASSROOMS and/or INTEGRATED SIMULATION AND MODELING ENVIRONMENT. The project gratefully acknowledges the support of the National Science Foundation (REPP & ROLE programs) -- grant numbers REC #9814682 and REC-0126227.

Comments and Questions

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

Click to Run Model

globals
[
  start-energy      ;; constant used to initialize turtles

  colors            ;; list that holds the colors used for students' turtles
  color-names       ;; list that holds the names of the colors used for students' turtles
  all-shapes        ;; list that holds all the possible shapes used in the game
]

breed [ icons icon ]
breed [ androids android ]
breed [ students student ]

androids-own
[
  predator?
  energy
]

students-own
[
  user-id
  my-icon
  predator?
  energy
  step-size
]

icons-own
[
  color-name
]

;;
;; Setup Procedures
;;

to startup
  ;; standard hubnet setup code
  hubnet-reset

  ;; setup the model itself
  setup-vars
  setup
end 

;; initialize the global variables used throughout
;; this only needs to be called once during startup

to setup-vars
  set start-energy 10
  set colors (list white orange red yellow
                    (violet + 1) (sky + 1) pink )
  set color-names ["white" "orange" "red" "yellow"
                   "purple" "blue" "pink" ]
  set all-shapes [ "diamond" "heart" "pentagon" "square" "circle" "triangle" "rhombus" ]
end 

;; return all students to initial state and randomize locations
;; kill all androids

to setup
  reset-ticks
  clear-all-plots
  setup-patches

  ;; reset the students to a new location with new energy
  ask students
  [
    move-to one-of patches
    face one-of neighbors4
    set energy start-energy
    ifelse predator?
    [ set shape "wolf" ]
    [ set shape "sheep" ]
    ask my-icon [ move-to myself ]
    send-info-to-client
  ]

  ;; kill all the androids
  ask androids
    [ die ]
end 

;; make grass on half the patches.

to setup-patches
  ask patches
  [
    ifelse random 2 = 0
      [ set pcolor green - 3 ]
      [ set pcolor brown - 2 ]
  ]
end 

;; create a new android prey

to add-prey
  create-androids 1
  [
    move-to one-of patches
    set color gray
    face one-of neighbors4
    set predator? false
    set shape "sheep"
    set energy start-energy
  ]
end 

;; create a new android predator

to add-predators
  create-androids 1
  [
    move-to one-of patches
    face one-of neighbors4
    set color black
    set predator? true
    set energy start-energy
    set shape "wolf"
  ]
end 

;;
;; Runtime Procedures
;;

;; the main procedure

to go
  ;; listen to the hubnet client
  every 0.1
  [
    listen-clients
    display
  ]

  ;; if wander? is true then the androids wander around the landscape
  if wander?
    [ androids-wander ]

  ;; the delay below keep plants from growing too fast
  ;; and predator/prey from losing points too fast
  every 3
  [
    if any? turtles
    [
      plants-regrow
      ask students
      [
        set energy energy - 0.5
        if energy <= 0
        [ student-die ]
        update-energy-monitor
      ]
      do-plot
    ]
    tick
  ]
end 

;; android animals should move at random

to androids-wander

  ;; only execute at android-delay rate to keep the androids from moving too fast
  every android-delay
  [
    ask androids
    [
      face one-of neighbors4
      fd 1
      ifelse not predator?
      [
        eat-grass
        reproduce-android-prey
      ]
      [
        eat-prey
        reproduce-android-predator
      ]
      set energy energy - 0.5
      if energy <= 0
        [ die ]
    ]
  ]
end 

;; turtle procedure
;; creates a new android near the old one

to reproduce-android-prey
  if random-float 100 < prey-reproduce [
    set energy (energy / 2)
    hatch 1 [ rt random-float 360 fd 1 ]
  ]
end 

;; turtle procedure
;; creates a new android near the old one

to reproduce-android-predator
  if random-float 100 < predator-reproduce [
    set energy (energy / 2 )
    hatch 1 [ rt random-float 360 fd 1 ]
  ]
end 

;; regrow the grass

to plants-regrow
  let brown-patches patches with [ pcolor = brown - 2 ]

  ;; only regrow until the entire world is full
  ifelse count brown-patches > plant-regrowth-rate
  [
    ask n-of plant-regrowth-rate brown-patches
      [ set pcolor green - 3 ]
  ]
  [
    ask brown-patches [ set pcolor green - 3 ]
  ]
end 

;; turtle procedure
;; if you are a predator consume a prey

to eat-prey
  let eaten-prey one-of other turtles-here with [ breed != icons and not predator? and energy > 0 ]
  if eaten-prey != nobody
  [
    set energy energy + predator-gain-from-food
    ;; prey lose energy equal to one "life" when they are eaten
    ;; which is the amount of energy that everyone starts with.
    ask eaten-prey
      [ set energy max list 0 energy - start-energy ]
  ]
end 

;; turtle procedure
;; if you are are a prey eat some grass

to eat-grass
  ;; make the colors a little darker to contrast
  ;; with the icon colors.
  if pcolor = green - 3
  [
    set energy energy + prey-gain-from-food
    set pcolor brown - 2
  ]
end 

;; force all students to become prey

to make-students-prey
  ask students [
    set predator? false
    set shape "sheep"
    set color grey
    send-info-to-client
    hubnet-send user-id "status" "You are now prey"
  ]
end 

;; force all students to become predators

to make-students-predators
  ask students [
    set predator? true
    set shape "wolf"
    set color black
    send-info-to-client
    hubnet-send user-id "status" "You are now a predator"
  ]
end 

;;
;; HubNet Procedures
;;

;; get commands from the clients

to listen-clients
  while [ hubnet-message-waiting? ]
  [
    hubnet-fetch-message
    ifelse hubnet-enter-message?
    [ create-new-student ]
    [
      ifelse hubnet-exit-message?
      [ remove-student ]
      [ execute-command hubnet-message-tag ]
    ]
  ]
end 

;; how to execute a command that was received

to execute-command [ command ]
  ask students with [ user-id = hubnet-message-source ]
  [
    ;; animals can only move if they have energy
    ;; and can only recreate themselves if they are dead
    ifelse energy > 0
    [
      if command = "up"
      [ execute-move 0 ]
      if command = "down"
      [ execute-move 180 ]
      if command = "right"
      [ execute-move 90 ]
      if command = "left"
      [ execute-move 270 ]
    ]
    [
      if command = "recreate as predator"
      [
        setup-student-vars true
        send-info-to-client
        hubnet-send user-id "status" "You are now a predator"
      ]
      if command = "recreate as prey"
      [
        setup-student-vars false
        send-info-to-client
        hubnet-send user-id "status" "You are now prey"
      ]
    ]
    if command = "Change Appearance"
    [ execute-change-turtle ]
    if command = "step-size"
    [
      set step-size hubnet-message
    ]
  ]
end 

to student-die
  set shape "face sad"
  send-info-to-client
  update-energy-monitor
end 

;; create a new student agent

to create-new-student
  create-students 1
  [
    init-student
    send-info-to-client
  ]
end 

;; turtle procedure
;; initialize the student agent

to init-student
  set user-id hubnet-message-source
  move-to one-of patches
  create-icon
  setup-student-vars random 2 = 0
end 

;; to create the variables for a student agent

to setup-student-vars [is-predator?]  ;; turtle procedure
  set predator? is-predator?
  ifelse predator?
  [ set shape "wolf" ]
  [ set shape "sheep" ]
  face one-of neighbors4
  set step-size 1
  set energy start-energy
end 

;; turtle procedure
;; create the icon for each student

to create-icon
  let parent self
  hatch-icons 1
  [
    ask parent [ set my-icon myself ]
    move-to parent
    set-unique-shape-and-color
    set size 1.5
  ]
end 

;; turtle procedure
;; make sure each student has a unique shape and color

to set-unique-shape-and-color
  set color one-of colors
  set shape one-of all-shapes
  set color-name item (position color colors) color-names
end 

;; sends the appropriate monitor information back to the client

to send-info-to-client
  hubnet-send user-id "You are a:" (word [color-name] of my-icon " " [shape] of my-icon)
  hubnet-send user-id "Located at:" (word "(" pxcor "," pycor ")")
  hubnet-send user-id "role" my-role
  update-energy-monitor
end 

;; turtle procedure
;; specify the role

to-report my-role
  if energy <= 0
    [ report "dead" ]
  report ifelse-value (shape = "wolf") [ "predator" ][ "prey" ]
end 

;; show the appropriate amount of energy

to update-energy-monitor
  hubnet-send user-id "energy" max list 0 precision energy 2
  if energy <= 0
  [
    hubnet-send user-id "role" my-role
    hubnet-send user-id "status" "You died, you may recreate as a predator or prey"
  ]
end 

;; remove students that are no longer connected

to remove-student
  ask students with [ user-id = hubnet-message-source ]
  [
    ask my-icon [ die ]
    die
  ]
end 

;; turtle procedure
;; move the student agent

to execute-move [ new-heading ]
  set heading new-heading
  fd step-size
  set energy energy - 0.5
  ask my-icon
  [
    set heading new-heading
    fd [step-size] of myself
  ]
  ifelse predator?
    [ eat-prey ]
    [ eat-grass ]
  update-energy-monitor
  hubnet-send user-id "Located at:" (word "(" pxcor "," pycor ")")
  if energy <= 0
  [ student-die ]
end 

;; turtle procedure
;; change the icon

to execute-change-turtle
  show-turtle
  ask my-icon [ set-unique-shape-and-color ]
  hubnet-send user-id "You are a:" (word ([color-name] of my-icon) " " [shape] of my-icon)
end 

;;
;; Plotting Procedure
;;

to do-plot
  let players turtles with [ breed != icons ]
  set-current-plot "Average Energy"  ;;this plots the total energy of prey, and of predators
  set-current-plot-pen "Predators"
  ifelse any? players with [ predator? ]
    [ plot mean [ energy ] of  players with [ predator? ] ]
    [ plot 0 ]

  set-current-plot-pen "Prey"
  ifelse any? players with [ not predator? ]
    [ plot mean [ energy ] of players with [ not predator? ] ]
    [ plot 0 ]

  set-current-plot "Population"
  set-current-plot-pen "Predators"
  plot count players with [ predator? ]

  set-current-plot-pen "Prey"
  plot count players with [ not predator? ]
end 


; Copyright 2002 Uri Wilensky.
; See Info tab for full copyright and license.

There are 7 versions of this model.

Uploaded by When Description Download
Uri Wilensky over 4 years ago Updated to NetLogo 5.0.4 Download this version
Uri Wilensky almost 5 years ago Updated version tag Download this version
Uri Wilensky almost 5 years ago Updated to version from NetLogo 5.0.3 distribution Download this version
Uri Wilensky almost 6 years ago Updated to NetLogo 5.0 Download this version
Uri Wilensky over 7 years ago Updated from NetLogo 4.1 Download this version
Uri Wilensky over 7 years ago Predator Prey Game Download this version
Uri Wilensky over 7 years ago Predator Prey Game Download this version

Attached files

File Type Description Last updated
Predator Prey Game.png preview Preview for 'Predator Prey Game' over 4 years ago, by Uri Wilensky Download

This model does not have any ancestors.

This model does not have any descendants.