Skip to content
Prev 174423 / 398502 Next

limiting simulated animal movement

On Sat, Mar 21, 2009 at 1:35 PM, Umesh Srinivasan
<umesh.srinivasan at gmail.com> wrote:
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