Learn R Programming

prioritizr (version 5.0.3)

solve: Solve

Description

Solve a conservation planning problem().

Usage

# S4 method for OptimizationProblem,Solver
solve(a, b, ...)

# S4 method for ConservationProblem,missing solve(a, b, ..., run_checks = TRUE, force = FALSE)

Arguments

a

problem() (i.e. '>ConservationProblem) or '>OptimizationProblem object.

b

'>Solver object. Not used if a is an '>ConservationProblem object.

...

arguments passed to compile().

run_checks

logical flag indicating whether presolve checks should be run prior solving the problem. These checks are performed using the presolve_check() function. Defaults to TRUE. Skipping these checks may reduce run time for large problems.

force

logical flag indicating if an attempt to should be made to solve the problem even if potential issues were detected during the presolve checks. Defaults to FALSE.

Value

A numeric, matrix, '>RasterLayer, '>Spatial, or sf::sf() object containing the solution to the problem. Additionally, the returned object will have the following additional attributes: "objective" containing the solution's objective, "runtime" denoting the number of seconds that elapsed while solving the problem, and "status" describing the status of the solution (e.g. "OPTIMAL" indicates that the optimal solution was found). In most cases, the first solution (e.g. "solution_001") will contain the best solution found by the solver (note that this may not be an optimal solution depending on the gap used to solve the problem and noting that the default gap is 0.1).

Details

After formulating a conservation planning problem(), it can be solved using an exact algorithm solver (see solvers for available solvers). If no solver has been explicitly specified, then the best available exact algorithm solver will be used by default (see add_default_solver(). Although these exact algorithm solvers will often display a lot of information that isn't really that helpful (e.g. nodes, cutting planes), they do display information about the progress they are making on solving the problem (e.g. the performance of the best solution found at a given point in time). If potential issues were detected during the presolve checks (see presolve_check()) and the problem is being forcibly solved (i.e. with force = TRUE), then it is also worth checking for any warnings displayed by the solver to see if these potential issues are actually causing issues (e.g. Gurobi can display warnings that include "Warning: Model contains large matrix coefficient range" and "Warning: Model contains large rhs").

The object returned from this function depends on the argument to a. If the argument to a is an '>OptimizationProblem object, then the solution is returned as a logical vector showing the status of each planning unit in each zone. However, in most cases, the argument to a is an '>ConservationProblem object, and so the type of object returned depends on the number of solutions generated and the type data used to represent the planning units:

numeric

vector containing the solution. Here, Each element corresponds to a different planning unit. If multiple solutions are generated, then the solution is returned as a list of numeric vectors.

matrix

containing numeric values for the solution. Here, rows correspond to different planning units, and fields (columns) correspond to different management zones. If multiple solutions are generated, then the solution is returned as a list of matrix objects.

'>Raster

object containing the solution in pixel values. If the argument to x contains a single management zone, then a RasterLayer object will be returned. Otherwise, if the argument to x contains multiple zones, then a '>RasterStack object will be returned containing a different layer for each management zone. If multiple solutions are generated, then the solution is returned as a list of Raster objects.

'>Spatial, sf::sf(), or data.frame

containing the solution in fields (columns). Here, each row corresponds to a different planning unit. If the argument to x contains a single zone, the fields containing solutions are named "solution_XXX" where "XXX" corresponds to the solution number. If the argument to x contains multiple zones, the fields containing solutions are named "solution_XXX_YYY" where "XXX" corresponds to the solution and "YYY" is the name of the management zone.

After solving problems that contain multiple zones, it may be useful to use the category_layer() or category_vector() function to reformat the output.

See Also

feature_representation(), problem(), solvers, category_layer(), presolve_check().

Examples

Run this code
# NOT RUN {
# set seed for reproducibility
set.seed(500)

# load data
data(sim_pu_raster, sim_pu_polygons, sim_pu_sf, sim_features,
     sim_pu_zones_stack, sim_pu_zones_sf, sim_features_zones)

# build minimal conservation problem with raster data
p1 <- problem(sim_pu_raster, sim_features) %>%
      add_min_set_objective() %>%
      add_relative_targets(0.1) %>%
      add_binary_decisions()
# }
# NOT RUN {
# solve the problem
s1 <- solve(p1)

# print solution
print(s1)

# print attributes describing the optimization process and the solution
print(attr(s1, "objective"))
print(attr(s1, "runtime"))
print(attr(s1, "status"))

# calculate feature representation in the solution
r1 <- feature_representation(p1, s1)
print(r1)

# plot solution
plot(s1, main = "solution", axes = FALSE, box = FALSE)
# }
# NOT RUN {
# build minimal conservation problem with polygon (Spatial) data
p2 <- problem(sim_pu_polygons, sim_features, cost_column = "cost") %>%
      add_min_set_objective() %>%
      add_relative_targets(0.1) %>%
      add_binary_decisions()
# }
# NOT RUN {
# solve the problem
s2 <- solve(p2)

# print first six rows of the attribute table
print(head(s2))

# calculate feature representation in the solution
r2 <- feature_representation(p2, s2[, "solution_1"])
print(r2)

# plot solution
spplot(s2, zcol = "solution_1", main = "solution", axes = FALSE, box = FALSE)
# }
# NOT RUN {
# build minimal conservation problem with polygon (sf) data
p3 <- problem(sim_pu_sf, sim_features, cost_column = "cost") %>%
      add_min_set_objective() %>%
      add_relative_targets(0.1) %>%
      add_binary_decisions()
# }
# NOT RUN {
# solve the problem
s3 <- solve(p3)

# print first six rows of the attribute table
print(head(s3))

# calculate feature representation in the solution
r3 <- feature_representation(p3, s3[, "solution_1"])
print(r3)

# plot solution
plot(s3[, "solution_1"])
# }
# NOT RUN {
# build multi-zone conservation problem with raster data
p4 <- problem(sim_pu_zones_stack, sim_features_zones) %>%
      add_min_set_objective() %>%
      add_relative_targets(matrix(runif(15, 0.1, 0.2), nrow = 5,
                                  ncol = 3)) %>%
      add_binary_decisions()
# }
# NOT RUN {
# solve the problem
s4 <- solve(p4)

# print solution
print(s4)

# calculate feature representation in the solution
r4 <- feature_representation(p4, s4)
print(r4)

# plot solution
plot(category_layer(s4), main = "solution", axes = FALSE, box = FALSE)
# }
# NOT RUN {
# build multi-zone conservation problem with polygon (sf) data
p5 <- problem(sim_pu_zones_sf, sim_features_zones,
              cost_column = c("cost_1", "cost_2", "cost_3")) %>%
      add_min_set_objective() %>%
      add_relative_targets(matrix(runif(15, 0.1, 0.2), nrow = 5,
                                  ncol = 3)) %>%
      add_binary_decisions()
# }
# NOT RUN {
# solve the problem
s5 <- solve(p5)

# print first six rows of the attribute table
print(head(s5))

# calculate feature representation in the solution
r5 <- feature_representation(p5, s5[, c("solution_1_zone_1",
                                        "solution_1_zone_2",
                                        "solution_1_zone_3")])
print(r5)

# create new column representing the zone id that each planning unit
# was allocated to in the solution
s5$solution <- category_vector(s5[, c("solution_1_zone_1",
                                      "solution_1_zone_2",
                                      "solution_1_zone_3")])
s5$solution <- factor(s5$solution)

# plot solution
plot(s5[, "solution"])
# }

Run the code above in your browser using DataLab