origDTThreads <- data.table::setDTthreads(2L)
origNcpus <- options(Ncpus = 2L)
xrange <- yrange <- c(-50, 50)
hab <- terra::rast(terra::ext(c(xrange, yrange)))
hab[] <- 0
# initialize agents
N <- 10
# previous points
x1 <- y1 <- rep(0, N)
# initial points
starts <- cbind(x = stats::runif(N, xrange[1], xrange[2]),
y = stats::runif(N, yrange[1], yrange[2]))
# create the agent object # the x1 and y1 are needed for "previous location"
agent <- terra::vect(data.frame(x1, y1, starts), geom = c("x", "y"))
ln <- rlnorm(N, 1, 0.02) # log normal step length
sd <- 30 # could be specified globally in params
if (interactive()) {
# clearPlot()
terra::plot(hab, col = "white")
}
for (i in 1:10) {
agent <- crw(agent = agent, extent = terra::ext(hab), stepLength = ln,
stddev = sd, lonlat = FALSE, torus = FALSE) # don't wrap
if (interactive()) terra::plot(agent[, 1], add = TRUE, col = 1:10)
}
terra::crds(agent) # many are "off" the map, i.e., beyond the extent of hab
agent <- SpaDES.tools::wrap(agent, bounds = terra::ext(hab))
terra::plot(agent, add = TRUE, col = 1:10) # now inside the extent of hab
# clean up
data.table::setDTthreads(origDTThreads)
options(Ncpus = origNcpus)
Run the code above in your browser using DataLab