View source: R/analysis_solution.R
| solution_filter | R Documentation |
Return a reduced solutionset-class object containing only the
runs or solutions that match the requested filters.
This function is intended to curate SolutionSet objects before
downstream analysis, plotting, frontier analysis, or post-hoc evaluation.
It filters all relevant components of the object consistently, including the
run table, design table, stored run-level solutions, and available summary
tables.
solution_filter(
x,
run_id = NULL,
solution_id = NULL,
status = NULL,
feasible_only = FALSE,
nondominated = FALSE,
objectives = NULL
)
x |
A |
run_id |
Optional integer vector of run ids to keep. |
solution_id |
Optional numeric vector of positive integer solution ids to keep. Runs without a stored solution are never matched by this filter. |
status |
Optional character vector of run statuses to keep. Matching is case-insensitive. |
feasible_only |
Logical. If |
nondominated |
Logical. If |
objectives |
Optional character vector of objective names to use when
|
A SolutionSet distinguishes between runs and stored solutions.
run_id identifies a run or attempted solve. Runs may be
feasible, optimal, infeasible, failed, or otherwise incomplete.
solution_id identifies a stored solution. Runs that did not
produce a solution have solution_id = NA.
Therefore, filtering by run_id and filtering by solution_id
are not always equivalent. For example, an infeasible run may have a
run_id but no solution_id.
The function filters:
x$solution$runs, using the selected run_ids;
x$solution$design, when it contains a run_id column;
x$solution$solutions, using the selected solution_ids;
all tables in x$summary that contain a run_id column.
The function does not renumber run_id or solution_id. This
preserves traceability to the original run design.
If more than one filter is supplied, filters are combined using logical
and. For example, setting both status = "optimal" and
solution_id = c(1, 3) keeps only optimal runs whose
solution_id is either 1 or 3.
If nondominated = TRUE, the function further keeps only non-dominated
solutions among the runs retained by the previous filters. Dominance is
evaluated in objective space using the objective values stored in the run
table. Objective senses are obtained from the objective specifications stored in
the original problem.
Non-dominated filtering requires the moocore package.
A filtered solutionset-class object.
solutionset-class,
solve,
get_runs,
get_objectives,
get_planning_units,
get_actions
# Load a complete simulated planning problem.
example_data <- load_sim_multiaction()
problem <- create_problem(
pu = example_data$planning_units,
features = example_data$features,
dist_features = example_data$dist_features,
cost = "cost"
) |>
add_actions(
example_data$actions,
cost = example_data$action_costs
) |>
add_effects(
example_data$effect_assumptions
) |>
add_constraint_targets_relative(0.05) |>
add_objective_min_cost(alias = "cost", include_pu_cost = FALSE) |>
add_objective_max_effect(alias = "benefit") |>
set_method_weighted_sum(
aliases = c("cost", "benefit"),
runs = set_runs_grid(n = 3),
normalize_weights = TRUE
)
if (requireNamespace("rcbc", quietly = TRUE)) {
problem <- set_solver_cbc(
problem,
verbose = FALSE
)
solutions <- solve(problem)
runs <- get_runs(solutions)
# Keep only runs with a usable solver status
feasible_solutions <- solution_filter(
solutions,
feasible_only = TRUE
)
# Keep selected runs
selected_runs <- solution_filter(
solutions,
run_id = runs$run_id[1:2]
)
# Keep one stored solution
solution_ids <- runs$solution_id[
!is.na(runs$solution_id)
]
if (length(solution_ids) > 0L) {
selected_solution <- solution_filter(
solutions,
solution_id = solution_ids[1]
)
}
# Keep only optimal runs
if ("optimal" %in% tolower(runs$status)) {
optimal_solutions <- solution_filter(
solutions,
status = "optimal"
)
}
# Keep only non-dominated solutions
if (requireNamespace("moocore", quietly = TRUE)) {
nondominated_solutions <- solution_filter(
solutions,
feasible_only = TRUE,
nondominated = TRUE
)
# Evaluate dominance using selected objectives
nondominated_subset <- solution_filter(
solutions,
feasible_only = TRUE,
nondominated = TRUE,
objectives = c("cost", "benefit")
)
}
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.