R/extendr-wrappers.R

Defines functions rust_corridor_graph_info rust_corridor_solve_cached rust_corridor_build_graph rust_corridor rust_vrp rust_tsp rust_huff rust_frlm_greedy rust_max_p rust_ward_constrained rust_spenc rust_azp rust_skater rust_connected_components rust_is_connected rust_mst rust_distance_matrix_manhattan rust_distance_matrix_euclidean

Documented in rust_azp rust_connected_components rust_corridor rust_distance_matrix_euclidean rust_distance_matrix_manhattan rust_frlm_greedy rust_huff rust_is_connected rust_mst rust_skater rust_spenc rust_tsp rust_vrp rust_ward_constrained

# Generated by extendr: Do not edit by hand

# nolint start

#
# This file was created with the following call:
#   .Call("wrap__make_spopt_wrappers", use_symbols = TRUE, package_name = "spopt")

#' @usage NULL
#' @useDynLib spopt, .registration = TRUE
NULL

#' Compute Euclidean distance matrix between two sets of points
#'
#' @param x1 X coordinates of first set of points
#' @param y1 Y coordinates of first set of points
#' @param x2 X coordinates of second set of points
#' @param y2 Y coordinates of second set of points
#' @return Distance matrix (n1 x n2)
#' @export
rust_distance_matrix_euclidean <- function(x1, y1, x2, y2) .Call(wrap__rust_distance_matrix_euclidean, x1, y1, x2, y2)

#' Compute Manhattan distance matrix between two sets of points
#'
#' @param x1 X coordinates of first set of points
#' @param y1 Y coordinates of first set of points
#' @param x2 X coordinates of second set of points
#' @param y2 Y coordinates of second set of points
#' @return Distance matrix (n1 x n2)
#' @export
rust_distance_matrix_manhattan <- function(x1, y1, x2, y2) .Call(wrap__rust_distance_matrix_manhattan, x1, y1, x2, y2)

#' Compute minimum spanning tree from adjacency matrix
#'
#' @param i Row indices (0-based) of adjacency matrix non-zero entries
#' @param j Column indices (0-based) of adjacency matrix non-zero entries
#' @param weights Edge weights (distances/dissimilarities)
#' @param n Number of nodes
#' @return List with MST edges (from, to, weight)
#' @export
rust_mst <- function(i, j, weights, n) .Call(wrap__rust_mst, i, j, weights, n)

#' Check if a graph is connected
#'
#' @param i Row indices of adjacency matrix
#' @param j Column indices of adjacency matrix
#' @param n Number of nodes
#' @return TRUE if connected, FALSE otherwise
#' @export
rust_is_connected <- function(i, j, n) .Call(wrap__rust_is_connected, i, j, n)

#' Find connected components
#'
#' @param i Row indices of adjacency matrix
#' @param j Column indices of adjacency matrix
#' @param n Number of nodes
#' @return Vector of component labels (0-based)
#' @export
rust_connected_components <- function(i, j, n) .Call(wrap__rust_connected_components, i, j, n)

#' Solve SKATER regionalization
#'
#' @param attrs Attribute matrix (n x p)
#' @param adj_i Row indices of adjacency
#' @param adj_j Column indices of adjacency
#' @param n_regions Number of regions to create
#' @param floor_var Optional floor variable values
#' @param floor_value Minimum floor value per region
#' @param seed Random seed
#' @return Vector of region labels (1-based)
#' @export
rust_skater <- function(attrs, adj_i, adj_j, n_regions, floor_var, floor_value, seed) .Call(wrap__rust_skater, attrs, adj_i, adj_j, n_regions, floor_var, floor_value, seed)

#' Solve AZP regionalization problem
#'
#' Automatic Zoning Procedure with basic, tabu, and SA variants.
#'
#' @param attrs Attribute matrix (n x p)
#' @param n_regions Number of regions to create
#' @param adj_i Row indices of adjacency (0-based)
#' @param adj_j Column indices of adjacency (0-based)
#' @param method "basic", "tabu", or "sa"
#' @param max_iterations Maximum iterations
#' @param tabu_length Tabu list length (for tabu method)
#' @param cooling_rate SA cooling rate (for sa method)
#' @param initial_temperature SA initial temperature (for sa method)
#' @param seed Random seed
#' @return List with labels, n_regions, objective
#' @export
rust_azp <- function(attrs, n_regions, adj_i, adj_j, method, max_iterations, tabu_length, cooling_rate, initial_temperature, seed) .Call(wrap__rust_azp, attrs, n_regions, adj_i, adj_j, method, max_iterations, tabu_length, cooling_rate, initial_temperature, seed)

#' Solve SPENC regionalization problem
#'
#' Spatially-encouraged spectral clustering.
#'
#' @param attrs Attribute matrix (n x p)
#' @param n_regions Number of regions to create
#' @param adj_i Row indices of adjacency (0-based)
#' @param adj_j Column indices of adjacency (0-based)
#' @param gamma RBF kernel parameter
#' @param seed Random seed
#' @return List with labels, n_regions, objective
#' @export
rust_spenc <- function(attrs, n_regions, adj_i, adj_j, gamma, seed) .Call(wrap__rust_spenc, attrs, n_regions, adj_i, adj_j, gamma, seed)

#' Solve spatially-constrained Ward clustering
#'
#' @param attrs Attribute matrix (n x p)
#' @param n_regions Number of regions to create
#' @param adj_i Row indices of adjacency (0-based)
#' @param adj_j Column indices of adjacency (0-based)
#' @return List with labels, n_regions, objective
#' @export
rust_ward_constrained <- function(attrs, n_regions, adj_i, adj_j) .Call(wrap__rust_ward_constrained, attrs, n_regions, adj_i, adj_j)

#' Solve Max-P regionalization problem
#'
#' Maximize the number of regions such that each region satisfies a minimum
#' threshold constraint on a spatial extensive attribute.
#'
#' @param attrs Attribute matrix (n x p) for computing within-region dissimilarity
#' @param threshold_var Values of the threshold variable (e.g., population)
#' @param threshold Minimum sum required per region
#' @param adj_i Row indices of adjacency (0-based)
#' @param adj_j Column indices of adjacency (0-based)
#' @param n_iterations Number of construction phase iterations
#' @param n_sa_iterations Number of simulated annealing iterations
#' @param cooling_rate SA cooling rate (e.g., 0.99)
#' @param tabu_length Tabu list length for SA
#' @param seed Random seed
#' @param compact Whether to optimize for compactness
#' @param compact_weight Weight for compactness vs dissimilarity (0-1)
#' @param compact_metric Compactness metric to use, either "centroid" or "nmi" (normalized moment of inertia)
#' @param centroids_x X coordinates of unit centroids (for compactness)
#' @param centroids_y Y coordinates of unit centroids (for compactness)
#' @param areas Areas of units (for compactness)
#' @param moments Second areal moments of units (for compactness)
#' @return List with labels (1-based), n_regions, objective, and compactness
#' @noRd
rust_max_p <- function(attrs, threshold_var, threshold, adj_i, adj_j, n_iterations, n_sa_iterations, cooling_rate, tabu_length, seed, homogeneous, compact, compact_weight, compact_metric, centroids_x, centroids_y, areas, moments) .Call(wrap__rust_max_p, attrs, threshold_var, threshold, adj_i, adj_j, n_iterations, n_sa_iterations, cooling_rate, tabu_length, seed, homogeneous, compact, compact_weight, compact_metric, centroids_x, centroids_y, areas, moments)

#' Solve FRLM using greedy heuristic
#'
#' @param n_candidates Number of candidate facility locations
#' @param path_candidates Flat array of candidate indices for each path
#' @param path_offsets Start index for each path in path_candidates
#' @param path_distances Distances to each candidate along paths
#' @param flow_volumes Volume of each flow
#' @param vehicle_range Maximum vehicle range
#' @param n_facilities Number of facilities to place
#' @return List with selected facilities and coverage info
#' @export
rust_frlm_greedy <- function(n_candidates, path_candidates, path_offsets, path_distances, flow_volumes, vehicle_range, n_facilities) .Call(wrap__rust_frlm_greedy, n_candidates, path_candidates, path_offsets, path_distances, flow_volumes, vehicle_range, n_facilities)

#' Compute Huff Model probabilities
#'
#' Computes probability surface based on distance decay and attractiveness.
#' Formula: \eqn{P_{ij} = (A_j \times D_{ij}^\beta) / \Sigma_k(A_k \times D_{ik}^\beta)}
#'
#' @param cost_matrix Cost/distance matrix (demand x stores)
#' @param attractiveness Attractiveness values for each store (pre-computed with exponents)
#' @param distance_exponent Distance decay exponent (typically negative, e.g., -1.5)
#' @param sales_potential Optional sales potential for each demand point
#' @return List with probabilities, market shares, expected sales
#' @export
rust_huff <- function(cost_matrix, attractiveness, distance_exponent, sales_potential) .Call(wrap__rust_huff, cost_matrix, attractiveness, distance_exponent, sales_potential)

#' Solve Traveling Salesman Problem (TSP)
#'
#' Solve a closed tour, open route, or fixed-end path over a square
#' cost/distance matrix using nearest-neighbor construction with optional
#' 2-opt and or-opt local search. Optional time windows and service times
#' can be supplied for each stop.
#'
#' @param cost_matrix Square cost/distance matrix (n x n)
#' @param start Start index (0-based)
#' @param end End index (0-based), or NULL for an open route
#' @param method Algorithm: "nn" (nearest-neighbor only) or "2-opt" (with local search)
#' @param earliest Optional earliest service times
#' @param latest Optional latest service times
#' @param service_time Optional service times at each stop
#' @return List with tour (1-based), total_cost, nn_cost, improvement_pct, iterations,
#'   arrival_time, and departure_time
#' @export
rust_tsp <- function(cost_matrix, start, end, method, earliest, latest, service_time) .Call(wrap__rust_tsp, cost_matrix, start, end, method, earliest, latest, service_time)

#' Solve Capacitated Vehicle Routing Problem (CVRP)
#'
#' Find minimum-cost routes for multiple vehicles, each with a capacity limit,
#' to serve all customers from a depot. Uses Clarke-Wright savings heuristic
#' with 2-opt, relocate, and swap improvement.
#'
#' @param cost_matrix Square cost/distance matrix (n x n)
#' @param depot Depot index (0-based)
#' @param demands Demand at each location (depot demand should be 0)
#' @param capacity Vehicle capacity
#' @param max_vehicles Maximum number of vehicles (NULL for unlimited)
#' @param method Algorithm: "savings" (construction only) or "2-opt" (with improvement)
#' @param service_times Optional service time at each stop (NULL for zero)
#' @param max_route_time Optional maximum total time per route (NULL for unlimited)
#' @param balance_time Whether to run route-time balancing phase
#' @param earliest Optional earliest arrival times at each stop
#' @param latest Optional latest arrival times at each stop
#' @return List with vehicle assignments, visit orders, costs, and route details
#' @export
rust_vrp <- function(cost_matrix, depot, demands, capacity, max_vehicles, method, service_times, max_route_time, balance_time, earliest, latest) .Call(wrap__rust_vrp, cost_matrix, depot, demands, capacity, max_vehicles, method, service_times, max_route_time, balance_time, earliest, latest)

#' Least-cost corridor routing on a raster grid
#'
#' @param values Flattened cell values (row-major), NaN = impassable
#' @param n_rows Number of rows in the raster
#' @param n_cols Number of columns in the raster
#' @param cell_width Cell width in CRS units
#' @param cell_height Cell height in CRS units
#' @param origin_cell 0-based cell index of origin
#' @param dest_cell 0-based cell index of destination
#' @param neighbours Cell connectivity: 4, 8, or 16
#' @param method Routing algorithm: "dijkstra", "bidirectional", or "astar"
#' @return List with path_cells, total_cost, solve_time_ms, graph_build_time_ms, n_edges
#' @export
rust_corridor <- function(values, n_rows, n_cols, cell_width, cell_height, origin_cell, dest_cell, neighbours, method) .Call(wrap__rust_corridor, values, n_rows, n_cols, cell_width, cell_height, origin_cell, dest_cell, neighbours, method)

#' Build a cached corridor graph from a raster grid
#' @noRd
rust_corridor_build_graph <- function(values, n_rows, n_cols, cell_width, cell_height, neighbours) .Call(wrap__rust_corridor_build_graph, values, n_rows, n_cols, cell_width, cell_height, neighbours)

#' Solve on a cached corridor graph
#' @noRd
rust_corridor_solve_cached <- function(graph, origin_cell, dest_cell, method) .Call(wrap__rust_corridor_solve_cached, graph, origin_cell, dest_cell, method)

#' Get metadata from a cached corridor graph
#' @noRd
rust_corridor_graph_info <- function(graph) .Call(wrap__rust_corridor_graph_info, graph)


# nolint end

Try the spopt package in your browser

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

spopt documentation built on April 22, 2026, 9:07 a.m.