Skip to content

Instantly share code, notes, and snippets.

@Martin-Jung
Created July 14, 2022 09:46
Show Gist options
  • Save Martin-Jung/354b8de4c8be5814c56a2ab97a942f1b to your computer and use it in GitHub Desktop.
Save Martin-Jung/354b8de4c8be5814c56a2ab97a942f1b to your computer and use it in GitHub Desktop.
################# other format ##################
#' @param x [`Raster-class`],
#' [sf::st_sf()],
#' [`SpatialPolygonsDataFrame-class`],
#' [`SpatialLinesDataFrame-class`],
#' [`SpatialPointsDataFrame-class`],
#' [data.frame()] object,
#' [numeric()] vector, or
#' [matrix()] specifying the planning units to use in the reserve
#' design exercise and their corresponding cost. It may be desirable to
#' exclude some planning units from the analysis, for example those outside
#' the study area. To exclude planning units, set the cost for those raster
#' cells to `NA`, or use the `add_locked_out_constraint` function.
pu <- data.frame(
id = seq_len(10),
cost_z1 = c(0.3, NA, runif(8)),
cost_z2 = c(0.3, NA, runif(8))
)
#' @param cost_column `character` name or `integer` indicating the
#' column(s) with the cost data. This argument must be supplied when the
#' argument to `x` is a [`Spatial-class`] or
#' `data.frame` object. This argument should contain the name of each
#' column containing cost data for each management zone when creating
#' problems with multiple zones. To create a problem with a single zone, then
#' set the argument to `cost_column` as a single column name.
cost_column <- colnames(pu)[2:ncol(pu)]
#' @param rij `data.frame` containing information on the amount of
#' each feature in each planning unit assuming each management zone. Similar
#' to `data.frame` arguments for `features`, the `data.frame`
#' objects must follow the conventions used by *Marxan*. Note that the
#' `"zone"` column is not needed for problems involving a single
#' management zone. Specifically, the argument should contain the following
#' columns:
#' \describe{
#' \item{pu}{`integer` planning unit identifier.}
#' \item{species}{`integer` feature identifier.}
#' \item{zone}{`integer` zone identifier (optional for
#' problems involving a single zone).}
#' \item{amount}{`numeric` amount of the feature in the
#' planning unit.}
rij <- data.frame(
pu = rep(seq_len(10), 6),
species = c(rep(1,20),rep(2,20), rep(3,20)),
zone = rep(c(rep(1,10), rep(2,10)),3),
amount = runif(60)
)
#' @param zones `data.frame` containing information on the zones. This
#' argument is only used when argument to `x` and `y` are
#' both `data.frame` objects and the problem being built contains
#' multiple zones. Following conventions used in `MarZone`, this
#' argument should contain the following columns:
#' columns:
#' \describe{
#' \item{id}{`integer` zone identifier.}
#' \item{name}{`character` zone name.}
#' }
z <- data.frame(
id = c(1,2),
name = c("z1", "z2")
)
# create targets
targs <- matrix(runif(6, max = 0.2), ncol = 2, nrow = 3)
# Create features file
feat <- data.frame(id = unique(rij$species),
name = c("species1", "species2", "species3"),
prop = 1
)
# create problem
p <-
problem(x = pu, features = feat,
zones = z,
cost_column = cost_column,
rij = rij) %>%
add_min_set_objective() %>%
add_relative_targets(targs) %>%
add_proportion_decisions()
# solve problem
s <- solve(p)
# print solution
print(s)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment