# [R] limiting simulated animal movement

Rainer M Krug r.m.krug at gmail.com
Sat Mar 21 13:07:49 CET 2009

On Sat, Mar 21, 2009 at 1:35 PM, Umesh Srinivasan
<umesh.srinivasan at gmail.com> wrote:
> Hi,
>
> I am trying to simulate animal movement in a gridded landscape made up of
> cells. At each time step (iteration), the animal moves from one cell to
> another in a random fashion.
> This is how I am simulating movement, where a and b are the x,y co-ordinates
> of the animal at the previous time step:
>
> for (i in 1:no.of.steps){
> direction <- sample(1:8, 1)
>
> if(direction == 1){
> a <- a
> b <- b - 1}
> if(direction == 2){
> a <- a
> b <- b + 1}
> if(direction == 3){
> a <- a - 1
> b <- b}
> if(direction == 4){
> a <- a + 1
> b <- b}
> if(direction == 5){
> a <- a - 1
> b <- b + 1}
> if(direction == 6){
> a <- a + 1
> b <- b + 1}
> if(direction == 7){
> a <- a + 1
> b <- b - 1}
> if(direction == 8){
> a <- a - 1
> b <- b - 1}
>
> The problem is
> 1. I want to limit animals to a pre-defined circular home range
> 2. I want to limit animals to the borders of the landscape itself

Before you can answer question 1 or two, you have to decide what the
animal should do when it reaches the boundary. Options are
a) it just stays where it is, and ignores the step which would lead
utside the home range / simulated landscape, it could be "reflected"
(like light from a mirror), or move along the border. In the case of
the simulated landscape, there are other possibilities, but I don't
think they are appropriate for your case.

The next step is to define the cells which belong to the allowed range
in which the animal is allowed to move (e.g. a matrix with 0, where it
is not allowed, and 1 where it is). The next step is to let your
animal start walking, as you are doing, and then check if the
destination cell is 0 or 1. Then you can decide, if it is 0, what you
want to do. You have to remember the cell from which it is walking, to
be able to react accordingly.

a function which returns the movement in x and the movement in y
direction, would be the best approach.

i.e (UNTESTED):

nextStep <- function() {
dx <- sample(c(-1, 0, 1), 1)
dy <- sample(c(-1, 0, 1), 1)

## if it does not move, try again
## this could be done more elegant, but it should work
if(dx==0 & dy==0) {
d <- nextStep()
dx <- d[1]
dy <- d[2]
}
d <- c(dx, dy)
names(d) <- c("dx", "dy")
return(d)
}

Hope this helps,

Rainer

>
>
> I tried to limit animals to their home range using:
>
> if (sqrt((x - a)^2 + (y - b)^2) > radius.range) {
> a <- a[i-1]
> b <- b[i-1]
> }
>
> Where x and y are co-ordinates for the centre of the home range
>
> But this is not working - giving NA values for x and y co-ordinates. Does
> anyone know what to do?
>
> Thanks and cheers,
> Umesh
>
>        [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>

--
Rainer M. Krug, Centre of Excellence for Invasion Biology,
Stellenbosch University, South Africa