generate_nsf | R Documentation |
Optimization by simulated annealing requires a function to select a neighbouring state to test given the current state. This function generates such a neighbour selection function. The resulting function will swap the status of a single planning unit.
generate_nsf(pu, buffer, locked, recalculate = 1L)
pu |
raster::RasterStack, sp::SpatialPolygonsDataFrame, or sf::sf object; planning units. |
buffer |
numeric; a buffer around the perimeter of the currently selected planning units in the units of the projection. Any planning units within this buffer will be considered for switching when generating a neighbouring state. |
locked |
name of a binary column (for vector data) or layer (for raster data) that specified planning units that are locked in or out and should be left unchanged. Alternatively, a integer vector of planning unit numbers may be supplied specifying units to leave unchanged. |
recalculate |
integer; frequency at which the set of planning units to
consider for switching should be recalculated, e.g. a value of 100 would
result in recalculating every 100 calls to the returned function. This
process of recalculating can be computationally intensive for large sets
of planning units, so choosing a larger value for |
In an attempt to more intelligently select a planning unit to switch, only those planning units meeting the following criteria will be considered:
Units touching the boundary of the existing reserve network, whether inside or outside.
Units within a defined distance, buffer
, of the boundary of the existing
reserve network.
This ensures that units in the interior of an existing reserve or far from an existing reserve are not changed.
A function to choose a neighbouring state given a vector of binary
decision variables. To return the list of planning units under consideration
for switching use pu_list = TRUE
.
# generate data
r <- raster::raster(nrows = 10, ncols = 10, crs = "+proj=laea", vals = 0)
# lock some units to be unchanged
names(r) <- "locked_out"
r[sample(1:100, 10)] <- 1
pus <- raster::rasterToPolygons(r)
selected <- sample(c(FALSE, TRUE), 100, replace = TRUE, prob = c(0.9, 0.1))
# generate neighbour selection function
neighbour <- generate_nsf(pus, buffer = 20, locked = "locked_out")
# neighbouring solution
neighbour(selected)
neighbour(selected, pu_list = TRUE)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.