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.
Arguments
- 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
recalculate
will speed up processing time.
Value
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
.
Details
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.
Examples
# 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)
#> [1] FALSE FALSE FALSE FALSE FALSE TRUE FALSE FALSE FALSE FALSE TRUE FALSE
#> [13] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE TRUE
#> [25] TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE TRUE TRUE FALSE
#> [37] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
#> [49] FALSE FALSE FALSE FALSE TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
#> [61] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE TRUE
#> [73] FALSE FALSE FALSE FALSE TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
#> [85] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
#> [97] FALSE FALSE FALSE FALSE
neighbour(selected, pu_list = TRUE)
#> [1] 1 2 3 4 6 7 11 12 13 14 15 16 17 21 22 24 25 26 27 32 33 34 35 36 42
#> [26] 43 44 46 51 52 53 54 55 56 57 58 61 62 64 66 67 71 72 74 77 78 81 82 83 86
#> [51] 87 88 91 92 93 96 97 98