R/generate-nsf.r
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 |
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
.
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.
# generate data r <- raster::raster(nrows = 10, ncols = 10, crs = "+proj=aea", 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 FALSE FALSE FALSE FALSE TRUE FALSE FALSE #> [13] FALSE FALSE TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE #> [25] FALSE FALSE FALSE TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE #> [37] FALSE TRUE TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE #> [49] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE TRUE FALSE FALSE FALSE #> [61] FALSE FALSE FALSE FALSE FALSE FALSE TRUE FALSE FALSE FALSE FALSE FALSE #> [73] FALSE FALSE FALSE TRUE FALSE FALSE FALSE FALSE TRUE FALSE FALSE FALSE #> [85] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE #> [97] FALSE FALSE FALSE FALSEneighbour(selected, pu_list = TRUE)#> [1] 4 5 6 7 8 10 14 15 17 18 19 20 24 25 26 28 29 30 34 35 36 37 38 39 40 #> [26] 46 48 49 50 55 56 57 58 60 61 62 65 66 67 72 75 76 77 78 81 82 85 86 87 91 #> [51] 92 95 96 97