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, spatially balanced sampling with the local
6//! pivotal method, Horvitz-Thompson estimation with the local-mean variance
7//! estimator, and cost-aware allocation across strata or frames. It has no
8//! dependencies and every randomised step is reproducible from a seed.
9//!
10//! ```
11//! use fieldopt_core::distance::euclidean_matrix;
12//! use fieldopt_core::routing::{grasp_routes, GraspOptions, RouteLimits};
13//! use fieldopt_core::sampling::{inclusion_probabilities, local_pivotal};
14//!
15//! // a 6 x 6 grid of segments plus a depot at the origin
16//! let mut x = vec![0.0]; let mut y = vec![0.0];
17//! for i in 0..36 { x.push(1.0 + (i % 6) as f64); y.push(1.0 + (i / 6) as f64); }
18//! let m = euclidean_matrix(&x, &y).unwrap();
19//!
20//! // select 12 segments, spatially balanced, with equal probabilities
21//! let coords: Vec<f64> = (1..37).flat_map(|i| vec![x[i], y[i]]).collect();
22//! let pi = inclusion_probabilities(&vec![1.0; 36], 12).unwrap();
23//! let sampled = local_pivotal(&coords, 2, &pi, 1).unwrap();
24//! let units: Vec<usize> = (0..36).filter(|&i| sampled[i]).map(|i| i + 1).collect();
25//!
26//! // route the field work from the depot with at most 5 segments per day
27//! let limits = RouteLimits { max_length: f64::INFINITY, max_stops: 5 };
28//! let sol = grasp_routes(&m, 0, &units, limits, GraspOptions::default()).unwrap();
29//! assert_eq!(sol.routes.iter().map(|r| r.len()).sum::<usize>(), 12);
30//! assert!(sol.total >= sol.lower_bound);
31//! ```
32
33pub mod allocation;
34pub mod distance;
35pub mod dualframe;
36pub mod estimation;
37pub mod rng;
38pub mod routing;
39pub mod sampling;
40pub mod spatial;
41pub mod twostage;
42
43pub use allocation::{allocate_for_budget, allocate_for_variance, Allocation, Stratum};
44pub use distance::{euclidean_matrix, haversine_matrix, Matrix};
45pub use dualframe::{dual_frame_allocation, Domain, DualFrame, DualFrameAllocation, Target};
46pub use estimation::{ht_total, local_mean_variance, srs_variance};
47pub use routing::{grasp_routes, route_length, GraspOptions, RouteLimits, Solution};
48pub use sampling::{inclusion_probabilities, local_pivotal};
49pub use spatial::{hilbert_order, systematic_replicates};
50pub use twostage::{two_stage_for_budget, two_stage_for_variance, TwoStage, TwoStageAllocation};