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:
- Coverage: it sees no guard, so it becomes a guard.
- Connectivity: it sees guards of different components, so it becomes a guard joining them.
- Interface: its two closest guards are both visible but not adjacent, so they are
connected directly, or through
q. - Quality:
qlies on the interface of its representativevwith a guardv'. For each neighborv''ofvnot adjacent tov', the estimates_maxof the sparse path fromv'overvtowardv''is compared with the shortest dense path fromqto a dense node on the interface ofvandv''; ifs_maxexceedsstretchtimes 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 tov'(it passes them swapped toaddPathToSpanner); here the first, whichv'represents, connects tov'and the last tov''. - When
qitself lies on the interface ofvandv'', OMPL pushesqin front of the previous best dense path instead of replacing it; here the dense path is justq. - 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
reduceVerticesis replaced by the deterministic greedyshortcut. - 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::queryconnects 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>
impl<Sp, Sa, C> Spars<Sp, Sa, C>
Sourcepub fn new(
space: Sp,
sampler: Sa,
checker: C,
config: SparsConfig,
) -> Result<Self, Error>
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,
impl<Sp, Sa, C, N> Spars<Sp, Sa, C, N>where
Sp: StateSpace,
Sa: Sampler<Sp::State>,
C: ValidityChecker<Sp>,
N: NearestNeighbors<Sp> + Clone,
Sourcepub fn with_nearest_neighbors(
space: Sp,
sampler: Sa,
checker: C,
config: SparsConfig,
nn: N,
) -> Result<Self, Error>
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>
impl<Sp, Sa, C, N> Spars<Sp, Sa, C, N>
Source§impl<Sp, Sa, C, N> Spars<Sp, Sa, C, N>
impl<Sp, Sa, C, N> Spars<Sp, Sa, C, N>
Sourcepub fn query(
&self,
start: &Sp::State,
goal: &GoalRegion<Sp::State>,
) -> Result<Option<Path<Sp::State>>, Error>
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.
Sourcepub const fn is_complete(&self) -> bool
pub const fn is_complete(&self) -> bool
Whether max_failures consecutive valid samples failed all four criteria.
Sourcepub fn node_count(&self) -> usize
pub fn node_count(&self) -> usize
The number of guards (sparse nodes).
Sourcepub fn dense_node_count(&self) -> usize
pub fn dense_node_count(&self) -> usize
The number of dense nodes: every valid sample drawn so far.
Sourcepub fn edge_count(&self) -> usize
pub fn edge_count(&self) -> usize
The number of sparse roadmap edges.
Sourcepub fn guard_counts(&self) -> [usize; 4]
pub fn guard_counts(&self) -> [usize; 4]
The number of guards added for each criterion: coverage, connectivity, interface, quality.