Skip to content

Instantly share code, notes, and snippets.

@Martin-Jung
Created April 17, 2025 20:52
Show Gist options
  • Select an option

  • Save Martin-Jung/aa3939c3835857b2254d7bc1c46ffc5d to your computer and use it in GitHub Desktop.

Select an option

Save Martin-Jung/aa3939c3835857b2254d7bc1c46ffc5d to your computer and use it in GitHub Desktop.
Solution Fragmentation tests using prioritizr testing data
# load packages
library(prioritizr)
library(prioritizrdata)
library(terra)
# import planning unit data
wa_pu <- get_wa_pu()
# preview data
print(wa_pu)
wa_pu[!is.na(wa_pu)] <- 1
# import feature data
wa_features <- get_wa_features()
# calculate budget
budget <- terra::global(wa_pu, "sum", na.rm = TRUE)[[1]] * 0.3
# create problem
p1 <-
problem(wa_pu, features = wa_features) %>%
add_relative_targets(0.2) %>%
add_proportion_decisions() %>%
add_default_solver(gap = 0.1, verbose = FALSE)
# Minimum shortfall
s1 <- solve(p1 |> add_min_shortfall_objective(budget) )
names(s1) <- "minshort"
# Repeat all but with binary decisions - max cov
s2 <- solve(p1 |> add_min_shortfall_objective(budget) |> add_binary_decisions() )
names(s2) <- "minshort_binary"
# --- #
# For ambitious targets
p2 <- p1 |>
add_loglinear_targets(10, 0.9, 1000, 0.2)
# Minimum shortfall
s3 <- solve(p2 |> add_min_shortfall_objective(budget) )
names(s3) <- "minshort_loglinear"
# Repeat all but with binary decisions
s4 <- solve(p2 |> add_min_shortfall_objective(budget) |> add_binary_decisions() )
names(s4) <- "minshort_loglinear_binary"
# Plot all next to each other
plot(c(s1,s2, s3,s4))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment