Skip to main content

Spars

Struct Spars 

Source
pub struct Spars<Sp: StateSpace, Sa, C, N = LinearScan<<Sp as StateSpace>::State>> { /* private fields */ }
Expand description

SPARS: an incremental sparse roadmap spanner that keeps a dense k-nearest PRM* graph next to the sparse one (Dobson and Bekris 2014, “Sparse roadmap spanners for asymptotically near-optimal motion planning”), ported from OMPL’s SPARS.

Every valid sample q first joins the dense graph D: it connects to its ceil(e (1 + 1/d) ln n) nearest dense nodes by valid motions, and gets a representative, the closest guard (ties by id) it can see within sparse_delta. Dense nodes joined by a dense edge shorter than dense_delta but represented by different guards v and v' lie on the interface of v and v'; each guard keeps, per other guard, the list of its dense nodes on that interface. Then q is tested against four criteria, in order, using the guards it can see within sparse_delta:

  1. Coverage: it sees no guard, so it becomes a guard.
  2. Connectivity: it sees guards of different components, so it becomes a guard joining them.
  3. Interface: its two closest guards are both visible but not adjacent, so they are connected directly, or through q.
  4. Quality: q lies on the interface of its representative v with a guard v'. For each neighbor v'' of v not adjacent to v', the estimate s_max of the sparse path from v' over v toward v'' is compared with the shortest dense path from q to a dense node on the interface of v and v''; if s_max exceeds stretch times its length, that dense path (extended by one dense node on each side) is added to the sparse graph as quality guards.

Construction ends after max_failures consecutive valid samples fail all four criteria. The roadmap is then, with high probability, a spanner of the dense graph: paths through it cost at most about stretch times the optimum plus a term in sparse_delta, with far fewer nodes than the dense graph. Spars::query searches only the sparse graph.

graph LR
    Sample --> Dense["add to D, update representatives"]
    Dense --> Coverage
    Coverage -->|fails| Connectivity
    Connectivity -->|fails| Interface
    Interface -->|fails| Quality["quality: interface of q, dense paths"]
    Quality -->|fails| Failure["failures += 1"]

The four criteria, their order, the representative and interface-list bookkeeping (addMilestone, calculateRepresentative, updateRepresentatives over the dense nodes within sparse_delta + dense_delta of each new guard), the loop nesting of checkAddPath and the failure counting (incremented only when all four tests fail, reset by every new guard, the direct interface edge and the zero-length path case) follow OMPL. Differences to OMPL’s SPARS, documented here so that results can be compared:

  • OMPL connects the first state of an added quality path to v'' and the last to v' (it passes them swapped to addPathToSpanner); here the first, which v' represents, connects to v' and the last to v''.
  • When q itself lies on the interface of v and v'', OMPL pushes q in front of the previous best dense path instead of replacing it; here the dense path is just q.
  • OMPL throws when a dense node has no interface neighbor with the expected representative (possible when the best dense path ends on another v'' interface, or when interface lists went stale); here that violation is skipped and the search goes on.
  • Every sparse edge of a quality path is checked before anything is added, and the path is skipped if one is invalid; OMPL adds them unchecked.
  • OMPL’s randomized reduceVertices is replaced by the deterministic greedy shortcut.
  • Dense paths come from Dijkstra (ties by id) instead of A*; their lengths agree.
  • A dense node without a visible guard has no representative (OMPL leaves an unset value); it is kept out of interface lists until a guard covers it, which the coverage criterion does in the same step.
  • Start and goal are not added as guards: Spars::query connects them to visible guards, so one roadmap answers many queries.
  • Invalid samples are drawn and dropped without counting as failures, as OMPL’s valid-state sampler retries until it finds a valid state.

Like OMPL’s, the quality test estimates the sparse path through v from guard distances alone (s_max), so an added path never makes the violation it repairs disappear; only the new guards taking over dense nodes do. Once many dense edges undercut dense_delta, every new quality path creates further violations and construction may not finish in practice, even at the default stretch 3 (on a 10 x 10 world with sparse_delta 2, from dense_delta of about 0.085 on; at stretch 1.2 already from about 0.06). Keep dense_delta small, as OMPL does, and bound Spars::construct by max_samples.

§Examples

use rand_core::SeedableRng;
use sampling_planning::{
    Euclidean, GoalRegion, UniformBox, Validity, ValidityChecker,
    roadmaps::{Spars, SparsConfig},
};

/// Free space everywhere.
struct Free;

impl ValidityChecker<Euclidean<2>> for Free {
    fn check_state(&self, _: &[f64; 2]) -> Validity {
        Validity::Valid
    }

    fn check_motion(&self, _: &Euclidean<2>, _: &[f64; 2], _: &[f64; 2]) -> Validity {
        Validity::Valid
    }
}

let sampler = UniformBox::new([0.0, 0.0], [10.0, 10.0]);
let mut config = SparsConfig::new(2.5, 0.05);
config.max_failures = 200;
let mut spars = Spars::new(Euclidean::<2>, sampler, Free, config).expect("valid");
spars.construct(100_000, &mut rand_pcg::Pcg64Mcg::seed_from_u64(1));

assert!(spars.is_complete());
assert!(spars.node_count() < spars.dense_node_count());
let path = spars.query(&[1.0, 1.0], &GoalRegion::new([9.0, 9.0], 0.5)).expect("valid start");
assert!(path.expect("free space").cost < 3.0 * 11.4);

Implementations§

Source§

impl<Sp, Sa, C> Spars<Sp, Sa, C>
where Sp: StateSpace, Sa: Sampler<Sp::State>, C: ValidityChecker<Sp>,

Source

pub fn new( space: Sp, sampler: Sa, checker: C, config: SparsConfig, ) -> Result<Self, Error>

Creates an empty roadmap with LinearScan indices over its guards and dense nodes.

§Errors

Error::InvalidRadius if a range is not positive and finite, dense_delta is not below sparse_delta, or the stretch is not above one.

Source§

impl<Sp, Sa, C, N> Spars<Sp, Sa, C, N>
where Sp: StateSpace, Sa: Sampler<Sp::State>, C: ValidityChecker<Sp>, N: NearestNeighbors<Sp> + Clone,

Source

pub fn with_nearest_neighbors( space: Sp, sampler: Sa, checker: C, config: SparsConfig, nn: N, ) -> Result<Self, Error>

Creates an empty roadmap with the given empty index; a clone of it indexes the dense nodes, the original the guards.

§Errors

The same as Spars::new.

Source§

impl<Sp, Sa, C, N> Spars<Sp, Sa, C, N>
where Sp: StateSpace, Sa: Sampler<Sp::State>, C: ValidityChecker<Sp>, N: NearestNeighbors<Sp>,

Source

pub fn construct<R: Rng + ?Sized>( &mut self, max_samples: usize, rng: &mut R, ) -> usize

Processes samples until construction is complete or max_samples were drawn, valid or not. Returns the number of samples drawn.

Source

pub fn step<R: Rng + ?Sized>(&mut self, rng: &mut R) -> bool

Draws one sample; if it is valid, adds it to the dense graph and applies the four criteria. Returns whether the sparse roadmap changed.

Source§

impl<Sp, Sa, C, N> Spars<Sp, Sa, C, N>
where Sp: StateSpace, C: ValidityChecker<Sp>, N: NearestNeighbors<Sp>,

Source

pub fn query( &self, start: &Sp::State, goal: &GoalRegion<Sp::State>, ) -> Result<Option<Path<Sp::State>>, Error>

The cheapest path from start to goal through the sparse roadmap, or None if the roadmap does not connect them. Both endpoints connect to every visible guard within sparse_delta; the dense graph is not searched.

§Errors

Error::InvalidStart if start does not check as valid.

Source

pub const fn is_complete(&self) -> bool

Whether max_failures consecutive valid samples failed all four criteria.

Source

pub fn node_count(&self) -> usize

The number of guards (sparse nodes).

Source

pub fn dense_node_count(&self) -> usize

The number of dense nodes: every valid sample drawn so far.

Source

pub fn edge_count(&self) -> usize

The number of sparse roadmap edges.

Source

pub fn guard_counts(&self) -> [usize; 4]

The number of guards added for each criterion: coverage, connectivity, interface, quality.

Trait Implementations§

Source§

impl<Sp: Clone + StateSpace, Sa: Clone, C: Clone, N: Clone> Clone for Spars<Sp, Sa, C, N>
where Sp::State: Clone,

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<Sp: Debug + StateSpace, Sa: Debug, C: Debug, N: Debug> Debug for Spars<Sp, Sa, C, N>
where Sp::State: Debug,

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<Sp, Sa, C, N> Forget for Spars<Sp, Sa, C, N>

§

impl<Sp, Sa, C, N> Freeze for Spars<Sp, Sa, C, N>
where Sp: Freeze, Sa: Freeze, C: Freeze, SparseGraph<<Sp as StateSpace>::State, N>: Freeze, Vec<<Sp as StateSpace>::State>: Freeze, N: Freeze,

§

impl<Sp, Sa, C, N> Move for Spars<Sp, Sa, C, N>

§

impl<Sp, Sa, C, N> RefUnwindSafe for Spars<Sp, Sa, C, N>

§

impl<Sp, Sa, C, N> Send for Spars<Sp, Sa, C, N>
where Sp: Send, N: Send, C: Send, Sa: Send, SparseGraph<<Sp as StateSpace>::State, N>: Send, Vec<<Sp as StateSpace>::State>: Send,

§

impl<Sp, Sa, C, N> Sync for Spars<Sp, Sa, C, N>
where Sp: Sync, N: Sync, C: Sync, Sa: Sync, SparseGraph<<Sp as StateSpace>::State, N>: Sync, Vec<<Sp as StateSpace>::State>: Sync,

§

impl<Sp, Sa, C, N> Unpin for Spars<Sp, Sa, C, N>
where Sp: Unpin, N: Unpin, C: Unpin, Sa: Unpin, SparseGraph<<Sp as StateSpace>::State, N>: Unpin, Vec<<Sp as StateSpace>::State>: Unpin,

§

impl<Sp, Sa, C, N> UnsafeUnpin for Spars<Sp, Sa, C, N>
where Sp: UnsafeUnpin, Sa: UnsafeUnpin, C: UnsafeUnpin, SparseGraph<<Sp as StateSpace>::State, N>: UnsafeUnpin, Vec<<Sp as StateSpace>::State>: UnsafeUnpin, N: UnsafeUnpin,

§

impl<Sp, Sa, C, N> UnwindSafe for Spars<Sp, Sa, C, N>
where Sp: UnwindSafe, Sa: UnwindSafe, C: UnwindSafe, SparseGraph<<Sp as StateSpace>::State, N>: UnwindSafe, Vec<<Sp as StateSpace>::State>: UnwindSafe, N: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.