# NOT RUN {
# seed seed for reproducibility
set.seed(600)
# load data
data(sim_pu_raster, sim_features, sim_pu_zones_stack, sim_features_zones)
# create minimal problem with binary decisions
p1 <- problem(sim_pu_raster, sim_features) %>%
add_min_set_objective() %>%
add_relative_targets(0.1) %>%
add_binary_decisions() %>%
add_default_solver(gap = 0, verbose = FALSE)
# }
# NOT RUN {
# solve problem
s1 <- solve(p1)
# print solution
print(s1)
# plot solution
plot(s1, main = "solution", axes = FALSE, box = FALSE)
# calculate irreplaceability scores
rc1 <- replacement_cost(p1, s1)
# print irreplaceability scores
print(rc1)
# plot irreplaceability scores
plot(rc1, main = "replacement cost", axes = FALSE, box = FALSE)
# }
# NOT RUN {
# since replacement cost scores can take a long time to calculate with
# binary decisions, we can calculate them using proportion-type
# decision variables. Note we are still calculating the scores for our
# previous solution (s1), we are just using a different optimization
# problem when calculating the scores.
p2 <- problem(sim_pu_raster, sim_features) %>%
add_min_set_objective() %>%
add_relative_targets(0.1) %>%
add_proportion_decisions() %>%
add_default_solver(gap = 0, verbose = FALSE)
# calculate irreplaceability scores using proportion type decisions
# }
# NOT RUN {
rc2 <- replacement_cost(p2, s1)
# print irreplaceability scores based on proportion type decisions
print(rc2)
# plot irreplacability scores based on proportion type decisions
# we can see that the irreplaceability values in rc1 and rc2 are similar,
# and this confirms that the proportion type decisions are a good
# approximation
plot(rc2, main = "replacement cost", axes = FALSE, box = FALSE)
# }
# NOT RUN {
# build multi-zone conservation problem with binary decisions
p3 <- problem(sim_pu_zones_stack, sim_features_zones) %>%
add_min_set_objective() %>%
add_relative_targets(matrix(runif(15, 0.1, 0.2), nrow = 5,
ncol = 3)) %>%
add_binary_decisions() %>%
add_default_solver(gap = 0, verbose = FALSE)
# }
# NOT RUN {
# solve the problem
s3 <- solve(p3)
# print solution
print(s3)
# plot solution
# each panel corresponds to a different zone, and data show the
# status of each planning unit in a given zone
plot(s3, main = paste0("zone ", seq_len(nlayers(s3))), axes = FALSE,
box = FALSE)
# calculate irreplaceability scores
rc3 <- replacement_cost(p3, s3)
# plot irreplaceability
# each panel corresponds to a different zone, and data show the
# irreplaceability of each planning unit in a given zone
plot(rc3, main = paste0("zone ", seq_len(nlayers(s3))), axes = FALSE,
box = FALSE)
# }
# NOT RUN {
# }
Run the code above in your browser using DataLab