-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdata_structures.R
More file actions
530 lines (457 loc) · 18.6 KB
/
data_structures.R
File metadata and controls
530 lines (457 loc) · 18.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
#' Validate MinPatch inputs
#'
#' Internal function to validate all inputs to the MinPatch algorithm,
#' including locked-in and locked-out constraints
#'
#' @param solution Binary solution vector
#' @param planning_units sf object with planning units
#' @param targets data.frame with targets
#' @param costs numeric vector of costs
#' @param min_patch_size minimum patch size
#' @param patch_radius patch radius for adding patches
#' @param boundary_penalty Boundary penalty value
#' @param locked_in_indices Optional indices of locked-in planning units
#' @param locked_out_indices Optional indices of locked-out planning units
#' @param area_dict Optional area dictionary for locked-in patch size validation
#' @param verbose Logical, whether to print warnings
#'
#' @return NULL (throws errors if validation fails)
#' @keywords internal
validate_inputs <- function(solution, planning_units, targets, costs,
min_patch_size, patch_radius, boundary_penalty,
locked_in_indices = NULL, locked_out_indices = NULL,
area_dict = NULL, verbose = TRUE) {
# Check solution
if (!is.numeric(solution) || !all(solution %in% c(0, 1))) {
stop("Solution must be a binary numeric vector (0s and 1s)")
}
# Check planning units
if (!inherits(planning_units, "sf")) {
stop("planning_units must be an sf object")
}
if (nrow(planning_units) != length(solution)) {
stop("Number of planning units must match length of solution vector")
}
# Check for required columns in planning units
required_cols <- c("geometry")
if (!all(required_cols %in% names(planning_units))) {
stop("planning_units must contain geometry column")
}
# Check targets
if (!is.data.frame(targets)) {
stop("targets must be a data.frame")
}
required_target_cols <- c("feature_id", "target")
if (!all(required_target_cols %in% names(targets))) {
stop("targets must contain 'feature_id' and 'target' columns")
}
# Check numeric parameters (handle units objects from sf)
min_patch_size_numeric <- as.numeric(min_patch_size)
if (!is.numeric(min_patch_size_numeric) || min_patch_size_numeric <= 0) {
stop("min_patch_size must be a positive number")
}
patch_radius_numeric <- as.numeric(patch_radius)
if (!is.numeric(patch_radius_numeric) || patch_radius_numeric <= 0) {
stop("patch_radius must be a positive number")
}
if (!is.numeric(boundary_penalty) || boundary_penalty < 0) {
stop("boundary_penalty must be a non-negative number")
}
# Check costs if provided
if (!is.null(costs)) {
if (!is.numeric(costs) || length(costs) != length(solution)) {
stop("costs must be a numeric vector with same length as solution")
}
if (any(costs < 0)) {
stop("costs must be non-negative")
}
}
# Validate locked-in and locked-out constraints
if (!is.null(locked_in_indices) && !is.null(locked_out_indices)) {
# Check for conflicts between locked-in and locked-out
conflicts <- intersect(locked_in_indices, locked_out_indices)
if (length(conflicts) > 0) {
stop(paste("Conflict detected: Planning units", paste(conflicts, collapse = ", "),
"are both locked-in and locked-out. This is not allowed."))
}
}
# Warn if locked-in units form patches smaller than min_patch_size
if (!is.null(locked_in_indices) && !is.null(area_dict) && length(locked_in_indices) > 0) {
locked_in_area <- sum(area_dict[as.character(locked_in_indices)])
if (as.numeric(locked_in_area) < as.numeric(min_patch_size_numeric)) {
if (verbose) {
warning(paste0("Locked-in planning units have total area (",
round(as.numeric(locked_in_area), 4),
") smaller than min_patch_size (",
min_patch_size_numeric,
"). These units will be preserved regardless of patch size constraints."))
}
}
}
}
#' Initialize MinPatch data structures
#'
#' Creates the internal data structures needed for MinPatch processing.
#' This function extracts locked-in and locked-out constraints from the
#' prioritizr problem and applies them as status codes:
#' - Status 2 (conserved) for locked-in units
#' - Status 3 (excluded) for locked-out units
#'
#' @param solution Binary solution vector
#' @param planning_units sf object with planning units
#' @param targets data.frame with targets
#' @param costs numeric vector of costs
#' @param min_patch_size minimum patch size
#' @param patch_radius patch radius
#' @param boundary_penalty Boundary penalty value
#' @param prioritizr_problem A prioritizr problem object
#' @param prioritizr_solution A solved prioritizr solution object
#' @param verbose Logical, whether to print progress
#'
#' @return List containing all necessary data structures
#' @keywords internal
initialize_minpatch_data <- function(solution, planning_units, targets, costs,
min_patch_size, patch_radius, boundary_penalty,
prioritizr_problem, prioritizr_solution, verbose = TRUE) {
n_units <- length(solution)
# Create unit dictionary: list with cost and status for each planning unit
if (is.null(costs)) {
costs <- rep(1, n_units) # Default unit costs
}
# Status codes: 0 = available, 1 = selected, 2 = conserved (locked-in), 3 = excluded (locked-out)
# Convert solution to status (1 = selected, 0 = available)
unit_dict <- vector("list", n_units)
names(unit_dict) <- as.character(seq_len(n_units))
for (i in seq_len(n_units)) {
unit_dict[[i]] <- list(
cost = costs[i],
status = as.integer(solution[i])
)
}
# Extract locked-in and locked-out constraints from prioritizr problem
locked_in_indices <- extract_locked_in_constraints(prioritizr_problem, verbose)
locked_out_indices <- extract_locked_out_constraints(prioritizr_problem, verbose)
# Apply locked-in constraints (status = 2)
if (length(locked_in_indices) > 0) {
for (idx in locked_in_indices) {
if (idx <= n_units) {
unit_dict[[as.character(idx)]]$status <- 2L
}
}
if (verbose) {
cat("Applied", length(locked_in_indices), "locked-in constraints\n")
}
}
# Apply locked-out constraints (status = 3)
if (length(locked_out_indices) > 0) {
for (idx in locked_out_indices) {
if (idx <= n_units) {
unit_dict[[as.character(idx)]]$status <- 3L
}
}
if (verbose) {
cat("Applied", length(locked_out_indices), "locked-out constraints\n")
}
}
# Calculate planning unit areas (needed for validation)
area_dict <- as.numeric(sf::st_area(planning_units))
names(area_dict) <- as.character(seq_len(n_units))
# Create cost dictionary
cost_dict <- costs
names(cost_dict) <- as.character(seq_len(n_units))
# Validate locked constraints after applying them
validate_inputs(solution, planning_units, targets, costs,
min_patch_size, patch_radius, boundary_penalty,
locked_in_indices, locked_out_indices, area_dict, verbose)
# Create boundary matrix (adjacency with shared boundary lengths)
boundary_matrix <- create_boundary_matrix(planning_units, verbose)
# Create abundance matrix (features in each planning unit)
# Extract directly from planning_units feature columns using prioritizr problem
abundance_matrix <- create_abundance_matrix(planning_units, prioritizr_problem)
# Create target dictionary
target_dict <- vector("list", nrow(targets))
names(target_dict) <- as.character(targets$feature_id)
for (i in seq_len(nrow(targets))) {
target_dict[[as.character(targets$feature_id[i])]] <- list(
name = paste0("feature_", targets$feature_id[i]),
target = targets$target[i],
penalty = 1.0, # Default penalty
type = 1 # Default type
)
}
# Create patch radius dictionary for adding patches
patch_radius_dict <- create_patch_radius_dict(planning_units, patch_radius, verbose)
return(list(
unit_dict = unit_dict,
area_dict = area_dict,
cost_dict = cost_dict,
boundary_matrix = boundary_matrix,
abundance_matrix = abundance_matrix,
target_dict = target_dict,
patch_radius_dict = patch_radius_dict,
min_patch_size = min_patch_size,
patch_radius = patch_radius,
boundary_penalty = boundary_penalty,
prioritizr_problem = prioritizr_problem,
prioritizr_solution = prioritizr_solution,
locked_in_indices = locked_in_indices,
locked_out_indices = locked_out_indices
))
}
#' Extract locked-in constraint indices from prioritizr problem
#'
#' @param prioritizr_problem A prioritizr problem object
#' @param verbose Logical, whether to print messages
#'
#' @return Integer vector of locked-in planning unit indices
#' @keywords internal
extract_locked_in_constraints <- function(prioritizr_problem, verbose = TRUE) {
locked_in <- integer(0)
if (!is.null(prioritizr_problem$constraints)) {
for (constraint in prioritizr_problem$constraints) {
# Check if this is a locked-in constraint
if (inherits(constraint, "LockedInConstraint")) {
# Extract indices using the constraint's data
if (!is.null(constraint$data) && "pu" %in% names(constraint$data)) {
locked_in <- unique(c(locked_in, constraint$data$pu))
}
}
}
}
return(sort(unique(locked_in)))
}
#' Extract locked-out constraint indices from prioritizr problem
#'
#' @param prioritizr_problem A prioritizr problem object
#' @param verbose Logical, whether to print messages
#'
#' @return Integer vector of locked-out planning unit indices
#' @keywords internal
extract_locked_out_constraints <- function(prioritizr_problem, verbose = TRUE) {
locked_out <- integer(0)
if (!is.null(prioritizr_problem$constraints)) {
for (constraint in prioritizr_problem$constraints) {
# Check if this is a locked-out constraint
if (inherits(constraint, "LockedOutConstraint")) {
# Extract indices using the constraint's data
if (!is.null(constraint$data) && "pu" %in% names(constraint$data)) {
locked_out <- unique(c(locked_out, constraint$data$pu))
}
}
}
}
return(sort(unique(locked_out)))
}
#' Create boundary matrix from planning units
#'
#' Creates a sparse matrix of shared boundary lengths between adjacent planning units.
#' Returns a Matrix::sparseMatrix for efficient storage and operations.
#' This optimized version supports parallel processing via the parallelly package.
#' When n_cores = 1, runs sequentially with no parallel overhead.
#'
#' @param planning_units sf object with planning unit geometries
#' @param verbose Logical, whether to print progress
#' @param n_cores Integer, number of cores to use. If NULL, uses availableCores(omit=1).
#' Set to 1 for sequential processing.
#'
#' @return Matrix::dgCMatrix sparse matrix where [i,j] is the shared boundary length
#' @keywords internal
create_boundary_matrix <- function(planning_units, verbose = TRUE, n_cores = NULL) {
n_units <- nrow(planning_units)
# Determine number of cores
if (is.null(n_cores)) {
if (requireNamespace("parallelly", quietly = TRUE)) {
n_cores <- parallelly::availableCores(omit = 2)
} else {
n_cores <- 1
}
}
# Only use parallel for larger datasets (overhead not worth it for small ones)
if (n_units < 500) {
n_cores <- 1
} else {
n_cores <- min(n_cores, n_units)
}
# Final safety check: ensure n_cores is always between 1 and n_units
n_cores <- max(1, min(n_cores, n_units))
if (verbose) {
if (n_cores > 1) {
cat("Calculating boundary matrix using", n_cores, "cores...\n")
} else {
cat("Calculating boundary matrix (optimized version)...\n")
}
}
# Check for invalid geometries and repair if needed
if (any(!sf::st_is_valid(planning_units))) {
cat("Warning: Invalid geometries detected, attempting to repair...\n")
planning_units <- sf::st_make_valid(planning_units)
}
# Pre-compute all boundaries once (major optimization)
boundaries <- sf::st_boundary(planning_units)
# Pre-compute all perimeters once for diagonal
perimeters <- as.numeric(sf::st_length(boundaries))
# Get sparse adjacency list (much more efficient than dense matrix)
touches_sparse <- sf::st_intersects(boundaries, boundaries)
# Split work into chunks - handle edge cases properly
if (n_cores == 1) {
# Single core: all units in one chunk
chunks <- list(seq_len(n_units))
} else {
# Multiple cores: split evenly
# Ensure we don't try to create more chunks than units
actual_cores <- min(n_cores, n_units)
if (actual_cores >= n_units) {
# If cores >= units, each unit gets its own chunk
chunks <- as.list(seq_len(n_units))
} else {
# Normal case: split into chunks
chunks <- split(seq_len(n_units), cut(seq_len(n_units), actual_cores, labels = FALSE))
}
}
# Function to process a chunk of units
process_chunk <- function(unit_indices) {
local_i <- integer()
local_j <- integer()
local_lengths <- numeric()
for (i in unit_indices) {
neighbors <- touches_sparse[[i]]
neighbors <- neighbors[neighbors != i]
if (length(neighbors) > 0) {
for (j in neighbors) {
if (i < j) { # Only process each pair once
intersection <- suppressWarnings(sf::st_intersection(
boundaries[i, ],
boundaries[j, ]
))
if (nrow(intersection) > 0) {
shared_length <- sum(as.numeric(sf::st_length(intersection)))
if (shared_length > 1e-10) {
local_i <- c(local_i, i, j)
local_j <- c(local_j, j, i)
local_lengths <- c(local_lengths, shared_length, shared_length)
} else if (shared_length > 0) {
local_i <- c(local_i, i, j)
local_j <- c(local_j, j, i)
local_lengths <- c(local_lengths, 1e-6, 1e-6)
}
}
}
}
}
}
list(i = local_i, j = local_j, x = local_lengths)
}
# Process chunks (parallel if n_cores > 1, sequential if n_cores = 1)
if (n_cores > 1 && requireNamespace("parallelly", quietly = TRUE)) {
# Parallel processing
cl <- parallelly::makeClusterPSOCK(n_cores, autoStop = TRUE, verbose = FALSE)
on.exit(parallel::stopCluster(cl), add = TRUE)
parallel::clusterExport(cl, c("boundaries", "touches_sparse"),
envir = environment())
parallel::clusterEvalQ(cl, library(sf))
if (verbose) cat("Processing chunks in parallel...\n")
results <- parallel::parLapply(cl, chunks, process_chunk)
} else {
# Sequential processing
results <- lapply(chunks, function(chunk) {
result <- process_chunk(chunk)
if (verbose && max(chunk) %% 100 == 0) {
cat("Processed", max(chunk), "of", n_units, "planning units\n")
}
result
})
}
# Combine results
if (verbose && n_cores > 1) cat("Combining results...\n")
i_indices <- unlist(lapply(results, function(r) r$i))
j_indices <- unlist(lapply(results, function(r) r$j))
boundary_lengths <- unlist(lapply(results, function(r) r$x))
# Add perimeters on diagonal
i_indices <- c(i_indices, seq_len(n_units))
j_indices <- c(j_indices, seq_len(n_units))
boundary_lengths <- c(boundary_lengths, perimeters)
# Create sparse matrix
Matrix::sparseMatrix(
i = i_indices,
j = j_indices,
x = boundary_lengths,
dims = c(n_units, n_units),
dimnames = list(as.character(seq_len(n_units)),
as.character(seq_len(n_units)))
)
}
#' Create abundance matrix from planning units
#'
#' Creates a matrix showing the amount of each feature in each planning unit
#' by extracting feature columns directly from planning_units using prioritizr problem
#'
#' @param planning_units sf object with planning unit geometries and feature columns
#' @param prioritizr_problem A prioritizr problem object to get feature names
#'
#' @return Named list where each planning unit contains feature abundances
#' @keywords internal
create_abundance_matrix <- function(planning_units, prioritizr_problem) {
n_units <- nrow(planning_units)
abundance_matrix <- vector("list", n_units)
names(abundance_matrix) <- as.character(seq_len(n_units))
# Initialize empty lists
for (i in seq_len(n_units)) {
abundance_matrix[[i]] <- list()
}
# Get feature names from prioritizr problem
feature_names <- prioritizr::feature_names(prioritizr_problem)
if (length(feature_names) > 0) {
for (i in seq_along(feature_names)) {
col_name <- feature_names[i]
feature_id <- as.character(i) # Use sequential numbering for feature IDs
# Check if this feature column exists in planning_units
if (col_name %in% names(planning_units)) {
abundances <- planning_units[[col_name]]
for (pu_id in seq_len(n_units)) {
if (abundances[pu_id] > 0) {
abundance_matrix[[as.character(pu_id)]][[feature_id]] <- abundances[pu_id]
}
}
} else {
warning(paste("Feature column", col_name, "not found in planning units"))
}
}
}
return(abundance_matrix)
}
#' Create patch radius dictionary
#'
#' For each planning unit, find all units within the specified patch radius.
#' Optimized version computes full distance matrix once instead of n times.
#'
#' @param planning_units sf object with planning unit geometries
#' @param patch_radius radius for patch creation
#'
#' @return Named list where each planning unit contains list of units within radius
#' @keywords internal
create_patch_radius_dict <- function(planning_units, patch_radius, verbose = TRUE) {
n_units <- nrow(planning_units)
patch_radius_dict <- vector("list", n_units)
names(patch_radius_dict) <- as.character(seq_len(n_units))
# Get centroids for distance calculations
centroids <- sf::st_centroid(planning_units %>%
dplyr::select("geometry"))
if (verbose) cat("Creating patch radius dictionary (optimized)...\n")
# OPTIMIZATION: Compute full distance matrix ONCE instead of n times
# This changes from O(n²) distance calculations to O(n²/2) calculations
dist_matrix <- sf::st_distance(centroids, centroids)
dist_matrix_numeric <- as.numeric(dist_matrix)
patch_radius_numeric <- as.numeric(patch_radius)
# Create matrix of dimensions n x n
dist_mat <- matrix(dist_matrix_numeric, nrow = n_units, ncol = n_units)
# For each unit, find neighbors within radius
for (i in seq_len(n_units)) {
# Use vectorized comparison on pre-computed distances
within_radius <- which(dist_mat[i, ] <= patch_radius_numeric & seq_len(n_units) != i)
patch_radius_dict[[i]] <- as.character(within_radius)
if (verbose && i %% 100 == 0) {
cat("Processed", i, "of", n_units, "planning units\n")
}
}
return(patch_radius_dict)
}