use rand_core::SeedableRng;
use rand_pcg::Pcg64Mcg;
use sampling_planning::{
Dubins, GoalRegion, Path, Problem, ReedsShepp, Se2, Se2State, StateSpace, StraightLine,
UniformSe2, Validity, ValidityChecker, discretized_motion,
trees::{Rrt, RrtConfig, RrtStar, RrtStarConfig},
};
#[derive(Debug, Clone, Copy)]
struct Yard;
impl Yard {
fn check(state: &Se2State) -> Validity {
let inside = (0.0..=10.0).contains(&state.x) && (0.0..=10.0).contains(&state.y);
let wall = (4.0..=6.0).contains(&state.x) && state.y <= 6.0;
if inside && !wall {
Validity::Valid
} else {
Validity::Invalid
}
}
}
impl<Sp: StateSpace<State = Se2State>> ValidityChecker<Sp> for Yard {
fn check_state(&self, state: &Se2State) -> Validity {
Self::check(state)
}
fn check_motion(&self, space: &Sp, from: &Se2State, to: &Se2State) -> Validity {
discretized_motion(space, from, to, 0.05, Self::check)
}
}
fn yard<Sp: StateSpace<State = Se2State>>(
space: Sp,
) -> Problem<Sp, UniformSe2, Yard, StraightLine> {
Problem {
space,
sampler: UniformSe2::new([0.0, 0.0], [10.0, 10.0]),
checker: Yard,
steering: StraightLine::new(1.0),
start: Se2State::new(1.0, 1.0, core::f64::consts::FRAC_PI_2),
goal: GoalRegion::new(Se2State::new(9.0, 1.0, -core::f64::consts::FRAC_PI_2), 0.3),
}
}
fn assert_solves<Sp: StateSpace<State = Se2State> + Clone>(space: &Sp, path: &Path<Se2State>) {
let problem = yard(space.clone());
assert_eq!(path.states.first(), Some(&problem.start));
assert!(
problem
.goal
.contains(space, path.states.last().expect("non-empty"))
);
let mut length = 0.0;
for pair in path.states.windows(2) {
let validity = Yard.check_motion(space, &pair[0], &pair[1]);
assert_eq!(validity, Validity::Valid, "{pair:?}");
length += space.distance(&pair[0], &pair[1]);
}
assert!(
(length - path.cost).abs() < 1e-9,
"{length} vs {}",
path.cost
);
}
fn star_config() -> RrtStarConfig {
RrtStarConfig::new(30.0, 1.0)
}
#[test]
fn rrt_star_plans_over_se2() {
let space = Se2::new(0.5);
let mut planner = RrtStar::new(yard(space), star_config()).expect("valid problem");
planner.run(3000, &mut Pcg64Mcg::seed_from_u64(1));
let path = planner.best_path().expect("RRT* solves the SE(2) yard");
assert_solves(&space, &path);
}
#[test]
fn rrt_plans_forward_only_over_dubins() {
let space = Dubins::new(0.8);
let mut planner = Rrt::new(yard(space), RrtConfig::default()).expect("valid problem");
let path = planner
.solve(20_000, &mut Pcg64Mcg::seed_from_u64(2))
.expect("RRT solves the Dubins yard");
assert_solves(&space, &path);
}
#[test]
fn rrt_star_plans_over_reeds_shepp_and_improves() {
let space = ReedsShepp::new(0.8);
let mut planner = RrtStar::new(yard(space), star_config()).expect("valid problem");
let mut rng = Pcg64Mcg::seed_from_u64(3);
let mut costs = Vec::new();
for _ in 0..4 {
planner.run(200, &mut rng);
costs.extend(planner.best_path().map(|path| path.cost));
}
let path = planner
.best_path()
.expect("RRT* solves the Reeds-Shepp yard");
assert_solves(&space, &path);
assert!(costs.windows(2).all(|pair| pair[1] <= pair[0]), "{costs:?}");
}