Foxes and Rabbits 2 NetLogo Model
Produced for the book series "Artificial Intelligence";
Author: W. J. Teahan; Publisher: Ventus Publishing Aps.
powered by NetLogo
view/download model file: Foxes-and-Rabbits-2.nlogo
WHAT IS IT?
This model shows how to use breeds to create two different breeds of turtle agents - foxes and rabbits - that can have different properties such as colour. Once the foxes and rabbits have been created, the rabbits that are too near to the foxes move away until they feel they are at a safe distance. The colour of the rabbit or rabbits is temporarily switched to magenta when they are moving away.
HOW TO USE IT
Press the setup button several times to see how the agents spread themselves throughout the environment. Then press the go button to see the rabbits move away from the foxes.
To show the paths of the rabbits, turn the Show-paths? switch to On. To clear the paths, press the clear-paths button.
If there are many agents, you will need to speed up the simulation using the speed slider in the Interface.
THE INTERFACE
The Interface buttons are defined as follows:
- setup: This will create a new population of foxes and rabbits at random locations.
- go: This will start the simulation. All the rabbits will move away from the foxes.
- clear-paths: This clears the rabbits' paths if any are drawn.
The Interface chooser, switches and sliders are defined as follows:
- movement-type: This controls the behaviour of the rabbits. If set to "All rabbits at once", the rabbits move apart all at the same time, one step at a time. If set to "One rabbit at a time", the rabbits will take turns until each is at a satisfactory distance from the fox nearest to it. (This is admittedly rather unrealistic behaviour, a bit like what happens in Kung Fu movies when the baddies attack the hero, but do this one at a time).
- Use-shapes?: If set to On, this will draw the agents using fox and rabbit shapes. If set to Off, it will fall back to the default shape.
- Show-paths?: If set to On, this will draw the paths of the rabbits that are moving.
- Show-cones?: If set to On, this will draw a cone around the foxes showing the distances within which the rabbits will feel that it is not safe. (This distance is controlled by the rabbit-radius slider).
- no-of-foxes: This is the number of foxes that will be created when the setup button is pressed.
- no-of-rabbits: This is the number of rabbits that will be created when the setup button is pressed.
- rabbit-radius: This is the distance away from a fox for which the rabbit will feel unsafe.
- wiggle-angle: This controls the wiggle behaviour of the rabbit as it moves. A larger number results in more random movement; a value of 0 will result in the rabbit heading in straight lines unless it comes too near another fox.
HOW IT WORKS
It uses two turtle agent breeds, foxes and rabbits, and shows how you can use the turtles-own command so that both breeds end up with common variables - age and gender.
The setup command simply calls the create-foxes and create-rabbits command to create the agents. These are created at random locations. The foxes move apart slightly to avoid being too near each other. The rabbits will turn away from the nearest fox.
The go command directs the rabbits that are nearby to each fox to move away all at the same time or one at a time, depending on the value of the movement-type slider.
WHAT IS ITS PURPOSE?
Its purpose is to show how to define and create breeds of agents, and provide an example of agent movement.
THINGS TO NOTICE
Notice the different types of behaviour that arise when the movement-type chooser is set to "All rabbits at once" and "One rabbit at a time". Even though the base rabbit behaviour is the same for both - that is, both use the same run-away procedure - the overall system behaviour is very different. When the movement-type slider is set at "All rabbits at once", quite a few of the rabbits end up being "trapped", usually between three equidistant foxes. Also, if you turn on the Show-paths? switch, the paths that are drawn are very different for the different types of movement.
THINGS TO TRY
Try changing the values of the chooser, switches and sliders to see what happens.
EXTENDING THE MODEL
Try having the foxes move around chasing the rabbits rather than stay still.
RELATED MODELS
See the Foxes and Rabbits model.
CREDITS AND REFERENCES
To refer to this model in publications, please use:
Teahan, W. J. (2010). Foxes and Rabbits NetLogo model.
Artificial Intelligence. Ventus Publishing Aps.
PROCEDURES
; Foxes and Rabbits model.
;
; Creates some fox and rabbit turtle agents in the environment at random.
;
; Copyright 2010 William John Teahan. All Rights Reserved.
;
breed [foxes fox]
breed [rabbits rabbit]
turtles-own [age gender]
to setup
; Creates the foxes and rabbits at random positions.
clear-all ;; clear everything
create-foxes no-of-foxes
[
if (Use-shapes?)
[ set shape "fox" ]
set age 0
set size 8
ifelse random 2 = 0
[ set gender "Male"
set color brown ]
[ set gender "Female"
set color grey ]
setxy random-xcor random-ycor
if (count foxes > 1)
[ set heading towards min-one-of other foxes [distance myself] + 180 ] ; point away from nearest fox
if (count foxes in-radius 10 > 1) ; move away from the nearest fox
[ forward 5 ]
]
create-rabbits no-of-rabbits
[
if (Use-shapes?)
[ set shape "rabbit" ]
ifelse random 2 = 0
[ set gender "Male"
set color white ]
[ set gender "Female"
set color yellow ]
set age 0
set size 4
setxy random-xcor random-ycor
set heading towards min-one-of foxes [distance myself] + 180 ; point away from nearest fox
]
if (Show-cones?)
[ ask foxes
[ ask patches in-radius rabbit-radius
[ set pcolor sky - 4 ]
]
]
end
to go
; The rabbits run away from the foxes.
let ran-away? false
let all-safe? true
ask foxes
[
let nearby-rabbits rabbits in-radius rabbit-radius
display
if (count nearby-rabbits > 1)
[
ask nearby-rabbits
[
let this-colour color
set color magenta ; the rabbit is scared by a nearby fox
if (movement-type = "All rabbits at once")
[ ; all rabbits move away from the nearest fox incrementally at the same time
run-away
display
]
if (movement-type = "One rabbit at a time")
[ ; one rabbit moves away until it is safe
while [count foxes in-radius rabbit-radius > 0]
[
run-away
display
]
]
set color this-colour ; the rabbit feels safe again
]
]
]
end
to clear-paths
;; Removes any turtle paths.
clear-drawing
end
to wiggle
; The rabbit wiggles a bit i.e. This adds a bit of randomness to its behaviour.
rt random wiggle-angle
lt random wiggle-angle
forward 1
end
to run-away
; The rabbit tries to run away from the nearest fox.
if (movement-type = "All rabbits at once")
[ set heading towards min-one-of foxes [distance myself] + 180 ] ; point away from nearest fox
if Show-paths?
[ pen-down ]
wiggle
end
;
; Copyright 2010 by William John Teahan. All rights reserved.
;
; Permission to use, modify or redistribute this model is hereby granted,
; provided that both of the following requirements are followed:
; a) this copyright notice is included.
; b) this model will not be redistributed for profit without permission
; from William John Teahan.
; Contact William John Teahan for appropriate licenses for redistribution for
; profit.
;
; To refer to this model in publications, please use:
;
; Teahan, W. J. (2010). Foxes and Rabbits NetLogo model.
; Artificial Intelligence. Ventus Publishing Aps
;
