View source: R/add_gap_portfolio.R
| add_gap_portfolio | R Documentation |
Generate a portfolio of solutions for a conservation planning problem by finding a certain number of solutions that are all within a pre-specified optimality gap. This method is useful for generating multiple solutions that can be used to calculate selection frequencies for small-sized problems (similar to Marxan).
add_gap_portfolio(x, number_solutions = 10, pool_gap = 0.1)
x |
|
number_solutions |
|
pool_gap |
|
This strategy for generating a portfolio requires problems to
be solved using the Gurobi software (i.e., using
add_gurobi_solver(). Specifically, version 9.0.0 (or greater)
of the gurobi package must be installed.
Note if the total number of solutions that
meet the optimality gap is fewer than number_solutions,
then the number of solutions returned may be fewer than requested.
Also, note that this portfolio function only works with problems
that have binary decisions (i.e., specified using
add_binary_decisions()).
An updated problem() object with the portfolio added to it.
Other functions for adding portfolios:
add_cuts_portfolio(),
add_default_portfolio(),
add_extra_portfolio(),
add_shuffle_portfolio(),
add_single_portfolio(),
add_top_portfolio()
# set seed for reproducibility
set.seed(600)
# load data
sim_pu_raster <- get_sim_pu_raster()
sim_features <- get_sim_features()
sim_zones_pu_raster <- get_sim_zones_pu_raster()
sim_zones_features <- get_sim_zones_features()
# create minimal problem with a portfolio containing 10 solutions within 20%
# of optimality
p1 <-
problem(sim_pu_raster, sim_features) %>%
add_min_set_objective() %>%
add_relative_targets(0.05) %>%
add_gap_portfolio(number_solutions = 5, pool_gap = 0.2) %>%
add_default_solver(gap = 0, verbose = FALSE)
# solve problem and generate portfolio
s1 <- solve(p1)
# convert portfolio into a multi-layer raster
s1 <- terra::rast(s1)
# print number of solutions found
print(terra::nlyr(s1))
# plot solutions
plot(s1, axes = FALSE)
# create multi-zone problem with a portfolio containing 10 solutions within
# 20% of optimality
p2 <-
problem(sim_zones_pu_raster, sim_zones_features) %>%
add_min_set_objective() %>%
add_relative_targets(matrix(runif(15, 0.1, 0.2), nrow = 5, ncol = 3)) %>%
add_gap_portfolio(number_solutions = 5, pool_gap = 0.2) %>%
add_default_solver(gap = 0, verbose = FALSE)
# solve problem and generate portfolio
s2 <- solve(p2)
# convert portfolio into a multi-layer raster of category layers
s2 <- terra::rast(lapply(s2, category_layer))
# print number of solutions found
print(terra::nlyr(s2))
# plot solutions in portfolio
plot(s2, axes = FALSE)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.