Skip to main content

sampling_planning/
sampler.rs

1//! Samplers: where planners draw new states from.
2
3mod informed;
4mod uniform_box;
5
6pub use informed::ProlateHyperspheroid;
7use rand_core::Rng;
8pub use uniform_box::{INFORMED_BOX_ATTEMPTS, UniformBox};
9
10use crate::StateSpace;
11
12/// Draws states for a planner.
13///
14/// `&mut self` lets stateful samplers (low-discrepancy sequences, informed samplers) advance.
15/// The RNG is borrowed, never owned, so one seeded RNG drives a whole planning run.
16pub trait Sampler<S> {
17    /// Draws one state.
18    fn sample<R: Rng + ?Sized>(&mut self, rng: &mut R) -> S;
19
20    /// Draws a state directly from the informed set
21    /// `{x : d(start, x) + d(x, goal) <= max_cost}` within this sampler's support, or returns
22    /// `None` to make the planner fall back to rejection sampling from [`Sampler::sample`].
23    ///
24    /// Informed planners ([`RrtStar`](crate::trees::RrtStar) with
25    /// [`informed`](crate::trees::RrtStarConfig::informed), BIT*) call this once a solution
26    /// exists. A returned state must lie in the informed set under `space`'s metric and be
27    /// distributed uniformly over the informed set intersected with the support of
28    /// [`Sampler::sample`]; otherwise the planner loses its optimality guarantees.
29    ///
30    /// When direct sampling does not apply (an unsupported space, or a set so large that
31    /// rejection is cheaper), return `None` **without** drawing from `rng`, so that the
32    /// fallback sees the same random stream as without this method. `None` after drawing is
33    /// allowed when an attempt budget runs out.
34    ///
35    /// The default returns `None`: rejection sampling, which works for every sampler and space.
36    /// [`UniformBox`] samples a [`ProlateHyperspheroid`] in Euclidean spaces.
37    fn sample_informed<Sp, R>(
38        &mut self,
39        space: &Sp,
40        rng: &mut R,
41        start: &S,
42        goal: &S,
43        max_cost: f64,
44    ) -> Option<S>
45    where
46        Sp: StateSpace<State = S>,
47        R: Rng + ?Sized,
48    {
49        let _ = (space, rng, start, goal, max_cost);
50
51        None
52    }
53}
54
55/// Draws a uniform `f64` in `[0, 1)` from one `next_u64` call.
56///
57/// Uses the top 52 bits as the mantissa of a float in `[1, 2)` and subtracts one, so the
58/// result is bit-for-bit reproducible on every platform.
59pub fn unit_f64<R: Rng + ?Sized>(rng: &mut R) -> f64 {
60    /// Bit pattern of `1.0_f64`: zero sign, biased exponent 1023, empty mantissa.
61    const ONE: u64 = 0x3FF0_0000_0000_0000;
62
63    f64::from_bits(ONE | (rng.next_u64() >> 12)) - 1.0
64}
65
66#[cfg(test)]
67mod tests {
68    use super::*;
69    use rand_core::SeedableRng;
70
71    #[test]
72    fn unit_f64_stays_in_half_open_unit_interval() {
73        let mut rng = rand_pcg::Pcg64Mcg::seed_from_u64(1);
74
75        for _ in 0..10_000 {
76            let x = unit_f64(&mut rng);
77            assert!((0.0..1.0).contains(&x));
78        }
79    }
80}