pub fn shortcut_random<Sp, C, R>(
space: &Sp,
checker: &C,
path: &Path<Sp::State>,
max_edge: f64,
attempts: usize,
rng: &mut R,
) -> Path<Sp::State>Expand description
Randomized shortcutting: attempts times, picks two random points along the path and
replaces the piece between them by a direct motion if that is valid, at most max_edge
long and strictly cheaper.
Unlike shortcut the endpoints of a shortcut lie anywhere on the path, not only at its
states, so it also straightens corners. All three motions of a splice are checked: from
the state before the cut to its first point, across the cut, and from its second point to
the state after it. Run shortcut afterward to drop states that became redundant.
ยงExamples
use rand_core::SeedableRng;
use sampling_planning::{Euclidean, Path, Validity, ValidityChecker, shortcut_random};
/// 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 corner = Path { states: vec![[0.0, 0.0], [2.0, 0.0], [2.0, 2.0]], cost: 4.0 };
let mut rng = rand_pcg::Pcg64Mcg::seed_from_u64(1);
let smoothed = shortcut_random(&Euclidean::<2>, &Free, &corner, f64::INFINITY, 50, &mut rng);
assert!(smoothed.cost < 3.0);