if (FALSE) {
# load ape package
require(ape)
# load data
sim_pu_raster <- get_sim_pu_raster()
sim_features <- get_sim_features()
sim_phylogeny <- get_sim_phylogeny()
sim_zones_pu_raster <- get_sim_zones_pu_raster()
sim_zones_features <- get_sim_zones_features()
# plot the simulated phylogeny
par(mfrow = c(1, 1))
plot(sim_phylogeny, main = "phylogeny")
# create problem with a maximum phylogenetic endemism objective,
# where each feature needs 10% of its distribution to be secured for
# it to be adequately conserved and a total budget of 1900
p1 <-
problem(sim_pu_raster, sim_features) %>%
add_max_phylo_end_objective(1900, sim_phylogeny) %>%
add_relative_targets(0.1) %>%
add_binary_decisions() %>%
add_default_solver(verbose = FALSE)
# solve problem
s1 <- solve(p1)
# plot solution
plot(s1, main = "solution", axes = FALSE)
# find out which features have their targets met
r1 <- eval_target_coverage_summary(p1, s1)
print(r1, width = Inf)
# plot the phylogeny and color the adequately represented features in red
plot(
sim_phylogeny, main = "adequately represented features",
tip.color = replace(
rep("black", terra::nlyr(sim_features)),
sim_phylogeny$tip.label %in% r1$feature[r1$met],
"red"
)
)
# rename the features in the example phylogeny for use with the
# multi-zone data
sim_phylogeny$tip.label <- feature_names(sim_zones_features)
# create targets for a multi-zone problem. Here, each feature needs a total
# of 10 units of habitat to be conserved among the three zones to be
# considered adequately conserved
targets <- tibble::tibble(
feature = feature_names(sim_zones_features),
zone = list(zone_names(sim_zones_features))[
rep(1, number_of_features(sim_zones_features))],
type = rep("absolute", number_of_features(sim_zones_features)),
target = rep(10, number_of_features(sim_zones_features))
)
# create a multi-zone problem with a maximum phylogenetic endemism
# objective, where the total expenditure in all zones is 5000.
p2 <-
problem(sim_zones_pu_raster, sim_zones_features) %>%
add_max_phylo_end_objective(5000, sim_phylogeny) %>%
add_manual_targets(targets) %>%
add_binary_decisions() %>%
add_default_solver(verbose = FALSE)
# solve problem
s2 <- solve(p2)
# plot solution
plot(category_layer(s2), main = "solution", axes = FALSE)
# find out which features have their targets met
r2 <- eval_target_coverage_summary(p2, s2)
print(r2, width = Inf)
# plot the phylogeny and color the adequately represented features in red
plot(
sim_phylogeny, main = "adequately represented features",
tip.color = replace(
rep("black", terra::nlyr(sim_features)), which(r2$met), "red"
)
)
# create a multi-zone problem with a maximum phylogenetic endemism
# objective, where each zone has a separate budget.
p3 <-
problem(sim_zones_pu_raster, sim_zones_features) %>%
add_max_phylo_end_objective(c(2500, 500, 2000), sim_phylogeny) %>%
add_manual_targets(targets) %>%
add_binary_decisions() %>%
add_default_solver(verbose = FALSE)
# solve problem
s3 <- solve(p3)
# plot solution
plot(category_layer(s3), main = "solution", axes = FALSE)
# find out which features have their targets met
r3 <- eval_target_coverage_summary(p3, s3)
print(r3, width = Inf)
# plot the phylogeny and color the adequately represented features in red
plot(
sim_phylogeny, main = "adequately represented features",
tip.color = replace(
rep("black", terra::nlyr(sim_features)), which(r3$met), "red"
)
)
}
Run the code above in your browser using DataLab