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