make_walk: Creates a random walk path.

Description Usage Arguments Value Examples

View source: R/make_walk.R

Description

This function creates a two-dimensional movement path as a series of steps. Movement can be correlated (current step length and direction depend on previous step) or uncorrelated. The initial position and orientation of the moving object are given by the arguments x0, y0, dir0, while the length and direction of each step, together with the conditions for accepting a proposed step and detecting when movement should end are given by functions supplied as arguments. The algorithm proceeds as follows:

  1. The stop.cond function is called to check if movement is finished. Note: the first check takes place before any steps have been taken so a function can reject an unacceptable start position or orientation.

  2. An angle is drawn from the function next.angle and the object's orientation is updated. If turning.angles is TRUE the angle is treated as a turning angle which is added to the previous orientation. If turning.angles is FALSE, the object's new orientation is simply set to the angle. In both cases, the new orientation is normalized to (-pi, pi].

  3. A step length is chosen using function next.step.

  4. A new candidate position is calculated for the given orientation and step length.

  5. The candidate position is tested with the accept.pos function; if accepted, the object moves to this position; otherwise a new candidate position is chosen. If no position is accepted after accept.limit attempts, the movement terminates.

This process continues until the stop.cond function signals completion or no more candidate positions can be found.

Usage

1
2
3
make_walk(x0, y0, dir0, next.angle, next.steplen = 1,
  accept.pos = function(x, y) TRUE, stop.cond = function(wdat) wdat$nsteps
  >= 100, turning.angles = TRUE, accept.limit = 100, id = 1)

Arguments

x0,y0

Ordinates of the starting location.

dir0

Initial orientation (radians). If NULL or missing, a random angle in the interval (-pi, pi] will be used.

next.angle

A function to generate angles. The function should take a single argument (a walk.data object).

next.steplen

Either a function to generate step lengths, or a numeric value for constant step length. If a function, it should take a single argument (a walk.data object).

accept.pos

A function to test if a candidate position is acceptable. The function should take two arguments: x, y ordinates of the proposed position.

stop.cond

A function to test if movement is finished. The function should take a single argument (a walk.data object).

turning.angles

If TRUE (default), angles from the next.angle function are taken as turning angles, and the orientation of the moving object at each step is the sum of its previous orientation and the turning angle. This results in a correlated random walk. If FALSE, angles are taken as the new orientation for each step.

accept.limit

Maximum number of candidate positions to test at each move. If exceeded, the movement is finished.

id

An identifer (integer or character) to store in the id field of the returned walk.data object. If missing or NULL it will be set to 1.

Value

A named list (class walk.data) with elements:

id

identifier (integer or character)

x

vector of X ordinates

y

vector of Y ordinates

dir

vector of orientations (radians)

xstart

first X ordinate (same as x[1])

xend

last X ordinate (same as x[ length(x) ])

ystart

first Y ordinate (same as y[1])

yend

last Y ordinate (same as y[ length(y) ])

steplen

vector of step lengths (first element will be 0)

dist.direct

direct line distance from the start to the end of the path

dist.path

distance along the path (sum of step lengths)

nsteps

number of steps taken

status

one of: 'active' while movement is continuing (ie. when walk data is passed to angle, step length and stopping functions); 'finished' if movement terminated normally; 'failed' if movement terminated because no further acceptable positions could be found.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
## Not run: 
# Start position and orientation
x0 <- 0
y0 <- 0
d0 <- runif(1, -pi, pi)

# Function to draw turning angles from a truncated Laplace distribution
# (walk.data argument is ignored)
angle <- function(wdat) rtrunclaplace(1, 0, 1, -pi, pi)

# Function for step lengths (walk.data argument ignored)
steplen <- function(wdat) 1.0

# Function to test acceptance of a proposed new position.
# As an example, we will reject any positions below a given Y.
posfn <- function(x, y) y > 10

# Function to test if movement is finished.
# As an example, we stop when either the direct distance from
# the start point or the total path distance exceed given values.
stopfn <- function(wdat) wdat$dist.direct > 20 || wdat$dist.path > 50

w <- make_walk(
  x0, y0, d0,
  next.angle = anglefn,
  next.step = lenfn,
  accept.pos = posfn,
  stop.cond = stopfn)

# simple plot of the movement path with base graphics
with(w, plot(x, y, type="l"))

## End(Not run)

mbedward/walkies documentation built on May 22, 2019, 12:19 p.m.