prioritizr / prioritizr
Showing 49 of 290 files from the diff.
Other files ignored by Codecov
man/objectives.Rd has changed.
R/objectives.R has changed.
Makefile has changed.
docs/index.html has changed.
docs/pkgdown.yml has changed.
man/solve.Rd has changed.
.Rbuildignore has changed.
R/portfolios.R has changed.
man/rij_matrix.Rd has changed.
tests/testthat.R has changed.
man/targets.Rd has changed.
docs/404.html has changed.
man/problem.Rd has changed.
docs/authors.html has changed.
README.md has changed.
NEWS.md has changed.
R/constraints.R has changed.
R/RcppExports.R has changed.
DESCRIPTION has changed.
man/portfolios.Rd has changed.
cran-comments.md has changed.

@@ -94,6 +94,7 @@
Loading
94 94
#' @family constraints
95 95
#'
96 96
#' @examples
97 +
#' \dontrun{
97 98
#' # set seed for reproducibility
98 99
#' set.seed(500)
99 100
#'
@@ -119,7 +120,7 @@
Loading
119 120
#' # create problem with added locked in constraints using spatial polygon data
120 121
#' locked_in <- sim_pu_polygons[sim_pu_polygons$locked_in == 1, ]
121 122
#' p5 <- p1 %>% add_locked_in_constraints(locked_in)
122 -
#' \dontrun{
123 +
#'
123 124
#' # solve problems
124 125
#' s1 <- solve(p1)
125 126
#' s2 <- solve(p2)
@@ -146,7 +147,6 @@
Loading
146 147
#'
147 148
#' # reset plot
148 149
#' par(mfrow = c(1, 1))
149 -
#' }
150 150
#'
151 151
#' # create minimal multi-zone problem with spatial data
152 152
#' p6 <- problem(sim_pu_zones_polygons, sim_features_zones,
@@ -163,7 +163,7 @@
Loading
163 163
#' locked_matrix <- as.matrix(locked_matrix)
164 164
#'
165 165
#' p7 <- p6 %>% add_locked_in_constraints(locked_matrix)
166 -
#' \dontrun{
166 +
#'
167 167
#' # solve problem
168 168
#' s6 <- solve(p6)
169 169
#'
@@ -176,10 +176,10 @@
Loading
176 176
#'
177 177
#' # plot solution
178 178
#' spplot(s6, zcol = "solution", main = "solution", axes = FALSE, box = FALSE)
179 -
#' }
179 +
#'
180 180
#' # create multi-zone problem with locked in constraints using field names
181 181
#' p8 <- p6 %>% add_locked_in_constraints(c("locked_1", "locked_2", "locked_3"))
182 -
#' \dontrun{
182 +
#'
183 183
#' # solve problem
184 184
#' s8 <- solve(p8)
185 185
#'
@@ -193,7 +193,7 @@
Loading
193 193
#'
194 194
#' # plot solution
195 195
#' spplot(s8, zcol = "solution", main = "solution", axes = FALSE, box = FALSE)
196 -
#' }
196 +
#'
197 197
#' # create multi-zone problem with raster planning units
198 198
#' p9 <- problem(sim_pu_zones_stack, sim_features_zones) %>%
199 199
#'       add_min_set_objective() %>%
@@ -210,13 +210,10 @@
Loading
210 210
#' locked_in_stack[[3]][3] <- 1
211 211
#'
212 212
#' # plot locked in stack
213 -
#' \dontrun{
214 213
#' plot(locked_in_stack)
215 -
#' }
216 214
#' # add locked in raster units to problem
217 215
#' p9 <- p9 %>% add_locked_in_constraints(locked_in_stack)
218 216
#'
219 -
#' \dontrun{
220 217
#' # solve problem
221 218
#' s9 <- solve(p9)
222 219
#'

@@ -135,6 +135,7 @@
Loading
135 135
#' *Journal of Applied Ecology*, 56: 913--922.
136 136
#'
137 137
#' @examples
138 +
#' \dontrun{
138 139
#' # load data
139 140
#' data(sim_pu_raster, sim_pu_zones_stack, sim_features, sim_features_zones)
140 141
#'
@@ -166,7 +167,7 @@
Loading
166 167
#'   m
167 168
#' })
168 169
#' p4 <- p1 %>% add_feature_contiguity_constraints(data = cm4)
169 -
#' \dontrun{
170 +
#'
170 171
#' # solve problems
171 172
#' s1 <- stack(solve(p1), solve(p2), solve(p3), solve(p4))
172 173
#'
@@ -175,7 +176,7 @@
Loading
175 176
#'      main = c("basic solution", "contiguity constraints",
176 177
#'               "feature contiguity constraints",
177 178
#'               "feature contiguity constraints with data"))
178 -
#' }
179 +
#'
179 180
#' # create minimal problem with multiple zones, and limit the solver to
180 181
#' # 30 seconds to obtain solutions in a feasible period of time
181 182
#' p5 <- problem(sim_pu_zones_stack, sim_features_zones) %>%
@@ -207,7 +208,7 @@
Loading
207 208
#'   matrix(ifelse(i == 1, 1, 0), ncol = 3, nrow = 3))
208 209
#' print(zm8)
209 210
#' p8 <- p5 %>% add_feature_contiguity_constraints(zm8)
210 -
#' \dontrun{
211 +
#'
211 212
#' # solve problems
212 213
#' s2 <- lapply(list(p5, p6, p7, p8), solve)
213 214
#' s2 <- stack(lapply(s2, category_layer))

@@ -107,6 +107,7 @@
Loading
107 107
#' @seealso [problem()], [solve()], <https://www.gurobi.com/documentation/9.5/refman/numerics_gurobi_guidelines.html>.
108 108
#'
109 109
#' @examples
110 +
#' \dontrun{
110 111
#' # set seed for reproducibility
111 112
#' set.seed(500)
112 113
#'
@@ -154,7 +155,7 @@
Loading
154 155
#' # note that a warning is thrown which suggests that we might encounter
155 156
#' # some numerical instability issues when trying to solve the problem
156 157
#' print(presolve_check(p3))
157 -
#' \dontrun{
158 +
#'
158 159
#' # let's forcibly solve the problem using Gurobi and tell it to
159 160
#' # be extra careful about numerical instability problems
160 161
#' s3 <- p3 %>%

@@ -60,7 +60,6 @@
Loading
60 60
extern SEXP _prioritizr_rcpp_predefined_optimization_problem(SEXP);
61 61
extern SEXP _prioritizr_rcpp_set_optimization_problem_shuffled(SEXP);
62 62
extern SEXP _prioritizr_rcpp_sp_to_polyset(SEXP, SEXP, SEXP);
63 -
extern SEXP _prioritizr_rcpp_str_tree_to_sparse_matrix(SEXP);
64 63
extern SEXP _prioritizr_rcpp_summarize_exactextractr(SEXP, SEXP, SEXP, SEXP);
65 64
66 65
static const R_CallMethodDef CallEntries[] = {
@@ -116,7 +115,6 @@
Loading
116 115
    {"_prioritizr_rcpp_predefined_optimization_problem",                   (DL_FUNC) &_prioritizr_rcpp_predefined_optimization_problem,                   1},
117 116
    {"_prioritizr_rcpp_set_optimization_problem_shuffled",                 (DL_FUNC) &_prioritizr_rcpp_set_optimization_problem_shuffled,                 1},
118 117
    {"_prioritizr_rcpp_sp_to_polyset",                                     (DL_FUNC) &_prioritizr_rcpp_sp_to_polyset,                                     3},
119 -
    {"_prioritizr_rcpp_str_tree_to_sparse_matrix",                         (DL_FUNC) &_prioritizr_rcpp_str_tree_to_sparse_matrix,                         1},
120 118
    {"_prioritizr_rcpp_summarize_exactextractr",                           (DL_FUNC) &_prioritizr_rcpp_summarize_exactextractr,                           4},
121 119
    {NULL, NULL, 0}
122 120
};

@@ -64,6 +64,7 @@
Loading
64 64
#' @inherit add_min_set_objective return
65 65
#'
66 66
#' @examples
67 +
#' \dontrun{
67 68
#' # load data
68 69
#' data(sim_pu_raster, sim_pu_zones_stack, sim_features, sim_features_zones)
69 70
#'
@@ -73,13 +74,12 @@
Loading
73 74
#'       add_relative_targets(0.1) %>%
74 75
#'       add_binary_decisions() %>%
75 76
#'       add_default_solver(verbose = FALSE)
76 -
#' \dontrun{
77 +
#'
77 78
#' # solve problem
78 79
#' s1 <- solve(p1)
79 80
#'
80 81
#' # plot solution
81 82
#' plot(s1, main = "solution", axes = FALSE, box = FALSE)
82 -
#' }
83 83
#'
84 84
#' # create multi-zone problem with minimum largest shortfall objective,
85 85
#' # with 10% representation targets for each feature, and set
@@ -90,13 +90,13 @@
Loading
90 90
#'       add_relative_targets(matrix(0.1, ncol = 3, nrow = 5)) %>%
91 91
#'       add_binary_decisions() %>%
92 92
#'       add_default_solver(verbose = FALSE)
93 -
#' \dontrun{
93 +
#'
94 94
#' # solve problem
95 95
#' s2 <- solve(p2)
96 96
#'
97 97
#' # plot solution
98 98
#' plot(category_layer(s2), main = "solution", axes = FALSE, box = FALSE)
99 -
#' }
99 +
#'
100 100
#' # create multi-zone problem with minimum largest shortfall objective,
101 101
#' # with 10% representation targets for each feature, and set
102 102
#' # separate budgets of 1800 for each management zone
@@ -105,7 +105,7 @@
Loading
105 105
#'       add_relative_targets(matrix(0.1, ncol = 3, nrow = 5)) %>%
106 106
#'       add_binary_decisions() %>%
107 107
#'       add_default_solver(verbose = FALSE)
108 -
#' \dontrun{
108 +
#'
109 109
#' # solve problem
110 110
#' s3 <- solve(p3)
111 111
#'

@@ -47,6 +47,7 @@
Loading
47 47
#' @family constraints
48 48
#'
49 49
#' @examples
50 +
#' \dontrun{
50 51
#' # set seed for reproducibility
51 52
#' set.seed(500)
52 53
#'
@@ -69,7 +70,7 @@
Loading
69 70
#'                           status = 1)
70 71
#'
71 72
#' p3 <- p1 %>% add_manual_locked_constraints(locked_data)
72 -
#' \dontrun{
73 +
#'
73 74
#' # solve problems
74 75
#' s1 <- solve(p1)
75 76
#' s2 <- solve(p2)
@@ -85,7 +86,7 @@
Loading
85 86
#'
86 87
#' plot(s3, main = "add_manual_constraints")
87 88
#' plot(s3[s3$solution_1 == 1, ], col = "darkgreen", add = TRUE)
88 -
#' }
89 +
#'
89 90
#' # create minimal problem with multiple zones
90 91
#' p4 <- problem(sim_pu_zones_polygons, sim_features_zones,
91 92
#'               c("cost_1", "cost_2", "cost_3")) %>%
@@ -109,7 +110,7 @@
Loading
109 110
#'
110 111
#' # create problem with added constraints
111 112
#' p5 <- p4 %>% add_manual_locked_constraints(locked_data2)
112 -
#' \dontrun{
113 +
#'
113 114
#' # solve problem
114 115
#' s4 <- solve(p4)
115 116
#' s5 <- solve(p5)

@@ -60,6 +60,7 @@
Loading
60 60
#' @family summaries
61 61
#'
62 62
#' @examples
63 +
#' \dontrun{
63 64
#' # set seed for reproducibility
64 65
#' set.seed(500)
65 66
#'
@@ -109,7 +110,6 @@
Loading
109 110
#'     sum(pu$spp2 * s1$s, na.rm = TRUE) / sum(pu$spp2, na.rm = TRUE)),
110 111
#'   c("spp1", "spp2")))
111 112
#'
112 -
#' \dontrun{
113 113
#' # solve the problem using an exact algorithm solver
114 114
#' s1_2 <- solve(p1)
115 115
#' print(s1_2)
@@ -138,14 +138,14 @@
Loading
138 138
#'
139 139
#' # plot solution
140 140
#' plot(s2, main = "solution", axes = FALSE, box = FALSE)
141 -
#' }
141 +
#'
142 142
#' # build minimal conservation problem with polygon (Spatial) data
143 143
#' p3 <- problem(sim_pu_polygons, sim_features, cost_column = "cost") %>%
144 144
#'       add_min_set_objective() %>%
145 145
#'       add_relative_targets(0.1) %>%
146 146
#'       add_binary_decisions() %>%
147 147
#'       add_default_solver(verbose = FALSE)
148 -
#' \dontrun{
148 +
#'
149 149
#' # solve the problem
150 150
#' s3 <- solve(p3)
151 151
#'
@@ -158,7 +158,7 @@
Loading
158 158
#'
159 159
#' # plot solution
160 160
#' spplot(s3, zcol = "solution_1", main = "solution", axes = FALSE, box = FALSE)
161 -
#' }
161 +
#'
162 162
#' # build multi-zone conservation problem with raster data
163 163
#' p4 <- problem(sim_pu_zones_stack, sim_features_zones) %>%
164 164
#'       add_min_set_objective() %>%
@@ -166,7 +166,7 @@
Loading
166 166
#'                                   ncol = 3)) %>%
167 167
#'       add_binary_decisions() %>%
168 168
#'       add_default_solver(verbose = FALSE)
169 -
#' \dontrun{
169 +
#'
170 170
#' # solve the problem
171 171
#' s4 <- solve(p4)
172 172
#'
@@ -179,7 +179,7 @@
Loading
179 179
#'
180 180
#' # plot solution
181 181
#' plot(category_layer(s4), main = "solution", axes = FALSE, box = FALSE)
182 -
#' }
182 +
#'
183 183
#'
184 184
#' # build multi-zone conservation problem with polygon (sf) data
185 185
#' p5 <- problem(sim_pu_zones_sf, sim_features_zones,
@@ -189,7 +189,7 @@
Loading
189 189
#'                                   ncol = 3)) %>%
190 190
#'       add_binary_decisions() %>%
191 191
#'       add_default_solver(verbose = FALSE)
192 -
#' \dontrun{
192 +
#'
193 193
#' # solve the problem
194 194
#' s5 <- solve(p5)
195 195
#'

@@ -21,6 +21,7 @@
Loading
21 21
#' @family decisions
22 22
#'
23 23
#' @examples
24 +
#' \dontrun{
24 25
#' # set seed for reproducibility
25 26
#' set.seed(500)
26 27
#'
@@ -33,13 +34,13 @@
Loading
33 34
#'       add_relative_targets(0.1) %>%
34 35
#'       add_proportion_decisions() %>%
35 36
#'       add_default_solver(verbose = FALSE)
36 -
#' \dontrun{
37 +
#'
37 38
#' # solve problem
38 39
#' s1 <- solve(p1)
39 40
#'
40 41
#' # plot solutions
41 42
#' plot(s1, main = "solution")
42 -
#' }
43 +
#'
43 44
#' # build multi-zone conservation problem with proportion decisions
44 45
#' p2 <- problem(sim_pu_zones_stack, sim_features_zones) %>%
45 46
#'       add_min_set_objective() %>%
@@ -47,7 +48,7 @@
Loading
47 48
#'                                   ncol = 3)) %>%
48 49
#'       add_proportion_decisions() %>%
49 50
#'       add_default_solver(verbose = FALSE)
50 -
#' \dontrun{
51 +
#'
51 52
#' # solve the problem
52 53
#' s2 <- solve(p2)
53 54
#'

@@ -5,7 +5,6 @@
Loading
5 5
  arma::sp_mat &rij, Rcpp::NumericVector &targets, double portfolio_size,
6 6
  arma::sp_mat &out) {
7 7
  // initialize variables
8 -
  const std::size_t n_elem = rij.n_nonzero;
9 8
  const double n_pu = static_cast<double>(rij.n_cols);
10 9
  const double n_f = static_cast<double>(rij.n_rows);
11 10
  const double mult = n_pu / (n_pu - 1.0);

@@ -24,6 +24,7 @@
Loading
24 24
#' @return `numeric` values.
25 25
#'
26 26
#' @examples
27 +
#' \dontrun{
27 28
#' # create series of x-values
28 29
#' x <- seq(0, 1000)
29 30
#'
@@ -32,12 +33,10 @@
Loading
32 33
#' y <- loglinear_interpolation(x, 200, 100, 900, 15)
33 34
#'
34 35
#' # plot the interpolated values
35 -
#' \dontrun{
36 36
#' plot(y ~ x)
37 37
#'
38 38
#' # add the reference points to the plot (shown in red)
39 39
#' points(x = c(200, 900), y = c(100, 15), pch = 18, col = "red", cex = 2)
40 -
#' }
41 40
#'
42 41
#' # this function can also be used to calculate representation targets
43 42
#' # following Rodrigues et al. (2014). For example, let's say that
@@ -72,8 +71,8 @@
Loading
72 71
#'   xlab = "Range size km^2" , ylab = "Representation target (%)",
73 72
#'   type = "l", xaxt = "n")
74 73
#' axis(1, pretty(log10(spp_range_size_km2)),
75 -
#'      10^pretty(log10(spp_range_size_km2))) 
76 -
#'
74 +
#'      10^pretty(log10(spp_range_size_km2)))
75 +
#' }
77 76
#' @export
78 77
loglinear_interpolation <- function(x, coordinate_one_x, coordinate_one_y,
79 78
                                  coordinate_two_x, coordinate_two_y) {

@@ -61,6 +61,7 @@
Loading
61 61
#' benefit functions. *Conservation Biology*, 19: 2009--2014.
62 62
#'
63 63
#' @examples
64 +
#' \dontrun{
64 65
#' # load data
65 66
#' data(sim_pu_raster, sim_pu_zones_stack, sim_features, sim_features_zones)
66 67
#'
@@ -70,13 +71,12 @@
Loading
70 71
#'       add_relative_targets(0.1) %>%
71 72
#'       add_binary_decisions() %>%
72 73
#'       add_default_solver(verbose = FALSE)
73 -
#' \dontrun{
74 +
#'
74 75
#' # solve problem
75 76
#' s1 <- solve(p1)
76 77
#'
77 78
#' # plot solution
78 79
#' plot(s1, main = "solution", axes = FALSE, box = FALSE)
79 -
#' }
80 80
#'
81 81
#' # create multi-zone problem with minimum shortfall objective,
82 82
#' # with 10% representation targets for each feature, and set
@@ -87,13 +87,13 @@
Loading
87 87
#'       add_relative_targets(matrix(0.1, ncol = 3, nrow = 5)) %>%
88 88
#'       add_binary_decisions() %>%
89 89
#'       add_default_solver(verbose = FALSE)
90 -
#' \dontrun{
90 +
#'
91 91
#' # solve problem
92 92
#' s2 <- solve(p2)
93 93
#'
94 94
#' # plot solution
95 95
#' plot(category_layer(s2), main = "solution", axes = FALSE, box = FALSE)
96 -
#' }
96 +
#'
97 97
#' # create multi-zone problem with minimum shortfall objective,
98 98
#' # with 10% representation targets for each feature, and set
99 99
#' # separate budgets for each management zone
@@ -102,7 +102,7 @@
Loading
102 102
#'       add_relative_targets(matrix(0.1, ncol = 3, nrow = 5)) %>%
103 103
#'       add_binary_decisions() %>%
104 104
#'       add_default_solver(verbose = FALSE)
105 -
#' \dontrun{
105 +
#'
106 106
#' # solve problem
107 107
#' s3 <- solve(p3)
108 108
#'

@@ -7,7 +7,6 @@
Loading
7 7
8 8
  // initialization
9 9
  std::size_t n_z = solution.ncol();
10 -
  std::size_t n_pu = solution.nrow();
11 10
  double out = 0.0;
12 11
  arma::sp_mat curr_matrix;
13 12
@@ -16,22 +15,10 @@
Loading
16 15
  import_connectivity_matrix_list(data, matrices, true);
17 16
18 17
  // extract penalty data from matrices
19 -
  for (std::size_t z1 = 0; z1 < solution.ncol(); ++z1) {
20 -
    for (std::size_t z2 = 0; z2 < solution.ncol(); ++z2) {
18 +
  for (std::size_t z1 = 0; z1 < n_z; ++z1) {
19 +
    for (std::size_t z2 = 0; z2 < n_z; ++z2) {
21 20
      // extract connectivity matrix
22 21
      curr_matrix = matrices[z1][z2];
23 -
24 -
      // // add sum of connectivity values along matrix diagonal
25 -
      // if (z1 == z2) {
26 -
      //   for (std::size_t i = 0; i < n_pu; ++i) {
27 -
      //     out += (curr_matrix(i, i) * solution(i, z1));
28 -
      //   }
29 -
      // }
30 -
      //
31 -
      // // force diagonal to zero for subequent calculations so that
32 -
      // // the matrix diagonal values are not counted twice
33 -
      // curr_matrix.diag().zeros();
34 -
35 22
      // add sum of connectivity values between pairs of planning
36 23
      // units where one unit is selected and the other one isn't
37 24
      for (arma::sp_mat::const_iterator it = curr_matrix.begin();

@@ -31,6 +31,7 @@
Loading
31 31
#' @rdname proximity_matrix
32 32
#'
33 33
#' @examples
34 +
#' \dontrun{
34 35
#' # load data
35 36
#' data(sim_pu_raster, sim_pu_sf, sim_pu_lines, sim_pu_points)
36 37
#'
@@ -63,7 +64,6 @@
Loading
63 64
#' cm_pts <- proximity_matrix(pts, distance = 2)
64 65
#'
65 66
#' # plot data and the proximity matrices
66 -
#' \dontrun{
67 67
#' par(mfrow = c(4,2))
68 68
#'
69 69
#' ## plot raster and proximity matrix

@@ -34,6 +34,7 @@
Loading
34 34
#' @family portfolios
35 35
#'
36 36
#' @examples
37 +
#' \dontrun{
37 38
#' # set seed for reproducibility
38 39
#' set.seed(500)
39 40
#'
@@ -47,13 +48,12 @@
Loading
47 48
#'       add_shuffle_portfolio(10, remove_duplicates = FALSE) %>%
48 49
#'       add_default_solver(gap = 0.2, verbose = FALSE)
49 50
#'
50 -
#' \dontrun{
51 51
#' # solve problem and generate 10 solutions within 20% of optimality
52 52
#' s1 <- solve(p1)
53 53
#'
54 54
#' # plot solutions in portfolio
55 55
#' plot(stack(s1), axes = FALSE, box = FALSE)
56 -
#' }
56 +
#'
57 57
#' # build multi-zone conservation problem with shuffle portfolio
58 58
#' p2 <- problem(sim_pu_zones_stack, sim_features_zones) %>%
59 59
#'       add_min_set_objective() %>%
@@ -63,7 +63,6 @@
Loading
63 63
#'       add_shuffle_portfolio(10, remove_duplicates = FALSE) %>%
64 64
#'       add_default_solver(gap = 0.2, verbose = FALSE)
65 65
#'
66 -
#' \dontrun{
67 66
#' # solve the problem
68 67
#' s2 <- solve(p2)
69 68
#'
@@ -71,8 +70,8 @@
Loading
71 70
#' str(s2, max.level = 1)
72 71
#'
73 72
#' # plot solutions in portfolio
74 -
#' plot(stack(lapply(s2, category_layer)), main = "solution", axes = FALSE,
75 -
#'      box = FALSE)
73 +
#' plot(stack(lapply(s2, category_layer)),
74 +
#'      main = "solution", axes = FALSE, box = FALSE)
76 75
#' }
77 76
#' @name add_shuffle_portfolio
78 77
NULL

@@ -63,7 +63,8 @@
Loading
63 63
#' @family penalties
64 64
#'
65 65
#' @examples
66 -
#' # load ape package
66 +
#' \dontrun{
67 +
#' # load package
67 68
#' require(ape)
68 69
#'
69 70
#' # load data
@@ -92,20 +93,18 @@
Loading
92 93
#' # denote higher extinction risk)
93 94
#' w3 <- c(0, 0, 0, 100, 200)
94 95
#' p3 <- p1 %>% add_feature_weights(w3)
95 -
#' \dontrun{
96 +
#'
96 97
#' # solve problems
97 98
#' s1 <- stack(solve(p1), solve(p2), solve(p3))
98 99
#'
99 100
#' # plot solutions
100 101
#' plot(s1, main = c("equal weights", "rarity weights", "manual weights"),
101 102
#'      axes = FALSE, box = FALSE)
102 -
#' }
103 103
#'
104 104
#' # plot the example phylogeny
105 -
#' \dontrun{
106 105
#' par(mfrow = c(1, 1))
107 106
#' plot(sim_phylogeny, main = "simulated phylogeny")
108 -
#' }
107 +
#'
109 108
#' # create problem with a maximum phylogenetic diversity objective,
110 109
#' # where each feature needs 10% of its distribution to be secured for
111 110
#' # it to be adequately conserved and a total budget of 1900
@@ -114,7 +113,7 @@
Loading
114 113
#'       add_relative_targets(0.1) %>%
115 114
#'       add_binary_decisions() %>%
116 115
#'       add_default_solver(verbose = FALSE)
117 -
#' \dontrun{
116 +
#'
118 117
#' # solve problem
119 118
#' s4 <- solve(p4)
120 119
#'
@@ -129,7 +128,7 @@
Loading
129 128
#' plot(sim_phylogeny, main = "represented features",
130 129
#'      tip.color = replace(rep("black", nlayers(sim_features)),
131 130
#'                          which(r4$met), "red"))
132 -
#' }
131 +
#'
133 132
#' # we can see here that the third feature ("layer.3", i.e.,
134 133
#' # sim_features[[3]]) is not represented in the solution. Let us pretend
135 134
#' # that it is absolutely critical this feature is adequately conserved
@@ -145,7 +144,7 @@
Loading
145 144
#' # the branch lengths in the phylogeny so solutions that represent this
146 145
#' # feature be much closer to optimality
147 146
#' print(sim_phylogeny$edge.length)
148 -
#' \dontrun{
147 +
#'
149 148
#' # create problem with high weighting for the third feature and solve it
150 149
#' s5 <- p4 %>% add_feature_weights(w5) %>% solve()
151 150
#'
@@ -164,7 +163,7 @@
Loading
164 163
#' plot(sim_phylogeny, main = "represented features",
165 164
#'      tip.color = replace(rep("black", nlayers(sim_features)),
166 165
#'                          which(r5$met), "red"))
167 -
#' }
166 +
#'
168 167
#' # create multi-zone problem with maximum features objective,
169 168
#' # with 10% representation targets for each feature, and set
170 169
#' # a budget such that the total maximum expenditure in all zones
@@ -185,7 +184,7 @@
Loading
185 184
#'
186 185
#' # create problem with weights
187 186
#' p7 <- p6 %>% add_feature_weights(w7)
188 -
#' \dontrun{
187 +
#'
189 188
#' # solve problems
190 189
#' s6 <- solve(p6)
191 190
#' s7 <- solve(p7)
@@ -193,7 +192,7 @@
Loading
193 192
#' # plot solutions
194 193
#' plot(stack(category_layer(s6), category_layer(s7)),
195 194
#'      main = c("equal weights", "manual weights"), axes = FALSE, box = FALSE)
196 -
#' }
195 +
#'
197 196
#' # create minimal problem to show the correct method for setting
198 197
#' # weights for problems with manual targets
199 198
#' p8 <- problem(sim_pu_raster, sim_features) %>%
@@ -204,7 +203,7 @@
Loading
204 203
#'       add_feature_weights(matrix(c(1, 200), ncol = 1)) %>%
205 204
#'       add_binary_decisions() %>%
206 205
#'       add_default_solver(verbose = FALSE)
207 -
#' \dontrun{
206 +
#'
208 207
#' # solve problem
209 208
#' s8 <- solve(p8)
210 209
#'

@@ -27,6 +27,7 @@
Loading
27 27
#' @family decisions
28 28
#'
29 29
#' @examples
30 +
#' \dontrun{
30 31
#' # set seed for reproducibility
31 32
#' set.seed(500)
32 33
#'
@@ -39,13 +40,13 @@
Loading
39 40
#'       add_relative_targets(0.1) %>%
40 41
#'       add_binary_decisions() %>%
41 42
#'       add_default_solver(verbose = FALSE)
42 -
#' \dontrun{
43 +
#'
43 44
#' # solve problem
44 45
#' s1 <- solve(p1)
45 46
#'
46 47
#' # plot solution
47 48
#' plot(s1, main = "solution")
48 -
#' }
49 +
#'
49 50
#' # build multi-zone conservation problem with binary decisions
50 51
#' p2 <- problem(sim_pu_zones_stack, sim_features_zones) %>%
51 52
#'       add_min_set_objective() %>%
@@ -53,7 +54,7 @@
Loading
53 54
#'                                   ncol = 3)) %>%
54 55
#'       add_binary_decisions() %>%
55 56
#'       add_default_solver(verbose = FALSE)
56 -
#' \dontrun{
57 +
#'
57 58
#' # solve the problem
58 59
#' s2 <- solve(p2)
59 60
#'

@@ -30,6 +30,7 @@
Loading
30 30
#' @family constraints
31 31
#'
32 32
#' @examples
33 +
#' \dontrun{
33 34
#' # set seed for reproducibility
34 35
#' set.seed(500)
35 36
#'
@@ -55,7 +56,7 @@
Loading
55 56
#' # create problem with added locked out constraints using spatial polygon data
56 57
#' locked_out <- sim_pu_polygons[sim_pu_polygons$locked_out == 1, ]
57 58
#' p5 <- p1 %>% add_locked_out_constraints(locked_out)
58 -
#' \dontrun{
59 +
#'
59 60
#' # solve problems
60 61
#' s1 <- solve(p1)
61 62
#' s2 <- solve(p2)
@@ -82,7 +83,6 @@
Loading
82 83
#'
83 84
#' # reset plot
84 85
#' par(mfrow = c(1, 1))
85 -
#' }
86 86
#'
87 87
#' # create minimal multi-zone problem with spatial data
88 88
#' p6 <- problem(sim_pu_zones_polygons, sim_features_zones,
@@ -98,7 +98,7 @@
Loading
98 98
#' locked_matrix <- as.matrix(locked_matrix)
99 99
#'
100 100
#' p7 <- p6 %>% add_locked_out_constraints(locked_matrix)
101 -
#' \dontrun{
101 +
#'
102 102
#' # solve problem
103 103
#' s6 <- solve(p6)
104 104
#'
@@ -111,11 +111,11 @@
Loading
111 111
#'
112 112
#' # plot solution
113 113
#' spplot(s6, zcol = "solution", main = "solution", axes = FALSE, box = FALSE)
114 -
#' }
114 +
#'
115 115
#' # create multi-zone problem with locked out constraints using field names
116 116
#' p8 <- p6 %>% add_locked_out_constraints(c("locked_1", "locked_2",
117 117
#'                                           "locked_3"))
118 -
#' \dontrun{
118 +
#'
119 119
#' # solve problem
120 120
#' s8 <- solve(p8)
121 121
#'
@@ -129,7 +129,7 @@
Loading
129 129
#'
130 130
#' # plot solution
131 131
#' spplot(s8, zcol = "solution", main = "solution", axes = FALSE, box = FALSE)
132 -
#' }
132 +
#'
133 133
#' # create multi-zone problem with raster planning units
134 134
#' p9 <- problem(sim_pu_zones_stack, sim_features_zones) %>%
135 135
#'       add_min_set_objective() %>%
@@ -146,13 +146,12 @@
Loading
146 146
#' locked_out_stack[[3]][3] <- 1
147 147
#'
148 148
#' # plot locked out stack
149 -
#' \dontrun{
149 +
#'
150 150
#' plot(locked_out_stack)
151 -
#' }
151 +
#'
152 152
#' # add locked out raster units to problem
153 153
#' p9 <- p9 %>% add_locked_out_constraints(locked_out_stack)
154 154
#'
155 -
#' \dontrun{
156 155
#' # solve problem
157 156
#' s9 <- solve(p9)
158 157
#'

@@ -32,7 +32,8 @@
Loading
32 32
#'
33 33
#' # plot categorical raster and binary stack representation
34 34
#' \dontrun{
35 -
#' plot(stack(x, y), main = c("x[[1]]", "x[[2]]", "x[[3]]", "y"), nr = 1)
35 +
#' plot(stack(x, y), main = c("x[[1]]", "x[[2]]", "x[[3]]", "y"),
36 +
#'      nr = 1)
36 37
#' }
37 38
#' @export
38 39
category_layer <- function(x) {

@@ -20,6 +20,7 @@
Loading
20 20
#' @inherit marxan_boundary_data_to_matrix return
21 21
#'
22 22
#' @examples
23 +
#' \dontrun{
23 24
#' # create marxan connectivity data with four planning units and one zone,
24 25
#' # and symmetric connectivity values
25 26
#' bldf1 <- expand.grid(id1 = seq_len(4), id2 = seq_len(4))
@@ -36,9 +37,8 @@
Loading
36 37
#' print(m1)
37 38
#'
38 39
#' # visualize matrix
39 -
#' \dontrun{
40 40
#' image(m1)
41 -
#' }
41 +
#'
42 42
#' # create marxan connectivity data with four planning units and one zone,
43 43
#' # and asymmetric connectivity values
44 44
#' bldf2 <- expand.grid(id1 = seq_len(4), id2 = seq_len(4))
@@ -55,9 +55,8 @@
Loading
55 55
#' print(m2)
56 56
#'
57 57
#' # visualize matrix
58 -
#' \dontrun{
59 58
#' image(m2)
60 -
#' }
59 +
#'
61 60
#' # create marxan connectivity with three planning units and two zones,
62 61
#' # and symmetric connectivity values
63 62
#' bldf3 <- expand.grid(id1 = seq_len(3), id2 = seq_len(3),
@@ -75,6 +74,7 @@
Loading
75 74
#'
76 75
#' # print array
77 76
#' print(m3)
77 +
#' }
78 78
#' @export
79 79
marxan_connectivity_data_to_matrix <- function(x, data, symmetric = TRUE) {
80 80
  # assert that argument to data is valid

@@ -28,6 +28,7 @@
Loading
28 28
#' @family decisions
29 29
#'
30 30
#' @examples
31 +
#' \dontrun{
31 32
#' # set seed for reproducibility
32 33
#' set.seed(500)
33 34
#'
@@ -40,13 +41,13 @@
Loading
40 41
#'       add_relative_targets(0.1) %>%
41 42
#'       add_semicontinuous_decisions(0.5) %>%
42 43
#'       add_default_solver(verbose = FALSE)
43 -
#' \dontrun{
44 +
#'
44 45
#' # solve problem
45 46
#' s1 <- solve(p1)
46 47
#'
47 48
#' # plot solutions
48 49
#' plot(s1, main = "solution")
49 -
#' }
50 +
#'
50 51
#' # build multi-zone conservation problem with semi-continuous decisions
51 52
#' p2 <- problem(sim_pu_zones_stack, sim_features_zones) %>%
52 53
#'       add_min_set_objective() %>%
@@ -54,7 +55,7 @@
Loading
54 55
#'                                   ncol = 3)) %>%
55 56
#'       add_semicontinuous_decisions(0.5) %>%
56 57
#'       add_default_solver(verbose = FALSE)
57 -
#' \dontrun{
58 +
#'
58 59
#' # solve the problem
59 60
#' s2 <- solve(p2)
60 61
#'

@@ -72,6 +72,7 @@
Loading
72 72
#' @family targets
73 73
#'
74 74
#' @examples
75 +
#' \dontrun{
75 76
#' # set seed for reproducibility
76 77
#' set.seed(500)
77 78
#'
@@ -90,14 +91,13 @@
Loading
90 91
#' # create problem with varying targets for each feature
91 92
#' targets <- c(1, 2, 3, 2, 1)
92 93
#' p2 <- p %>% add_absolute_targets(targets)
93 -
#' \dontrun{
94 +
#'
94 95
#' # solve problem
95 96
#' s <- stack(solve(p1), solve(p2))
96 97
#'
97 98
#' # plot solution
98 99
#' plot(s, main = c("equal targets", "varying targets"), axes = FALSE,
99 100
#'      box = FALSE)
100 -
#' }
101 101
#'
102 102
#' # create a problem with multiple management zones
103 103
#' p3 <- problem(sim_pu_zones_stack, sim_features_zones) %>%
@@ -116,13 +116,11 @@
Loading
116 116
#' p4 <- p3 %>% add_absolute_targets(p4_targets)
117 117
#'
118 118
#' # solve problem
119 -
#' \dontrun{
120 -
#' # solve problem
121 119
#' s4 <- solve(p4)
122 120
#'
123 121
#' # plot solution (pixel values correspond to zone identifiers)
124 122
#' plot(category_layer(s4), main = c("equal targets"))
125 -
#' }
123 +
#'
126 124
#' # create a problem with targets that require a varying amount of each
127 125
#' # feature to be represented in each zone
128 126
#' p5_targets <- matrix(rpois(15, 1),
@@ -133,8 +131,7 @@
Loading
133 131
#' print(p5_targets)
134 132
#'
135 133
#' p5 <- p3 %>% add_absolute_targets(p4_targets)
136 -
#' # solve problem
137 -
#' \dontrun{
134 +
#'
138 135
#' # solve problem
139 136
#' s5 <- solve(p5)
140 137
#'

@@ -84,6 +84,7 @@
Loading
84 84
#' @family objectives
85 85
#'
86 86
#' @examples
87 +
#' \dontrun{
87 88
#' # load data
88 89
#' data(sim_pu_raster, sim_pu_zones_stack, sim_features, sim_features_zones)
89 90
#'
@@ -100,13 +101,12 @@
Loading
100 101
#'       add_max_cover_objective(500) %>%
101 102
#'       add_binary_decisions() %>%
102 103
#'       add_default_solver(verbose = FALSE)
103 -
#' \dontrun{
104 +
#'
104 105
#' # solve problem
105 106
#' s1 <- solve(p1)
106 107
#'
107 108
#' # plot solution
108 109
#' plot(s1, main = "solution", axes = FALSE, box = FALSE)
109 -
#' }
110 110
#'
111 111
#' # threshold the multi-zone feature data to generate binary biodiversity data
112 112
#' sim_binary_features_zones <- sim_features_zones
@@ -125,13 +125,12 @@
Loading
125 125
#'       add_max_cover_objective(800) %>%
126 126
#'       add_binary_decisions() %>%
127 127
#'       add_default_solver(verbose = FALSE)
128 -
#' \dontrun{
128 +
#'
129 129
#' # solve problem
130 130
#' s2 <- solve(p2)
131 131
#'
132 132
#' # plot solution
133 133
#' plot(category_layer(s2), main = "solution", axes = FALSE, box = FALSE)
134 -
#' }
135 134
#'
136 135
#' # create multi-zone problem with maximum utility objective that
137 136
#' # has separate budgets for each zone
@@ -139,7 +138,7 @@
Loading
139 138
#'       add_max_cover_objective(c(400, 400, 400)) %>%
140 139
#'       add_binary_decisions() %>%
141 140
#'       add_default_solver(verbose = FALSE)
142 -
#' \dontrun{
141 +
#'
143 142
#' # solve problem
144 143
#' s3 <- solve(p3)
145 144
#'

@@ -114,6 +114,7 @@
Loading
114 114
#' @family penalties
115 115
#'
116 116
#' @examples
117 +
#' \dontrun{
117 118
#' # set seed for reproducibility
118 119
#' set.seed(600)
119 120
#'
@@ -146,7 +147,6 @@
Loading
146 147
#' # print problem
147 148
#' print(p2)
148 149
#'
149 -
#' \dontrun{
150 150
#' # solve the two problems
151 151
#' s1 <- solve(p1)
152 152
#' s2 <- solve(p2)
@@ -163,12 +163,10 @@
Loading
163 163
#' # for real conservation planning exercises,
164 164
#' # it would be worth exploring a range of penalty values (e.g., ranging
165 165
#' # from 1 to 100 increments of 5) to explore the trade-offs
166 -
#' }
167 166
#'
168 167
#' # now, let's examine a conservation planning exercise involving multiple
169 168
#' # management zones
170 169
#'
171 -
#' \dontrun{
172 170
#' # create targets for each feature within each zone,
173 171
#' # these targets indicate that each zone needs to represent 10% of the
174 172
#' # spatial distribution of each feature

@@ -55,6 +55,7 @@
Loading
55 55
#'   added to it.
56 56
#'
57 57
#' @examples
58 +
#' \dontrun{
58 59
#' # set seed for reproducibility
59 60
#' set.seed(500)
60 61
#'
@@ -67,13 +68,12 @@
Loading
67 68
#'       add_relative_targets(0.1) %>%
68 69
#'       add_binary_decisions() %>%
69 70
#'       add_default_solver(verbose = FALSE)
70 -
#' \dontrun{
71 +
#'
71 72
#' # solve problem
72 73
#' s1 <- solve(p1)
73 74
#'
74 75
#' # plot solution
75 76
#' plot(s1, main = "solution", axes = FALSE, box = FALSE)
76 -
#' }
77 77
#'
78 78
#' # create multi-zone problem with minimum set objective
79 79
#' targets_matrix <- matrix(rpois(15, 1), nrow = 5, ncol = 3)
@@ -83,7 +83,7 @@
Loading
83 83
#'       add_absolute_targets(targets_matrix) %>%
84 84
#'       add_binary_decisions() %>%
85 85
#'       add_default_solver(verbose = FALSE)
86 -
#' \dontrun{
86 +
#'
87 87
#' # solve problem
88 88
#' s2 <- solve(p2)
89 89
#'

@@ -47,6 +47,7 @@
Loading
47 47
#' @family constraints
48 48
#'
49 49
#' @examples
50 +
#' \dontrun{
50 51
#' # set seed for reproducibility
51 52
#' set.seed(500)
52 53
#'
@@ -69,7 +70,7 @@
Loading
69 70
#'                           lower = 1, upper = 1)
70 71
#'
71 72
#' p3 <- p1 %>% add_manual_bounded_constraints(bounds_data)
72 -
#' \dontrun{
73 +
#'
73 74
#' # solve problems
74 75
#' s1 <- solve(p1)
75 76
#' s2 <- solve(p2)
@@ -85,7 +86,7 @@
Loading
85 86
#'
86 87
#' plot(s3, main = "add_bounds_constraints")
87 88
#' plot(s3[s3$solution_1 == 1, ], col = "darkgreen", add = TRUE)
88 -
#' }
89 +
#'
89 90
#' # create minimal problem with multiple zones
90 91
#' p4 <- problem(sim_pu_zones_polygons, sim_features_zones,
91 92
#'               c("cost_1", "cost_2", "cost_3")) %>%
@@ -110,7 +111,7 @@
Loading
110 111
#'
111 112
#' # create problem with added constraints
112 113
#' p5 <- p4 %>% add_manual_bounded_constraints(bounds_data2)
113 -
#' \dontrun{
114 +
#'
114 115
#' # solve problem
115 116
#' s4 <- solve(p4)
116 117
#' s5 <- solve(p5)

@@ -51,13 +51,14 @@
Loading
51 51
#' @rdname boundary_matrix
52 52
#'
53 53
#' @examples
54 +
#' \dontrun{
54 55
#' # load data
55 56
#' data(sim_pu_raster, sim_pu_polygons)
56 57
#'
57 58
#' # subset data to reduce processing time
58 59
#' r <- crop(sim_pu_raster, c(0, 0.3, 0, 0.3))
59 60
#' ply <- sim_pu_polygons[c(1:2, 10:12, 20:22), ]
60 -
#' ply2 <- st_as_sf(ply)
61 +
#' ply2 <- sf::st_as_sf(ply)
61 62
#'
62 63
#' # create boundary matrix using raster data
63 64
#' bm_raster <- boundary_matrix(r)
@@ -69,14 +70,12 @@
Loading
69 70
#' bm_ply2 <- boundary_matrix(ply2)
70 71
#'
71 72
#' # plot raster and boundary matrix
72 -
#' \dontrun{
73 73
#' par(mfrow = c(1, 2))
74 74
#' plot(r, main = "raster", axes = FALSE, box = FALSE)
75 75
#' plot(raster(as.matrix(bm_raster)), main = "boundary matrix",
76 76
#'      axes = FALSE, box = FALSE)
77 -
#' }
77 +
#'
78 78
#' # plot polygons and boundary matrices
79 -
#' \dontrun{
80 79
#' par(mfrow = c(1, 3))
81 80
#' plot(r, main = "polygons (Spatial)", axes = FALSE, box = FALSE)
82 81
#' plot(raster(as.matrix(bm_ply1)), main = "boundary matrix", axes = FALSE,

@@ -23,6 +23,7 @@
Loading
23 23
#' @family targets
24 24
#'
25 25
#' @examples
26 +
#' \dontrun{
26 27
#' # set seed for reproducibility
27 28
#' set.seed(500)
28 29
#'
@@ -41,14 +42,14 @@
Loading
41 42
#' # create problem with varying targets for each feature
42 43
#' targets <- c(0.1, 0.2, 0.3, 0.4, 0.5)
43 44
#' p2 <- p %>% add_relative_targets(targets)
44 -
#' \dontrun{
45 +
#'
45 46
#' # solve problem
46 47
#' s <- stack(solve(p1), solve(p2))
47 48
#'
48 49
#' # plot solution
49 50
#' plot(s, main = c("10% targets", "varying targets"), axes = FALSE,
50 51
#'      box = FALSE)
51 -
#' }
52 +
#'
52 53
#' # create a problem with multiple management zones
53 54
#' p3 <- problem(sim_pu_zones_stack, sim_features_zones) %>%
54 55
#'       add_min_set_objective() %>%
@@ -65,13 +66,11 @@
Loading
65 66
#' p4 <- p3 %>% add_relative_targets(p4_targets)
66 67
#'
67 68
#' # solve problem
68 -
#' \dontrun{
69 -
#' # solve problem
70 69
#' s4 <- solve(p4)
71 70
#'
72 71
#' # plot solution (pixel values correspond to zone identifiers)
73 72
#' plot(category_layer(s4), main = c("equal targets"))
74 -
#' }
73 +
#'
75 74
#' # create a problem with targets that require a varying amount of each
76 75
#' # feature to be represented in each zone
77 76
#' p5_targets <- matrix(runif(15, 0.01, 0.2), nrow = 5, ncol = 3,
@@ -80,8 +79,7 @@
Loading
80 79
#' print(p5_targets)
81 80
#'
82 81
#' p5 <- p3 %>% add_relative_targets(p4_targets)
83 -
#' # solve problem
84 -
#' \dontrun{
82 +
#'
85 83
#' # solve problem
86 84
#' s5 <- solve(p5)
87 85
#'

@@ -7,6 +7,7 @@
Loading
7 7
8 8
  // initialization
9 9
  std::size_t n_z = solution.ncol();
10 +
  std::size_t n_pu = solution.nrow();
10 11
  double out = 0.0;
11 12
  arma::sp_mat curr_matrix;
12 13
@@ -15,8 +16,8 @@
Loading
15 16
  import_connectivity_matrix_list(data, matrices, true);
16 17
17 18
  // extract penalty data from matrices
18 -
  for (std::size_t z1 = 0; z1 < solution.ncol(); ++z1) {
19 -
    for (std::size_t z2 = 0; z2 < solution.ncol(); ++z2) {
19 +
  for (std::size_t z1 = 0; z1 < n_z; ++z1) {
20 +
    for (std::size_t z2 = 0; z2 < n_z; ++z2) {
20 21
      // extract connectivity matrix
21 22
      curr_matrix = matrices[z1][z2];
22 23
@@ -29,7 +30,7 @@
Loading
29 30
      }
30 31
31 32
      // set matrix cells to zero for planning units not selected in solution
32 -
      for (std::size_t i = 0; i < solution.nrow(); ++i)  {
33 +
      for (std::size_t i = 0; i < n_pu; ++i)  {
33 34
        curr_matrix.row(i) *= solution(i, z1);
34 35
        curr_matrix.col(i) *= solution(i, z2);
35 36
      }

@@ -42,6 +42,7 @@
Loading
42 42
#' @aliases rij_matrix,Raster,Raster-method rij_matrix,Spatial,Raster-method rij_matrix,sf,Raster-method
43 43
#'
44 44
#' @examples
45 +
#' \dontrun{
45 46
#' # load data
46 47
#' data(sim_pu_raster, sim_pu_polygons, sim_pu_sf, sim_pu_zones_stack)
47 48
#'
@@ -60,6 +61,7 @@
Loading
60 61
#' # create rij matrix using raster stack planning units
61 62
#' rij_zones_raster <- rij_matrix(sim_pu_zones_stack, sim_features)
62 63
#' print(rij_zones_raster)
64 +
#' }
63 65
#' @export
64 66
methods::setGeneric("rij_matrix",
65 67
                    signature = methods::signature("x", "y"),

@@ -101,6 +101,7 @@
Loading
101 101
#' 105: 103--111.
102 102
#'
103 103
#' @examples
104 +
#' \dontrun{
104 105
#' # load ape package
105 106
#' require(ape)
106 107
#'
@@ -109,10 +110,9 @@
Loading
109 110
#'      sim_features_zones)
110 111
#'
111 112
#' # plot the simulated phylogeny
112 -
#' \dontrun{
113 113
#' par(mfrow = c(1, 1))
114 114
#' plot(sim_phylogeny, main = "phylogeny")
115 -
#' }
115 +
#'
116 116
#' # create problem with a maximum phylogenetic diversity objective,
117 117
#' # where each feature needs 10% of its distribution to be secured for
118 118
#' # it to be adequately conserved and a total budget of 1900
@@ -121,7 +121,7 @@
Loading
121 121
#'       add_relative_targets(0.1) %>%
122 122
#'       add_binary_decisions() %>%
123 123
#'       add_default_solver(verbose = FALSE)
124 -
#' \dontrun{
124 +
#'
125 125
#' # solve problem
126 126
#' s1 <- solve(p1)
127 127
#'
@@ -138,7 +138,7 @@
Loading
138 138
#'        rep("black", nlayers(sim_features)),
139 139
#'        sim_phylogeny$tip.label %in% r1$feature[r1$met],
140 140
#'        "red"))
141 -
#' }
141 +
#'
142 142
#' # rename the features in the example phylogeny for use with the
143 143
#' # multi-zone data
144 144
#' sim_phylogeny$tip.label <- feature_names(sim_features_zones)
@@ -160,7 +160,7 @@
Loading
160 160
#'       add_manual_targets(targets) %>%
161 161
#'       add_binary_decisions() %>%
162 162
#'       add_default_solver(verbose = FALSE)
163 -
#' \dontrun{
163 +
#'
164 164
#' # solve problem
165 165
#' s2 <- solve(p2)
166 166
#'
@@ -175,7 +175,7 @@
Loading
175 175
#' plot(sim_phylogeny, main = "adequately represented features",
176 176
#'      tip.color = replace(rep("black", nlayers(sim_features)),
177 177
#'                          which(r2$met), "red"))
178 -
#' }
178 +
#'
179 179
#' # create a multi-zone problem with a maximum phylogenetic diversity
180 180
#' # objective, where each zone has a separate budget.
181 181
#' p3 <- problem(sim_pu_zones_stack, sim_features_zones) %>%
@@ -183,7 +183,7 @@
Loading
183 183
#'       add_manual_targets(targets) %>%
184 184
#'       add_binary_decisions() %>%
185 185
#'       add_default_solver(verbose = FALSE)
186 -
#' \dontrun{
186 +
#'
187 187
#' # solve problem
188 188
#' s3 <- solve(p3)
189 189
#'

@@ -66,10 +66,10 @@
Loading
66 66
#' @family constraints
67 67
#'
68 68
#' @examples
69 +
#' \dontrun{
69 70
#' # load data
70 71
#' data(sim_pu_raster, sim_features)
71 72
#'
72 -
#' \dontrun{
73 73
#' # create a baseline problem with minimum shortfall objective
74 74
#' p0 <- problem(sim_pu_raster, sim_features) %>%
75 75
#'       add_min_shortfall_objective(1800) %>%

@@ -67,6 +67,7 @@
Loading
67 67
#' @family objectives
68 68
#'
69 69
#' @examples
70 +
#' \dontrun{
70 71
#' # load data
71 72
#' data(sim_pu_raster, sim_pu_zones_stack, sim_features, sim_features_zones)
72 73
#'
@@ -75,13 +76,12 @@
Loading
75 76
#'       add_max_utility_objective(5000) %>%
76 77
#'       add_binary_decisions() %>%
77 78
#'       add_default_solver(gap = 0, verbose = FALSE)
78 -
#' \dontrun{
79 +
#'
79 80
#' # solve problem
80 81
#' s1 <- solve(p1)
81 82
#'
82 83
#' # plot solution
83 84
#' plot(s1, main = "solution", axes = FALSE, box = FALSE)
84 -
#' }
85 85
#'
86 86
#' # create multi-zone problem with maximum utility objective that
87 87
#' # has a single budget for all zones
@@ -89,13 +89,13 @@
Loading
89 89
#'       add_max_utility_objective(5000) %>%
90 90
#'       add_binary_decisions() %>%
91 91
#'       add_default_solver(gap = 0, verbose = FALSE)
92 -
#' \dontrun{
92 +
#'
93 93
#' # solve problem
94 94
#' s2 <- solve(p2)
95 95
#'
96 96
#' # plot solution
97 97
#' plot(category_layer(s2), main = "solution", axes = FALSE, box = FALSE)
98 -
#' }
98 +
#'
99 99
#'
100 100
#' # create multi-zone problem with maximum utility objective that
101 101
#' # has separate budgets for each zone
@@ -103,7 +103,7 @@
Loading
103 103
#'       add_max_utility_objective(c(1000, 2000, 3000)) %>%
104 104
#'       add_binary_decisions() %>%
105 105
#'       add_default_solver(gap = 0, verbose = FALSE)
106 -
#' \dontrun{
106 +
#'
107 107
#' # solve problem
108 108
#' s3 <- solve(p3)
109 109
#'

@@ -102,6 +102,7 @@
Loading
102 102
#' 4061--4072.
103 103
#'
104 104
#' @examples
105 +
#' \dontrun{
105 106
#' # load ape package
106 107
#' require(ape)
107 108
#'
@@ -110,10 +111,9 @@
Loading
110 111
#'      sim_features_zones)
111 112
#'
112 113
#' # plot the simulated phylogeny
113 -
#' \dontrun{
114 114
#' par(mfrow = c(1, 1))
115 115
#' plot(sim_phylogeny, main = "phylogeny")
116 -
#' }
116 +
#'
117 117
#' # create problem with a maximum phylogenetic endemism objective,
118 118
#' # where each feature needs 10% of its distribution to be secured for
119 119
#' # it to be adequately conserved and a total budget of 1900
@@ -122,7 +122,7 @@
Loading
122 122
#'       add_relative_targets(0.1) %>%
123 123
#'       add_binary_decisions() %>%
124 124
#'       add_default_solver(verbose = FALSE)
125 -
#' \dontrun{
125 +
#'
126 126
#' # solve problem
127 127
#' s1 <- solve(p1)
128 128
#'
@@ -139,7 +139,7 @@
Loading
139 139
#'        rep("black", nlayers(sim_features)),
140 140
#'        sim_phylogeny$tip.label %in% r1$feature[r1$met],
141 141
#'        "red"))
142 -
#' }
142 +
#'
143 143
#' # rename the features in the example phylogeny for use with the
144 144
#' # multi-zone data
145 145
#' sim_phylogeny$tip.label <- feature_names(sim_features_zones)
@@ -161,7 +161,7 @@
Loading
161 161
#'       add_manual_targets(targets) %>%
162 162
#'       add_binary_decisions() %>%
163 163
#'       add_default_solver(verbose = FALSE)
164 -
#' \dontrun{
164 +
#'
165 165
#' # solve problem
166 166
#' s2 <- solve(p2)
167 167
#'
@@ -176,7 +176,7 @@
Loading
176 176
#' plot(sim_phylogeny, main = "adequately represented features",
177 177
#'      tip.color = replace(rep("black", nlayers(sim_features)),
178 178
#'                          which(r2$met), "red"))
179 -
#' }
179 +
#'
180 180
#' # create a multi-zone problem with a maximum phylogenetic endemism
181 181
#' # objective, where each zone has a separate budget.
182 182
#' p3 <- problem(sim_pu_zones_stack, sim_features_zones) %>%
@@ -184,7 +184,7 @@
Loading
184 184
#'       add_manual_targets(targets) %>%
185 185
#'       add_binary_decisions() %>%
186 186
#'       add_default_solver(verbose = FALSE)
187 -
#' \dontrun{
187 +
#'
188 188
#' # solve problem
189 189
#' s3 <- solve(p3)
190 190
#'

@@ -113,6 +113,7 @@
Loading
113 113
#' reformat solutions that contain multiple zones.
114 114
#'
115 115
#' @examples
116 +
#' \dontrun{
116 117
#' # set seed for reproducibility
117 118
#' set.seed(500)
118 119
#'
@@ -126,7 +127,7 @@
Loading
126 127
#'       add_relative_targets(0.1) %>%
127 128
#'       add_binary_decisions() %>%
128 129
#'       add_default_solver(verbose = FALSE)
129 -
#' \dontrun{
130 +
#'
130 131
#' # solve the problem
131 132
#' s1 <- solve(p1)
132 133
#'
@@ -144,14 +145,14 @@
Loading
144 145
#'
145 146
#' # plot solution
146 147
#' plot(s1, main = "solution", axes = FALSE, box = FALSE)
147 -
#' }
148 +
#'
148 149
#' # build minimal conservation problem with polygon (Spatial) data
149 150
#' p2 <- problem(sim_pu_polygons, sim_features, cost_column = "cost") %>%
150 151
#'       add_min_set_objective() %>%
151 152
#'       add_relative_targets(0.1) %>%
152 153
#'       add_binary_decisions() %>%
153 154
#'       add_default_solver(verbose = FALSE)
154 -
#' \dontrun{
155 +
#'
155 156
#' # solve the problem
156 157
#' s2 <- solve(p2)
157 158
#'
@@ -164,7 +165,7 @@
Loading
164 165
#'
165 166
#' # plot solution
166 167
#' spplot(s2, zcol = "solution_1", main = "solution", axes = FALSE, box = FALSE)
167 -
#' }
168 +
#'
168 169
#'
169 170
#' # build minimal conservation problem with polygon (sf) data
170 171
#' p3 <- problem(sim_pu_sf, sim_features, cost_column = "cost") %>%
@@ -172,7 +173,7 @@
Loading
172 173
#'       add_relative_targets(0.1) %>%
173 174
#'       add_binary_decisions() %>%
174 175
#'       add_default_solver(verbose = FALSE)
175 -
#' \dontrun{
176 +
#'
176 177
#' # solve the problem
177 178
#' s3 <- solve(p3)
178 179
#'
@@ -185,7 +186,7 @@
Loading
185 186
#'
186 187
#' # plot solution
187 188
#' plot(s3[, "solution_1"])
188 -
#' }
189 +
#'
189 190
#'
190 191
#' # build multi-zone conservation problem with raster data
191 192
#' p4 <- problem(sim_pu_zones_stack, sim_features_zones) %>%
@@ -194,7 +195,7 @@
Loading
194 195
#'                                   ncol = 3)) %>%
195 196
#'       add_binary_decisions() %>%
196 197
#'       add_default_solver(verbose = FALSE)
197 -
#' \dontrun{
198 +
#'
198 199
#' # solve the problem
199 200
#' s4 <- solve(p4)
200 201
#'
@@ -207,7 +208,7 @@
Loading
207 208
#'
208 209
#' # plot solution
209 210
#' plot(category_layer(s4), main = "solution", axes = FALSE, box = FALSE)
210 -
#' }
211 +
#'
211 212
#' # build multi-zone conservation problem with polygon (sf) data
212 213
#' p5 <- problem(sim_pu_zones_sf, sim_features_zones,
213 214
#'               cost_column = c("cost_1", "cost_2", "cost_3")) %>%
@@ -216,7 +217,7 @@
Loading
216 217
#'                                   ncol = 3)) %>%
217 218
#'       add_binary_decisions() %>%
218 219
#'       add_default_solver(verbose = FALSE)
219 -
#' \dontrun{
220 +
#'
220 221
#' # solve the problem
221 222
#' s5 <- solve(p5)
222 223
#'

@@ -140,6 +140,7 @@
Loading
140 140
#' *Ecological Modelling*, 228: 14--22.
141 141
#'
142 142
#' @examples
143 +
#' \dontrun{
143 144
#' # set seed for reproducibility
144 145
#' set.seed(500)
145 146
#'
@@ -164,14 +165,13 @@
Loading
164 165
#' bmat <- boundary_matrix(sim_pu_raster)
165 166
#' p4 <- p1 %>% add_boundary_penalties(50, 1, data = bmat)
166 167
#'
167 -
#' \dontrun{
168 168
#' # solve problems
169 169
#' s <- stack(solve(p1), solve(p2), solve(p3), solve(p4))
170 170
#'
171 171
#' # plot solutions
172 172
#' plot(s, main = c("basic solution", "small penalties", "high penalties",
173 173
#'                  "precomputed data"), axes = FALSE, box = FALSE)
174 -
#' }
174 +
#'
175 175
#' # create minimal problem with multiple zones and limit the run-time for
176 176
#' # solver to 10 seconds so this example doesn't take too long
177 177
#' p5 <- problem(sim_pu_zones_stack, sim_features_zones) %>%
@@ -222,7 +222,6 @@
Loading
222 222
#' # create problem with the zone matrix
223 223
#' p10 <- p5 %>% add_boundary_penalties(500, zone = zm10)
224 224
#'
225 -
#' \dontrun{
226 225
#' # solve problems
227 226
#' s2 <- stack(category_layer(solve(p5)), category_layer(solve(p6)),
228 227
#'             category_layer(solve(p7)), category_layer(solve(p8)),

@@ -104,6 +104,7 @@
Loading
104 104
#' @family constraints
105 105
#'
106 106
#' @examples
107 +
#' \dontrun{
107 108
#' # load data
108 109
#' data(sim_pu_raster, sim_features, sim_pu_zones_stack, sim_features_zones)
109 110
#'
@@ -126,14 +127,13 @@
Loading
126 127
#' p4 <- p1 %>% add_neighbor_constraints(3,
127 128
#'                data = adjacency_matrix(sim_pu_raster, directions = 8))
128 129
#'
129 -
#' \dontrun{
130 130
#' # solve problems
131 131
#' s1 <- stack(list(solve(p1), solve(p2), solve(p3), solve(p4)))
132 132
#'
133 133
#' # plot solutions
134 134
#' plot(s1, box = FALSE, axes = FALSE,
135 135
#'      main = c("basic solution", "1 neighbor", "2 neighbors", "3 neighbors"))
136 -
#' }
136 +
#'
137 137
#' # create minimal problem with multiple zones
138 138
#' p5 <- problem(sim_pu_zones_stack, sim_features_zones) %>%
139 139
#'       add_min_set_objective() %>%
@@ -163,7 +163,7 @@
Loading
163 163
#' z8[2, 1] <- 1
164 164
#' print(z8)
165 165
#' p8 <- p5 %>% add_neighbor_constraints(c(0, 1, 2), z8)
166 -
#' \dontrun{
166 +
#'
167 167
#' # solve problems
168 168
#' s2 <- list(p5, p6, p7, p8)
169 169
#' s2 <- lapply(s2, solve)

@@ -160,12 +160,13 @@
Loading
160 160
#' *Series B: Biological Sciences*, 269: 2437--2441.
161 161
#'
162 162
#' @examples
163 +
#' \dontrun{
164 +
#' # load package
165 +
#' library(Matrix)
166 +
#'
163 167
#' # set seed for reproducibility
164 168
#' set.seed(600)
165 169
#'
166 -
#' # load Matrix package for visualizing matrices
167 -
#' require(Matrix)
168 -
#'
169 170
#' # load data
170 171
#' data(sim_pu_sf, sim_pu_zones_stack, sim_features, sim_features_zones)
171 172
#'
@@ -189,9 +190,8 @@
Loading
189 190
#' b_matrix[] <- rescale(b_matrix[])
190 191
#'
191 192
#' # visualize connectivity matrix
192 -
#' \dontrun{
193 193
#' image(b_matrix)
194 -
#' }
194 +
#'
195 195
#' # create a symmetric connectivity matrix where the connectivity between
196 196
#' # two planning units corresponds to their spatial proximity
197 197
#' # i.e., planning units that are further apart share less connectivity
@@ -206,9 +206,8 @@
Loading
206 206
#' d_matrix[d_matrix < 0.7] <- 0
207 207
#'
208 208
#' # visualize connectivity matrix
209 -
#' \dontrun{
210 209
#' image(d_matrix)
211 -
#' }
210 +
#'
212 211
#' # create a symmetric connectivity matrix where the connectivity
213 212
#' # between adjacent two planning units corresponds to their combined
214 213
#' # value in a field in the planning unit attribute data
@@ -222,9 +221,8 @@
Loading
222 221
#' c_matrix[] <- rescale(c_matrix[])
223 222
#'
224 223
#' # visualize connectivity matrix
225 -
#' \dontrun{
226 224
#' image(c_matrix)
227 -
#' }
225 +
#'
228 226
#'
229 227
#' # create penalties
230 228
#' penalties <- c(10, 25)
@@ -243,7 +241,7 @@
Loading
243 241
#'                paste0("b_matrix (", penalties,")"),
244 242
#'                paste0("d_matrix (", penalties,")"),
245 243
#'                paste0("c_matrix (", penalties,")"))
246 -
#' \dontrun{
244 +
#'
247 245
#' # solve problems
248 246
#' s2 <- lapply(p2, solve)
249 247
#'
@@ -253,7 +251,6 @@
Loading
253 251
#'   plot(s2[[i]], main = names(p2)[i], cex = 1.5, col = "white")
254 252
#'   plot(s2[[i]][s2[[i]]$solution_1 == 1, ], col = "darkgreen", add = TRUE)
255 253
#' }
256 -
#' }
257 254
#'
258 255
#' # create minimal multi-zone problem and limit solver to one minute
259 256
#' # to obtain solutions in a short period of time
@@ -267,9 +264,8 @@
Loading
267 264
#' a_matrix <- adjacency_matrix(sim_pu_zones_stack)
268 265
#'
269 266
#' # visualize matrix
270 -
#' \dontrun{
271 267
#' image(a_matrix)
272 -
#' }
268 +
#'
273 269
#' # create a zone matrix where connectivities are only present between
274 270
#' # planning units that are allocated to the same zone
275 271
#' zm1 <- as(diag(3), "Matrix")
@@ -344,7 +340,7 @@
Loading
344 340
#' names(p4) <- c("basic problem",
345 341
#'                paste0("zm", rep(seq_len(5), each = 2), " (",
346 342
#'                       rep(penalties2, 2), ")"))
347 -
#' \dontrun{
343 +
#'
348 344
#' # solve problems
349 345
#' s4 <- lapply(p4, solve)
350 346
#' s4 <- lapply(s4, category_layer)
@@ -352,7 +348,7 @@
Loading
352 348
#'
353 349
#' # plot solutions
354 350
#' plot(s4, main = names(p4), axes = FALSE, box = FALSE)
355 -
#' }
351 +
#'
356 352
#'
357 353
#' # create an array to manually specify the connectivities between
358 354
#' # each planning unit when they are allocated to each different zone
@@ -374,7 +370,7 @@
Loading
374 370
#'
375 371
#' # assign names to the problems
376 372
#' names(p5) <- c("basic problem", "connectivity array")
377 -
#' \dontrun{
373 +
#'
378 374
#' # solve problems
379 375
#' s5 <- lapply(p5, solve)
380 376
#' s5 <- lapply(s5, category_layer)

@@ -58,6 +58,7 @@
Loading
58 58
#' @family targets
59 59
#'
60 60
#' @examples
61 +
#' \dontrun{
61 62
#' # set seed for reproducibility
62 63
#' set.seed(500)
63 64
#'
@@ -70,13 +71,13 @@
Loading
70 71
#'       add_relative_targets(0.1) %>%
71 72
#'       add_binary_decisions() %>%
72 73
#'       add_default_solver(verbose = FALSE)
73 -
#' \dontrun{
74 +
#'
74 75
#' # solve problem
75 76
#' s1 <- solve(p1)
76 77
#'
77 78
#' # plot solution
78 79
#' plot(s1, main = "solution", axes = FALSE, box = FALSE)
79 -
#' }
80 +
#'
80 81
#' # create equivalent problem using add_manual_targets
81 82
#' p2 <- problem(sim_pu_raster, sim_features) %>%
82 83
#'       add_min_set_objective() %>%
@@ -85,13 +86,13 @@
Loading
85 86
#'                                     target = 0.1)) %>%
86 87
#'       add_binary_decisions() %>%
87 88
#'       add_default_solver(verbose = FALSE)
88 -
#' \dontrun{
89 +
#'
89 90
#' # solve problem
90 91
#' s2 <- solve(p2)
91 92
#'
92 93
#' # plot solution
93 94
#' plot(s2, main = "solution", axes = FALSE, box = FALSE)
94 -
#' }
95 +
#'
95 96
#' # create problem with targets set for only a few features
96 97
#' p3 <- problem(sim_pu_raster, sim_features) %>%
97 98
#'       add_min_set_objective() %>%
@@ -100,13 +101,13 @@
Loading
100 101
#'         sense = ">=", target = 0.1)) %>%
101 102
#'       add_binary_decisions() %>%
102 103
#'       add_default_solver(verbose = FALSE)
103 -
#' \dontrun{
104 +
#'
104 105
#' # solve problem
105 106
#' s3 <- solve(p3)
106 107
#'
107 108
#' # plot solution
108 109
#' plot(s3, main = "solution", axes = FALSE, box = FALSE)
109 -
#' }
110 +
#'
110 111
#' # create problem that aims to secure at least 10% of the habitat for one
111 112
#' # feature whilst ensuring that the solution does not capture more than
112 113
#' # 20 units habitat for different feature
@@ -118,13 +119,13 @@
Loading
118 119
#'         sense = c(">=", "<="), target = c(0.1, 0.2))) %>%
119 120
#'       add_binary_decisions() %>%
120 121
#'       add_default_solver(verbose = FALSE)
121 -
#' \dontrun{
122 +
#'
122 123
#' # solve problem
123 124
#' s4 <- solve(p4)
124 125
#'
125 126
#' # plot solution
126 127
#' plot(s4, main = "solution", axes = FALSE, box = FALSE)
127 -
#' }
128 +
#'
128 129
#' # create a multi-zone problem that requires a specific amount of each
129 130
#' # feature in each zone
130 131
#' targets_matrix <- matrix(rpois(15, 1), nrow = 5, ncol = 3)
@@ -134,13 +135,13 @@
Loading
134 135
#'       add_absolute_targets(targets_matrix) %>%
135 136
#'       add_binary_decisions() %>%
136 137
#'       add_default_solver(verbose = FALSE)
137 -
#' \dontrun{
138 +
#'
138 139
#' # solve problem
139 140
#' s5 <- solve(p5)
140 141
#'
141 142
#' # plot solution
142 143
#' plot(category_layer(s5), main = "solution", axes = FALSE, box = FALSE)
143 -
#' }
144 +
#'
144 145
#' # create equivalent problem using add_manual_targets
145 146
#' targets_dataframe <- expand.grid(feature = feature_names(sim_features_zones),
146 147
#'                                  zone = zone_names(sim_features_zones),
@@ -152,13 +153,13 @@
Loading
152 153
#'       add_manual_targets(targets_dataframe) %>%
153 154
#'       add_binary_decisions() %>%
154 155
#'       add_default_solver(verbose = FALSE)
155 -
#' \dontrun{
156 +
#'
156 157
#' # solve problem
157 158
#' s6 <- solve(p6)
158 159
#'
159 160
#' # plot solution
160 161
#' plot(category_layer(s6), main = "solution", axes = FALSE, box = FALSE)
161 -
#' }
162 +
#'
162 163
#' # create a problem that requires a total of 20 units of habitat to be
163 164
#' # captured for two species. This can be achieved through representing
164 165
#' # habitat in two zones. The first zone represents a full restoration of the
@@ -186,7 +187,7 @@
Loading
186 187
#'       add_manual_targets(targets_dataframe2) %>%
187 188
#'       add_binary_decisions() %>%
188 189
#'       add_default_solver(verbose = FALSE)
189 -
#' \dontrun{
190 +
#'
190 191
#' # solve problem
191 192
#' s7 <- solve(p7)
192 193
#'

@@ -396,8 +396,9 @@
Loading
396 396
#' points(s4[s4$solution_1 > 0.5, ], col = "darkgreen", cex = 2, pch = 19)
397 397
#'
398 398
#' # note that as_Spatial() is for convenience to plot all solutions together
399 -
#' plot(as_Spatial(s5), main = "sf (polygon) data", pch = 19)
400 -
#' plot(as_Spatial(s5[s5$solution_1 > 0.5, ]), col = "darkgreen", add = TRUE)
399 +
#' plot(sf::as_Spatial(s5), main = "sf (polygon) data", pch = 19)
400 +
#' plot(sf::as_Spatial(s5[s5$solution_1 > 0.5, ]),
401 +
#'      col = "darkgreen", add = TRUE)
401 402
#'
402 403
#' plot(s6, main = "preprocessed data (polygon data)", pch = 19)
403 404
#' plot(s6[s6$solution_1 > 0.5, ], col = "darkgreen", add = TRUE)

@@ -83,12 +83,13 @@
Loading
83 83
#' conservation, *Conservation Letters*, 3: 359--368.
84 84
#'
85 85
#' @examples
86 +
#' \dontrun{
87 +
#' # load package
88 +
#' library(Matrix)
89 +
#'
86 90
#' # set seed for reproducibility
87 91
#' set.seed(600)
88 92
#'
89 -
#' # load Matrix package for visualizing matrices
90 -
#' require(Matrix)
91 -
#'
92 93
#' # load data
93 94
#' data(sim_pu_sf, sim_pu_zones_stack, sim_features, sim_features_zones)
94 95
#'
@@ -135,9 +136,7 @@
Loading
135 136
#' acm1[] <- rescale(acm1[])
136 137
#'
137 138
#' # visualize asymmetric connectivity matrix
138 -
#' \dontrun{
139 139
#' image(acm1)
140 -
#' }
141 140
#'
142 141
#' # create penalties
143 142
#' penalties <- c(1, 50)
@@ -151,7 +150,6 @@
Loading
151 150
#' # assign names to the problems
152 151
#' names(p2) <- c("basic problem", paste0("acm1 (", penalties,")"))
153 152
#'
154 -
#' \dontrun{
155 153
#' # solve problems
156 154
#' s2 <- lapply(p2, solve)
157 155
#'
@@ -161,7 +159,6 @@
Loading
161 159
#'   plot(s2[[i]], main = names(p2)[i], cex = 1.5, col = "white")
162 160
#'   plot(s2[[i]][s2[[i]]$solution_1 == 1, ], col = "darkgreen", add = TRUE)
163 161
#' }
164 -
#' }
165 162
#'
166 163
#' # create minimal multi-zone problem and limit solver to one minute
167 164
#' # to obtain solutions in a short period of time
@@ -184,7 +181,6 @@
Loading
184 181
#' # assign names to the problems
185 182
#' names(p4) <- c("basic problem", paste0("acm2 (", penalties,")"))
186 183
#'
187 -
#' \dontrun{
188 184
#' # solve problems
189 185
#' s4 <- lapply(p4, solve)
190 186
#' s4 <- lapply(s4, category_layer)

@@ -91,6 +91,7 @@
Loading
91 91
#' reserve network. *Operations Research*, 54: 379--388.
92 92
#'
93 93
#' @examples
94 +
#' \dontrun{
94 95
#' # load data
95 96
#' data(sim_pu_raster, sim_features, sim_pu_zones_stack, sim_features_zones)
96 97
#'
@@ -103,14 +104,14 @@
Loading
103 104
#'
104 105
#' # create problem with added connected constraints
105 106
#' p2 <- p1 %>% add_contiguity_constraints()
106 -
#' \dontrun{
107 +
#'
107 108
#' # solve problems
108 109
#' s <- stack(solve(p1), solve(p2))
109 110
#'
110 111
#' # plot solutions
111 112
#' plot(s, main = c("basic solution", "connected solution"), axes = FALSE,
112 113
#'      box = FALSE)
113 -
#' }
114 +
#'
114 115
#' # create minimal problem with multiple zones, and limit the solver to
115 116
#' # 30 seconds to obtain solutions in a feasible period of time
116 117
#' p3 <- problem(sim_pu_zones_stack, sim_features_zones) %>%
@@ -141,7 +142,7 @@
Loading
141 142
#' z6[2, 1] <- 1
142 143
#' print(z6)
143 144
#' p6 <- p3 %>% add_contiguity_constraints(z6)
144 -
#' \dontrun{
145 +
#'
145 146
#' # solve problems
146 147
#' s2 <- lapply(list(p3, p4, p5, p6), solve)
147 148
#' s2 <- lapply(s2, category_layer)
@@ -150,7 +151,7 @@
Loading
150 151
#' # plot solutions
151 152
#' plot(s2, axes = FALSE, box = FALSE,
152 153
#'      main = c("basic solution", "p4", "p5", "p6"))
153 -
#' }
154 +
#'
154 155
#' # create a problem that has a main "reserve zone" and a secondary
155 156
#' # "corridor zone" to connect up import areas. Here, each feature has a
156 157
#' # target of 30% of its distribution. If a planning unit is allocated to the
@@ -193,7 +194,7 @@
Loading
193 194
#'       add_contiguity_constraints(z7) %>%
194 195
#'       add_binary_decisions() %>%
195 196
#'       add_default_solver(verbose = FALSE)
196 -
#' \dontrun{
197 +
#'
197 198
#' # solve problems
198 199
#' s7 <- category_layer(solve(p7))
199 200
#'

@@ -66,6 +66,7 @@
Loading
66 66
#' 16: 242--248.
67 67
#'
68 68
#' @examples
69 +
#' \dontrun{
69 70
#' # load data
70 71
#' data(sim_pu_raster, sim_pu_zones_stack, sim_features, sim_features_zones)
71 72
#'
@@ -75,13 +76,12 @@
Loading
75 76
#'       add_relative_targets(0.1) %>%
76 77
#'       add_binary_decisions() %>%
77 78
#'       add_default_solver(verbose = FALSE)
78 -
#' \dontrun{
79 +
#'
79 80
#' # solve problem
80 81
#' s1 <- solve(p1)
81 82
#'
82 83
#' # plot solution
83 84
#' plot(s1, main = "solution", axes = FALSE, box = FALSE)
84 -
#' }
85 85
#'
86 86
#' # create multi-zone problem with maximum features objective,
87 87
#' # with 10% representation targets for each feature, and set
@@ -92,13 +92,13 @@
Loading
92 92
#'       add_relative_targets(matrix(0.1, ncol = 3, nrow = 5)) %>%
93 93
#'       add_binary_decisions() %>%
94 94
#'       add_default_solver(verbose = FALSE)
95 -
#' \dontrun{
95 +
#'
96 96
#' # solve problem
97 97
#' s2 <- solve(p2)
98 98
#'
99 99
#' # plot solution
100 100
#' plot(category_layer(s2), main = "solution", axes = FALSE, box = FALSE)
101 -
#' }
101 +
#'
102 102
#' # create multi-zone problem with maximum features objective,
103 103
#' # with 10% representation targets for each feature, and set
104 104
#' # separate budgets for each management zone
@@ -107,7 +107,7 @@
Loading
107 107
#'       add_relative_targets(matrix(0.1, ncol = 3, nrow = 5)) %>%
108 108
#'       add_binary_decisions() %>%
109 109
#'       add_default_solver(verbose = FALSE)
110 -
#' \dontrun{
110 +
#'
111 111
#' # solve problem
112 112
#' s3 <- solve(p3)
113 113
#'

@@ -22,44 +22,38 @@
Loading
22 22
#' @aliases intersecting_units,Raster,Raster-method intersecting_units,Raster,Spatial-method intersecting_units,Spatial,Raster-method intersecting_units,Spatial,Spatial-method intersecting_units,sf,Spatial-method intersecting_units,Spatial,sf-method intersecting_units,sf,sf-method intersecting_units,Raster,sf-method intersecting_units,sf,Raster-method intersecting_units,data.frame,ANY-method
23 23
#'
24 24
#' @examples
25 +
#' \dontrun{
25 26
#' # create data
26 -
#' r <- raster(matrix(1:9, byrow = TRUE, ncol=3))
27 +
#' r <- raster(matrix(1:9, byrow = TRUE, ncol = 3))
27 28
#' r_with_holes <- r
28 29
#' r_with_holes[c(1, 5, 9)] <- NA
29 30
#' ply <- rasterToPolygons(r)
30 -
#' ply_with_holes <- st_as_sf(rasterToPolygons(r_with_holes))
31 +
#' ply_with_holes <- sf::st_as_sf(rasterToPolygons(r_with_holes))
31 32
#'
32 33
#' # intersect raster with raster
33 -
#' \dontrun{
34 34
#' par(mfrow = c(1, 2))
35 35
#' plot(r, main = "x=Raster")
36 36
#' plot(r_with_holes, main = "y=Raster")
37 -
#' }
38 37
#' print(intersecting_units(r, r_with_holes))
39 38
#'
40 39
#' # intersect raster with polygons (sf)
41 -
#' \dontrun{
42 40
#' par(mfrow = c(1, 2))
43 41
#' plot(r, main = "x=Raster")
44 42
#' plot(ply_with_holes, main = "y=sf", key.pos = NULL, reset = FALSE)
45 -
#' }
46 43
#' print(intersecting_units(r, ply_with_holes))
47 44
#'
48 45
#' # intersect polygons (Spatial) with raster
49 -
#' \dontrun{
50 46
#' par(mfrow = c(1, 2))
51 47
#' plot(ply, main = "x=Spatial")
52 48
#' plot(r_with_holes, main = "y=Raster")
53 -
#' }
54 49
#' print(intersecting_units(ply, r_with_holes))
55 50
#'
56 51
#' # intersect polygons (Spatial) with polygons (sf)
57 -
#' \dontrun{
58 52
#' par(mfrow = c(1, 2))
59 53
#' plot(ply, main = "x=Spatial")
60 54
#' plot(ply_with_holes, main = "y=sf", key.pos = NULL, reset = FALSE)
61 -
#' }
62 55
#' print(intersecting_units(ply, ply_with_holes))
56 +
#' }
63 57
#'
64 58
#' @export
65 59
methods::setGeneric("intersecting_units",

@@ -51,6 +51,7 @@
Loading
51 51
#' @rdname connectivity_matrix
52 52
#'
53 53
#' @examples
54 +
#' \dontrun{
54 55
#' # load data
55 56
#' data(sim_pu_raster, sim_pu_sf, sim_features)
56 57
#'
@@ -67,13 +68,12 @@
Loading
67 68
#' cm_raster <- connectivity_matrix(r, cd[[2]])
68 69
#'
69 70
#' ## plot data and matrix
70 -
#' \dontrun{
71 71
#' par(mfrow = c(1,3))
72 72
#' plot(r, main = "planning units (raster)", axes = FALSE, box = FALSE)
73 73
#' plot(cd[[2]], main = "conductivity", axes = FALSE, box = FALSE)
74 74
#' plot(clamp(raster(as.matrix(cm_raster)), lower = 1e-5, useValues = FALSE),
75 75
#'      main = "connectivity", axes = FALSE, box = FALSE)
76 -
#' }
76 +
#'
77 77
#' # create connectivity matrix using polygon planning unit data using
78 78
#' # the habitat suitability data for the second feature to represent
79 79
#' # planning unit conductances
@@ -84,12 +84,10 @@
Loading
84 84
#' cm_ply <- connectivity_matrix(ply, sim_features[[2]])
85 85
#'
86 86
#' ## plot data and matrix
87 -
#' \dontrun{
88 87
#' par(mfrow = c(1, 2))
89 -
#' plot(st_geometry(ply), main = "planning units (sf)")
88 +
#' plot(sf::st_geometry(ply), main = "planning units (sf)")
90 89
#' plot(clamp(raster(as.matrix(cm_ply)), lower = 1e-5, useValues = FALSE),
91 90
#'      main = "connectivity", axes = FALSE, box = FALSE)
92 -
#' }
93 91
#'
94 92
#' # create connectivity matrix using habitat suitability data for each feature,
95 93
#' # this could be useful if prioritisations should spatially clump
@@ -104,12 +102,10 @@
Loading
104 102
#' cm_sum <- Reduce("+", cm_sum) # sum matrices together
105 103
#'
106 104
#' ## plot data and matrix
107 -
#' \dontrun{
108 105
#' par(mfrow = c(1, 2))
109 106
#' plot(r, main = "planning units (raster)", axes = FALSE, box = FALSE)
110 107
#' plot(clamp(raster(as.matrix(cm_sum)), lower = 1e-5, useValues = FALSE),
111 108
#'      main = "connectivity", axes = FALSE, box = FALSE)
112 -
#' }
113 109
#'
114 110
#' ## we could take this example one step further, and use weights to indicate
115 111
#' ## relative importance of maintaining functional connectivity
@@ -125,12 +121,10 @@
Loading
125 121
#' cm_wsum <- Reduce("+", cm_wsum) # sum matrices together
126 122
#'
127 123
#' ## plot data and matrix
128 -
#' \dontrun{
129 124
#' par(mfrow = c(1, 2))
130 125
#' plot(r, main = "planning units (raster)", axes = FALSE, box = FALSE)
131 126
#' plot(clamp(raster(as.matrix(cm_wsum)), lower = 1e-5, useValues = FALSE),
132 127
#'      main = "connectivity", axes = FALSE, box = FALSE)
133 -
#' }
134 128
#'
135 129
#' ## since the statistical distribution of the connectivity values
136 130
#' ## for each feature (e.g., the mean and standard deviation of the
@@ -143,7 +137,6 @@
Loading
143 137
#' ## note that we wouldn't want to rescale them between 0 and 1 since
144 138
#' ## a value of zero means that there is no connectivity at all (and
145 139
#' ## and not a relatively small amount of connectivity)
146 -
#' \dontrun{
147 140
#' ### define helper function
148 141
#' library(scales) # load scales library for rescale
149 142
#' rescale_matrix <- function(x) {x@x <- rescale(x@x, c(0.01, 1)); x}
@@ -155,15 +148,12 @@
Loading
155 148
#' cm_lwsum <- lapply(cm_lwsum, rescale_matrix) # rescale matrices to [0.01, 1]
156 149
#' cm_lwsum <- Map("*", cm_lwsum, weights) # multiply by weights
157 150
#' cm_lwsum <- Reduce("+", cm_lwsum) # sum matrices together
158 -
#' }
159 151
#'
160 152
#' ## plot data and matrix
161 -
#' \dontrun{
162 153
#' par(mfrow = c(1, 2))
163 154
#' plot(r, main = "planning units (raster)", axes = FALSE, box = FALSE)
164 155
#' plot(clamp(raster(as.matrix(cm_lwsum)), lower = 1e-5, useValues = FALSE),
165 156
#'      main = "connectivity", axes = FALSE, box = FALSE)
166 -
#' }
167 157
#'
168 158
#' ## another approach for normalizing the data could be using z-scores
169 159
#' ## note that after normalizing the data we would need to add a constant
@@ -186,7 +176,6 @@
Loading
186 176
#' cm_zwsum <- Reduce("+", cm_zwsum) # sum matrices together
187 177
#'
188 178
#' ## plot data and matrix
189 -
#' \dontrun{
190 179
#' par(mfrow = c(1, 2))
191 180
#' plot(r, main = "planning units (raster)", axes = FALSE, box = FALSE)
192 181
#' plot(clamp(raster(as.matrix(cm_zwsum)), lower = 1e-5, useValues = FALSE),

@@ -46,6 +46,7 @@
Loading
46 46
#' @rdname adjacency_matrix
47 47
#'
48 48
#' @examples
49 +
#' \dontrun{
49 50
#' # load data
50 51
#' data(sim_pu_raster, sim_pu_sf, sim_pu_lines)
51 52
#'
@@ -71,7 +72,6 @@
Loading
71 72
#' am_lns <- adjacency_matrix(lns)
72 73
#'
73 74
#' # plot data and the adjacency matrices
74 -
#' \dontrun{
75 75
#' par(mfrow = c(4,2))
76 76
#'
77 77
#' ## plot raster and adjacency matrix

@@ -31,6 +31,7 @@
Loading
31 31
#' @family overviews
32 32
#'
33 33
#' @examples
34 +
#' \dontrun{
34 35
#' # load data
35 36
#' data(sim_pu_raster, sim_features)
36 37
#'
@@ -53,7 +54,7 @@
Loading
53 54
#' p4 <- p %>% add_manual_targets(data.frame(feature = names(sim_features),
54 55
#'                                           target = 0.1,
55 56
#'                                           type = "relative"))
56 -
#' \dontrun{
57 +
#'
57 58
#' # solve problem
58 59
#' s <- stack(solve(p1), solve(p2), solve(p3), solve(p4))
59 60
#'

@@ -40,6 +40,7 @@
Loading
40 40
#' more complex conservation problems. *Ecography*, 23: 565--574.
41 41
#'
42 42
#' @examples
43 +
#' \dontrun{
43 44
#' # set seed for reproducibility
44 45
#' set.seed(500)
45 46
#'
@@ -53,13 +54,13 @@
Loading
53 54
#'       add_cuts_portfolio(10) %>%
54 55
#'       add_default_solver(gap = 0.2, verbose = FALSE)
55 56
#'
56 -
#' \dontrun{
57 +
#'
57 58
#' # solve problem and generate 10 solutions within 20% of optimality
58 59
#' s1 <- solve(p1)
59 60
#'
60 61
#' # plot solutions in portfolio
61 62
#' plot(stack(s1), axes = FALSE, box = FALSE)
62 -
#' }
63 +
#'
63 64
#' # build multi-zone conservation problem with cuts portfolio
64 65
#' p2 <- problem(sim_pu_zones_stack, sim_features_zones) %>%
65 66
#'       add_min_set_objective() %>%
@@ -69,7 +70,6 @@
Loading
69 70
#'       add_cuts_portfolio(10) %>%
70 71
#'       add_default_solver(gap = 0.2, verbose = FALSE)
71 72
#'
72 -
#' \dontrun{
73 73
#' # solve the problem
74 74
#' s2 <- solve(p2)
75 75
#'
@@ -77,8 +77,8 @@
Loading
77 77
#' str(s2, max.level = 1)
78 78
#'
79 79
#' # plot solutions in portfolio
80 -
#' plot(stack(lapply(s2, category_layer)), main = "solution", axes = FALSE,
81 -
#'      box = FALSE)
80 +
#' plot(stack(lapply(s2, category_layer)),
81 +
#'      main = "solution", axes = FALSE, box = FALSE)
82 82
#' }
83 83
#' @name add_cuts_portfolio
84 84
NULL

@@ -29,7 +29,8 @@
Loading
29 29
#'
30 30
#' # plot categorical raster and binary stack representation
31 31
#' \dontrun{
32 -
#' plot(stack(x, y), main = c("x", "y[[1]]", "y[[2]]", "y[[3]]"), nr = 1)
32 +
#' plot(stack(x, y), main = c("x", "y[[1]]", "y[[2]]", "y[[3]]"),
33 +
#'      nr = 1)
33 34
#' }
34 35
#' @export
35 36
binary_stack <- function(x) {

@@ -16,7 +16,6 @@
Loading
16 16
  std::vector<arma::sp_mat> rij;
17 17
  import_rij(rij_list, rij);
18 18
  // intialize variables
19 -
  std::size_t n_f = static_cast<std::size_t>(rij[0].n_cols);
20 19
  std::size_t n_pu = static_cast<std::size_t>(rij[0].n_rows);
21 20
  std::size_t n_z = static_cast<std::size_t>(rij_list.size());
22 21
  std::size_t n_t = targets_feature.size();
Files Coverage
R 98.39%
src 98.22%
Project Totals (128 files) 98.34%
1
coverage:
2
  status:
3
    project:
4
      default:
5
        target: auto
6
        threshold: 20%
7
    patch:
8
      default:
9
        target: auto
10
        threshold: 20%
11
        informational: true
Sunburst
The inner-most circle is the entire project, moving away from the center are folders then, finally, a single file. The size and color of each slice is representing the number of statements and the coverage, respectively.
Icicle
The top section represents the entire project. Proceeding with folders and finally individual files. The size and color of each slice is representing the number of statements and the coverage, respectively.
Grid
Each block represents a single file in the project. The size and color of each block is represented by the number of statements and the coverage, respectively.
Loading