Skip to main content

fieldopt_core/
lib.rs

1//! # fieldopt-core
2//!
3//! Field-survey design under real field cost. The crate provides the
4//! computational pieces behind the R package `fieldopt`: travel matrices,
5//! GRASP routing of field work (with service times and route limits),
6//! spatially balanced sampling with the local pivotal method, balanced
7//! sampling with the cube method and the Voronoi measure of spatial balance,
8//! Horvitz-Thompson estimation with the local-mean variance estimator, and
9//! cost-aware allocation across strata or frames, for one or several study
10//! variables, in dual frames and in two stages. It has no dependencies and
11//! every randomised step is reproducible from a seed.
12//!
13//! ```
14//! use fieldopt_core::distance::euclidean_matrix;
15//! use fieldopt_core::routing::{grasp_routes, GraspOptions, RouteLimits};
16//! use fieldopt_core::sampling::{inclusion_probabilities, local_pivotal};
17//!
18//! // a 6 x 6 grid of segments plus a depot at the origin
19//! let mut x = vec![0.0]; let mut y = vec![0.0];
20//! for i in 0..36 { x.push(1.0 + (i % 6) as f64); y.push(1.0 + (i / 6) as f64); }
21//! let m = euclidean_matrix(&x, &y).unwrap();
22//!
23//! // select 12 segments, spatially balanced, with equal probabilities
24//! let coords: Vec<f64> = (1..37).flat_map(|i| vec![x[i], y[i]]).collect();
25//! let pi = inclusion_probabilities(&vec![1.0; 36], 12).unwrap();
26//! let sampled = local_pivotal(&coords, 2, &pi, 1).unwrap();
27//! let units: Vec<usize> = (0..36).filter(|&i| sampled[i]).map(|i| i + 1).collect();
28//!
29//! // route the field work from the depot with at most 5 segments per day
30//! let limits = RouteLimits { max_length: f64::INFINITY, max_stops: 5 };
31//! let sol = grasp_routes(&m, 0, &units, limits, GraspOptions::default()).unwrap();
32//! assert_eq!(sol.routes.iter().map(|r| r.len()).sum::<usize>(), 12);
33//! assert!(sol.total >= sol.lower_bound);
34//! ```
35
36/// Version of this crate, for reports.
37pub const VERSION: &str = env!("CARGO_PKG_VERSION");
38
39pub mod allocation;
40pub mod balance;
41pub mod distance;
42pub mod dualframe;
43pub mod estimation;
44pub mod rng;
45pub mod routing;
46pub mod sampling;
47pub mod spatial;
48pub mod twostage;
49
50pub use allocation::{
51    allocate_for_budget, allocate_for_variance, allocate_multivariate, Allocation, MultiAllocation,
52    Stratum,
53};
54pub use balance::{cube, spatial_balance};
55pub use distance::{euclidean_matrix, haversine_matrix, Matrix};
56pub use dualframe::{dual_frame_allocation, Domain, DualFrame, DualFrameAllocation, Target};
57pub use estimation::{ht_total, local_mean_variance, srs_variance};
58pub use routing::{
59    grasp_routes, grasp_routes_with_service, route_duration, route_length, GraspOptions,
60    RouteLimits, Solution,
61};
62pub use sampling::{inclusion_probabilities, local_pivotal};
63pub use spatial::{hilbert_order, systematic_replicates};
64pub use twostage::{two_stage_for_budget, two_stage_for_variance, TwoStage, TwoStageAllocation};