# NOT RUN {
# load ape package
require(ape)
# load data
data(sim_pu_raster, sim_features, sim_phylogeny, sim_pu_zones_stack,
sim_features_zones)
# plot the simulated phylogeny
# }
# NOT RUN {
par(mfrow = c(1, 1))
plot(sim_phylogeny, main = "phylogeny")
# }
# NOT RUN {
# create problem with a maximum phylogenetic diversity 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_div_objective(1900, sim_phylogeny) %>%
add_relative_targets(0.1) %>%
add_binary_decisions()
# }
# NOT RUN {
# solve problem
s1 <- solve(p1)
# plot solution
plot(s1, main = "solution", axes = FALSE, box = FALSE)
# find which features have their targets met
r1 <- feature_representation(p1, s1)
r1$target_met <- r1$relative_held > 0.1
print(r1)
# plot the phylogeny and color the adequately represented features in red
plot(sim_phylogeny, main = "adequately represented features",
tip.color = replace(
rep("black", nlayers(sim_features)),
sim_phylogeny$tip.label %in% r1$feature[r1$target_met],
"red"))
# }
# NOT RUN {
# rename the features in the example phylogeny for use with the
# multi-zone data
sim_phylogeny$tip.label <- feature_names(sim_features_zones)
# 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_features_zones),
zone = list(zone_names(sim_features_zones))[rep(1,
number_of_features(sim_features_zones))],
type = rep("absolute", number_of_features(sim_features_zones)),
target = rep(10, number_of_features(sim_features_zones)))
# create a multi-zone problem with a maximum phylogenetic diversity
# objective, where the total expenditure in all zones is 5000.
p2 <- problem(sim_pu_zones_stack, sim_features_zones) %>%
add_max_phylo_div_objective(5000, sim_phylogeny) %>%
add_manual_targets(targets) %>%
add_binary_decisions()
# }
# NOT RUN {
# solve problem
s2 <- solve(p2)
# plot solution
plot(category_layer(s2), main = "solution", axes = FALSE, box = FALSE)
# calculate total amount of habitat conserved for each feature among
# all three management zones
amount_held2 <- numeric(number_of_features(sim_features_zones))
for (z in seq_len(number_of_zones(sim_features_zones)))
amount_held2 <- amount_held2 +
cellStats(sim_features_zones[[z]] * s2[[z]], "sum")
# find which features have their targets met
targets_met2 <- amount_held2 >= targets$target
print(targets_met2)
# plot the phylogeny and color the adequately represented features in red
plot(sim_phylogeny, main = "adequately represented features",
tip.color = replace(rep("black", nlayers(sim_features)),
which(targets_met2), "red"))
# }
# NOT RUN {
# create a multi-zone problem with a maximum phylogenetic diversity
# objective, where each zone has a separate budget.
p3 <- problem(sim_pu_zones_stack, sim_features_zones) %>%
add_max_phylo_div_objective(c(2500, 500, 2000), sim_phylogeny) %>%
add_manual_targets(targets) %>%
add_binary_decisions()
# }
# NOT RUN {
# solve problem
s3 <- solve(p3)
# plot solution
plot(category_layer(s3), main = "solution", axes = FALSE, box = FALSE)
# calculate total amount of habitat conserved for each feature among
# all three management zones
amount_held3 <- numeric(number_of_features(sim_features_zones))
for (z in seq_len(number_of_zones(sim_features_zones)))
amount_held3 <- amount_held3 +
cellStats(sim_features_zones[[z]] * s3[[z]], "sum")
# find which features have their targets met
targets_met3 <- amount_held3 >= targets$target
print(targets_met3)
# plot the phylogeny and color the adequately represented features in red
plot(sim_phylogeny, main = "adequately represented features",
tip.color = replace(rep("black", nlayers(sim_features)),
which(targets_met3), "red"))
# }
Run the code above in your browser using DataLab