pub struct Kpiece<Sp, Sa, C, P, Pr, const K: usize>where
Sp: StateSpace,
P: Propagator<Sp>,{ /* private fields */ }Expand description
KPIECE: grows a tree of motions by forward propagation, guided by a grid over a projection of the state space (Sucan and Kavraki 2008, 2012).
Each motion’s end state falls into a grid cell. Cells with all 2K axis neighbors are
interior, the others exterior, the frontier of exploration. Each iteration:
graph LR
Select["select cell: exterior with prob. max(bias, share)"] --> Motion["pick motion, newest preferred"]
Motion --> Propagate["random control, random steps, stop at first invalid step"]
Propagate -->|long enough| Split["split at cell changes, add pieces"]
Propagate --> Score["scale cell score by good or bad factor"]Cells are ranked by importance score / ((neighbors + 1) coverage selections); a new cell
starts with score (1 + ln i) / (1e-3 + goal distance). Within a cell, motions are picked
from a half-normal distribution centered on the newest one. This follows OMPL’s
control::KPIECE1 with a single grid level, which also expands from the end state of the
chosen motion rather than a random state along it. With probability goal_bias one of the
close_samples motions closest to the goal is expanded instead.
KPIECE is probabilistically complete but not asymptotically optimal: Kpiece::solve
stops at the first motion that reaches the goal region. It needs no state sampler; the
problem’s sampler is not used.
§Examples
use rand_core::{Rng, SeedableRng};
use sampling_planning::{
Euclidean, GoalRegion, UniformBox, Validity, ValidityChecker, unit_f64,
kinodynamic::{KinodynamicProblem, Kpiece, KpieceConfig, Propagator},
};
/// 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
}
}
/// A point that moves with a velocity of at most 1 per axis.
struct Velocity;
impl Propagator<Euclidean<2>> for Velocity {
type Control = [f64; 2];
fn control_dimension(&self) -> usize {
2
}
fn sample_control<R: Rng + ?Sized>(&mut self, rng: &mut R) -> [f64; 2] {
[2.0 * unit_f64(rng) - 1.0, 2.0 * unit_f64(rng) - 1.0]
}
fn propagate(&self, _: &Euclidean<2>, from: &[f64; 2], u: &[f64; 2], dt: f64) -> [f64; 2] {
[from[0] + u[0] * dt, from[1] + u[1] * dt]
}
}
let problem = KinodynamicProblem {
space: Euclidean::<2>,
sampler: UniformBox::new([0.0, 0.0], [10.0, 10.0]),
checker: Free,
propagator: Velocity,
start: [1.0, 1.0],
goal: GoalRegion::new([9.0, 9.0], 0.5),
};
// The identity projection with half-unit cells.
let projection = |state: &[f64; 2]| *state;
let config = KpieceConfig::new([0.5, 0.5], 0.1, 10);
let mut kpiece = Kpiece::new(problem, projection, config).expect("valid");
let trajectory = kpiece
.solve(20_000, &mut rand_pcg::Pcg64Mcg::seed_from_u64(1))
.expect("free space");
assert_eq!(trajectory.controls.len() + 1, trajectory.states.len());Implementations§
Source§impl<Sp, Sa, C, P, Pr, const K: usize> Kpiece<Sp, Sa, C, P, Pr, K>
impl<Sp, Sa, C, P, Pr, const K: usize> Kpiece<Sp, Sa, C, P, Pr, K>
Sourcepub fn new(
problem: KinodynamicProblem<Sp, Sa, C, P>,
projection: Pr,
config: KpieceConfig<K>,
) -> Result<Self, Error>
pub fn new( problem: KinodynamicProblem<Sp, Sa, C, P>, projection: Pr, config: KpieceConfig<K>, ) -> Result<Self, Error>
Creates a planner whose tree holds only the start.
§Errors
Error::InvalidStartif the start state is not valid.Error::GoalBiasOutOfRangeifgoal_biasis not in[0, 1].Error::InvalidRadiusif a cell size is not positive and finite.Error::InvalidPropagationif the step size is not positive and finite or the step range is empty.Error::InvalidScheduleif a score factor is not in(0, 1]orexterior_biasnot in[0, 1].
Sourcepub fn solve<R: Rng + ?Sized>(
&mut self,
max_iterations: usize,
rng: &mut R,
) -> Option<Trajectory<Sp::State, P::Control>>
pub fn solve<R: Rng + ?Sized>( &mut self, max_iterations: usize, rng: &mut R, ) -> Option<Trajectory<Sp::State, P::Control>>
Runs iterations until a motion reaches the goal region or max_iterations have run, and
returns the solution, if any.
Sourcepub fn step<R: Rng + ?Sized>(&mut self, rng: &mut R) -> bool
pub fn step<R: Rng + ?Sized>(&mut self, rng: &mut R) -> bool
Runs one iteration. Returns whether motions were added.
Sourcepub fn best_trajectory(&self) -> Option<Trajectory<Sp::State, P::Control>>
pub fn best_trajectory(&self) -> Option<Trajectory<Sp::State, P::Control>>
The first trajectory found into the goal region, or None if none was found yet.
Sourcepub fn motion_count(&self) -> usize
pub fn motion_count(&self) -> usize
The number of motions in the tree, including the start.
Sourcepub fn cell_count(&self) -> usize
pub fn cell_count(&self) -> usize
The number of instantiated grid cells.