tests/testthat/helper-new-api.R

make_explicit_mo_problem <- function(
    n = 5L,
    method = c("weighted", "epsilon")
) {
  method <- match.arg(method)

  pu <- data.frame(
    id = 1:4,
    cost = c(1, 2, 3, 4)
  )

  features <- data.frame(
    id = 1:2,
    name = c("sp1", "sp2")
  )

  dist_features <- data.frame(
    pu = c(1, 1, 2, 3, 4),
    feature = c(1, 2, 2, 1, 2),
    amount = c(5, 2, 3, 4, 1)
  )

  actions <- data.frame(
    id = c("conservation", "restoration")
  )

  effects <- data.frame(
    action = rep(actions$id, each = 2),
    feature = rep(features$id, times = 2),
    relative_change = c(
      0, 0,
      0.5, 0.5
    )
  )

  x <- multiscape::create_problem(
    pu = pu,
    features = features,
    dist_features = dist_features,
    cost = "cost"
  ) |>
    multiscape::add_actions(
      actions = actions,
      cost = c(
        conservation = 1,
        restoration = 2
      )
    ) |>
    multiscape::add_effects(
      effects = effects
    ) |>
    multiscape::add_constraint_targets_relative(0.05) |>
    multiscape::add_objective_min_cost(alias = "cost") |>
    multiscape::add_objective_max_effect(alias = "benefit")

  if (identical(method, "weighted")) {
    x <- multiscape::set_method_weighted_sum(
      x,
      aliases = c("cost", "benefit"),
      runs = multiscape::set_runs_grid(
        n = n
      ),
      normalize_weights = TRUE
    )
  } else {
    x <- multiscape::set_method_epsilon_constraint(
      x,
      primary = "cost",
      runs = multiscape::set_runs_grid(
        n = n
      )
    )
  }

  multiscape::set_solver_cbc(
    x,
    gap_limit = 0,
    verbose = FALSE
  )
}


solve_explicit_mo_problem <- function(
    n = 5L,
    method = c("weighted", "epsilon")
) {
  skip_if_no_cbc()

  multiscape::solve(
    make_explicit_mo_problem(
      n = n,
      method = method
    )
  )
}

Try the multiscape package in your browser

Any scripts or data that you put into this service are public.

multiscape documentation built on Oct. 12, 2026, 5:08 p.m.