mod nodes;
use alloc::vec::Vec;
use rand_core::Rng;
use super::{Error, KinodynamicProblem, Propagator, Trajectory};
use crate::radius::count_to_f64;
use crate::{LinearScan, RemovableNeighbors, Sampler, StateSpace, ValidityChecker, unit_f64};
use nodes::{Node, Nodes};
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct SstStarSchedule {
pub shrink: f64,
pub first_epoch: usize,
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct SstConfig {
pub goal_bias: f64,
pub selection_radius: f64,
pub pruning_radius: f64,
pub step_size: f64,
pub max_steps: usize,
pub star: Option<SstStarSchedule>,
}
impl SstConfig {
#[must_use]
pub const fn new(
selection_radius: f64,
pruning_radius: f64,
step_size: f64,
max_steps: usize,
) -> Self {
Self {
goal_bias: 0.05,
selection_radius,
pruning_radius,
step_size,
max_steps,
star: None,
}
}
#[must_use]
pub const fn star(self, schedule: SstStarSchedule) -> Self {
Self {
star: Some(schedule),
..self
}
}
fn validate(&self) -> Result<(), Error> {
let positive = |value: f64| value.is_finite() && value > 0.0;
if !(0.0..=1.0).contains(&self.goal_bias) {
return Err(Error::GoalBiasOutOfRange);
}
if !(positive(self.selection_radius) && positive(self.pruning_radius)) {
return Err(Error::InvalidRadius);
}
if !positive(self.step_size) || self.max_steps == 0 {
return Err(Error::InvalidPropagation);
}
match self.star {
Some(s) if !(s.shrink > 0.0 && s.shrink < 1.0) || s.first_epoch == 0 => {
Err(Error::InvalidSchedule)
}
_ => Ok(()),
}
}
}
#[derive(Debug, Clone)]
struct Witness<S> {
state: S,
representative: usize,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum WitnessSlot {
Existing(usize),
New,
}
#[derive(Debug, Clone, Copy)]
struct Epoch {
index: usize,
end: usize,
}
#[derive(Clone)]
pub struct Sst<Sp, Sa, C, P, N = LinearScan<<Sp as StateSpace>::State>>
where
Sp: StateSpace,
P: Propagator<Sp>,
{
problem: KinodynamicProblem<Sp, Sa, C, P>,
config: SstConfig,
nodes: Nodes<Sp::State, P::Control>,
active: N,
witnesses: Vec<Witness<Sp::State>>,
witness_nn: N,
best_goal: Option<usize>,
iteration: usize,
epoch: Epoch,
near: Vec<usize>,
}
impl<Sp, Sa, C, P> Sst<Sp, Sa, C, P>
where
Sp: StateSpace,
Sa: Sampler<Sp::State>,
C: ValidityChecker<Sp>,
P: Propagator<Sp>,
{
pub fn new(
problem: KinodynamicProblem<Sp, Sa, C, P>,
config: SstConfig,
) -> Result<Self, Error> {
Self::with_nearest_neighbors(problem, config, LinearScan::default())
}
}
impl<Sp, Sa, C, P, N> Sst<Sp, Sa, C, P, N>
where
Sp: StateSpace,
Sa: Sampler<Sp::State>,
C: ValidityChecker<Sp>,
P: Propagator<Sp>,
N: RemovableNeighbors<Sp> + Clone,
{
pub fn with_nearest_neighbors(
problem: KinodynamicProblem<Sp, Sa, C, P>,
config: SstConfig,
nn: N,
) -> Result<Self, Error> {
config.validate()?;
let validity = problem.checker.check_state(&problem.start);
if !validity.is_valid() {
return Err(Error::InvalidStart(validity));
}
let start = problem.start.clone();
let mut active = nn.clone();
let mut witness_nn = nn;
active.insert(0, start.clone());
witness_nn.insert(0, start.clone());
Ok(Self {
best_goal: problem.goal.contains(&problem.space, &start).then_some(0),
nodes: Nodes::new(Node {
state: start.clone(),
parent: None,
control: None,
cost: 0.0,
children: 0,
active: true,
}),
problem,
active,
witnesses: alloc::vec![Witness {
state: start.clone(),
representative: 0,
}],
witness_nn,
iteration: 0,
epoch: Epoch {
index: 0,
end: config.star.map_or(usize::MAX, |s| s.first_epoch),
},
config,
near: Vec::new(),
})
}
pub fn step<R: Rng + ?Sized>(&mut self, rng: &mut R) -> bool {
self.advance_schedule();
self.iteration += 1;
let target = self.draw(rng);
let parent = self.select(&target);
let Some((state, control, duration)) = self.propagate(parent, rng) else {
return false;
};
let cost = self.nodes[parent].cost + duration;
let Some(slot) = self.witness_slot(&state, cost) else {
return false;
};
self.add(state, parent, (control, duration), cost, slot);
true
}
pub fn run<R: Rng + ?Sized>(&mut self, iterations: usize, rng: &mut R) {
for _ in 0..iterations {
self.step(rng);
}
}
fn advance_schedule(&mut self) {
let Some(schedule) = self.config.star else {
return;
};
if self.iteration < self.epoch.end {
return;
}
self.config.selection_radius *= schedule.shrink;
self.config.pruning_radius *= schedule.shrink;
self.epoch.index += 1;
self.epoch.end = self
.epoch
.end
.saturating_add(self.epoch_length(schedule, self.epoch.index));
}
fn epoch_length(&self, schedule: SstStarSchedule, j: usize) -> usize {
let degrees = self.problem.space.dimension() + self.problem.propagator.control_dimension();
let exponent = -count_to_f64(degrees + 1) * count_to_f64(j);
let length = (1.0 + libm::log(count_to_f64(j)))
* libm::pow(schedule.shrink, exponent)
* count_to_f64(schedule.first_epoch);
saturating_count(length)
}
fn draw<R: Rng + ?Sized>(&mut self, rng: &mut R) -> Sp::State {
if unit_f64(rng) < self.config.goal_bias {
self.problem.goal.center().clone()
} else {
self.problem.sampler.sample(rng)
}
}
fn select(&mut self, target: &Sp::State) -> usize {
let space = &self.problem.space;
self.active
.within_radius(space, target, self.config.selection_radius, &mut self.near);
let mut best: Option<usize> = None;
for &id in &self.near {
if best.is_none_or(|b| self.nodes[id].cost < self.nodes[b].cost) {
best = Some(id);
}
}
best.unwrap_or_else(|| {
self.active
.nearest(space, target)
.expect("every witness keeps one node active")
})
}
fn propagate<R: Rng + ?Sized>(
&mut self,
from: usize,
rng: &mut R,
) -> Option<(Sp::State, P::Control, f64)> {
let control = self.problem.propagator.sample_control(rng);
let steps = random_step_count(self.config.max_steps, rng);
let (space, propagator) = (&self.problem.space, &self.problem.propagator);
let mut state = self.nodes[from].state.clone();
for _ in 0..steps {
let next = propagator.propagate(space, &state, &control, self.config.step_size);
if !self
.problem
.checker
.check_motion(space, &state, &next)
.is_valid()
{
return None;
}
state = next;
}
let duration = count_to_f64(steps) * self.config.step_size;
Some((state, control, duration))
}
fn witness_slot(&self, state: &Sp::State, cost: f64) -> Option<WitnessSlot> {
let space = &self.problem.space;
let witness = self
.witness_nn
.nearest(space, state)
.expect("the start is always a witness");
let Witness {
state: point,
representative,
} = &self.witnesses[witness];
if space.distance(state, point) > self.config.pruning_radius {
return Some(WitnessSlot::New);
}
(cost < self.nodes[*representative].cost).then_some(WitnessSlot::Existing(witness))
}
fn add(
&mut self,
state: Sp::State,
parent: usize,
control: (P::Control, f64),
cost: f64,
slot: WitnessSlot,
) {
let id = self.nodes.next_id();
let previous = self.claim_witness(slot, id, &state);
let reaches_goal = self.problem.goal.contains(&self.problem.space, &state);
self.active.insert(id, state.clone());
self.nodes.push(Node {
state,
parent: Some(parent),
control: Some(control),
cost,
children: 0,
active: true,
});
if let Some(previous) = previous {
self.deactivate(previous);
}
if reaches_goal
&& self
.best_goal
.is_none_or(|best| cost < self.nodes[best].cost)
{
if let Some(old) = self.best_goal.replace(id) {
self.free_inactive_branch(old);
}
}
}
fn claim_witness(&mut self, slot: WitnessSlot, id: usize, state: &Sp::State) -> Option<usize> {
match slot {
WitnessSlot::Existing(witness) => Some(core::mem::replace(
&mut self.witnesses[witness].representative,
id,
)),
WitnessSlot::New => {
self.witness_nn.insert(self.witnesses.len(), state.clone());
self.witnesses.push(Witness {
state: state.clone(),
representative: id,
});
None
}
}
}
fn deactivate(&mut self, id: usize) {
self.active.remove(id);
self.nodes[id].active = false;
self.free_inactive_branch(id);
}
fn free_inactive_branch(&mut self, id: usize) {
let mut current = Some(id);
while let Some(node) = current {
let entry = &self.nodes[node];
let removable =
node != 0 && !entry.active && entry.children == 0 && self.best_goal != Some(node);
if !removable {
return;
}
current = self.nodes.free(node);
}
}
#[must_use]
pub fn best_trajectory(&self) -> Option<Trajectory<Sp::State, P::Control>> {
let goal = self.best_goal?;
let mut states = Vec::new();
let mut controls = Vec::new();
let mut current = Some(goal);
while let Some(id) = current {
let node = &self.nodes[id];
states.push(node.state.clone());
controls.extend(node.control.clone());
current = node.parent;
}
states.reverse();
controls.reverse();
Some(Trajectory {
states,
controls,
cost: self.nodes[goal].cost,
})
}
#[must_use]
pub fn node_count(&self) -> usize {
self.nodes.len()
}
#[must_use]
pub fn active_count(&self) -> usize {
self.active.len()
}
#[must_use]
pub const fn radii(&self) -> (f64, f64) {
(self.config.selection_radius, self.config.pruning_radius)
}
}
pub(super) fn random_step_count<R: Rng + ?Sized>(max_steps: usize, rng: &mut R) -> usize {
let max = u64::try_from(max_steps).unwrap_or(u64::MAX);
let offset = usize::try_from(rng.next_u64() % max).expect("below max_steps");
offset + 1
}
fn saturating_count(value: f64) -> usize {
if value.is_nan() || value < 1.0 {
0
} else if value >= f64::from(u32::MAX) {
usize::try_from(u32::MAX).unwrap_or(usize::MAX)
} else {
#[expect(
clippy::cast_possible_truncation,
clippy::cast_sign_loss,
reason = "value is in [1, u32::MAX) after the guards above"
)]
let count = value as u32;
usize::try_from(count).unwrap_or(usize::MAX)
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::{Euclidean, GoalRegion, NearestNeighbors, Validity};
use rand_core::SeedableRng;
struct Replay(Vec<f64>, usize);
impl Replay {
fn next(&mut self) -> f64 {
self.1 += 1;
self.0[(self.1 - 1) % self.0.len()]
}
}
impl Sampler<[f64; 1]> for Replay {
fn sample<R: Rng + ?Sized>(&mut self, _: &mut R) -> [f64; 1] {
[self.next()]
}
}
impl Propagator<Euclidean<1>> for Replay {
type Control = f64;
fn control_dimension(&self) -> usize {
1
}
fn sample_control<R: Rng + ?Sized>(&mut self, _: &mut R) -> f64 {
self.next()
}
fn propagate(&self, _: &Euclidean<1>, from: &[f64; 1], u: &f64, dt: f64) -> [f64; 1] {
[from[0] + u * dt]
}
}
struct Free;
impl ValidityChecker<Euclidean<1>> for Free {
fn check_state(&self, _: &[f64; 1]) -> Validity {
Validity::Valid
}
fn check_motion(&self, _: &Euclidean<1>, _: &[f64; 1], _: &[f64; 1]) -> Validity {
Validity::Valid
}
}
#[test]
fn hand_checked_witness_rules() {
let problem = KinodynamicProblem {
space: Euclidean::<1>,
sampler: Replay(alloc::vec![0.0, 2.0, 2.0, 0.0], 0),
checker: Free,
propagator: Replay(alloc::vec![2.0, 2.0, 0.2, 4.1], 0),
start: [0.0],
goal: GoalRegion::new([100.0], 0.1),
};
let mut config = SstConfig::new(0.1, 0.5, 1.0, 1);
config.goal_bias = 0.0;
let mut sst = Sst::new(problem, config).expect("valid");
let mut rng = rand_pcg::Pcg64Mcg::seed_from_u64(1);
assert!(sst.step(&mut rng));
assert!(sst.step(&mut rng));
assert!(!sst.step(&mut rng));
assert_eq!((sst.node_count(), sst.active_count()), (3, 3));
assert!(sst.step(&mut rng));
assert_eq!((sst.node_count(), sst.active_count()), (3, 3));
assert_eq!(sst.nodes.next_id(), 2, "the freed id is reused next");
assert_eq!(sst.witnesses.len(), 3);
assert_eq!(sst.witnesses[2].representative, 3);
assert_eq!(sst.active.nearest(&Euclidean::<1>, &[4.0]), Some(3));
}
#[test]
fn the_best_goal_node_survives_deactivation() {
let problem = KinodynamicProblem {
space: Euclidean::<1>,
sampler: Replay(alloc::vec![0.0, 1.0, 0.0], 0),
checker: Free,
propagator: Replay(alloc::vec![1.0, 1.0, 2.4], 0),
start: [0.0],
goal: GoalRegion::new([2.0], 0.1),
};
let mut config = SstConfig::new(0.1, 0.5, 1.0, 1);
config.goal_bias = 0.0;
let mut sst = Sst::new(problem, config).expect("valid");
let mut rng = rand_pcg::Pcg64Mcg::seed_from_u64(1);
assert!(sst.step(&mut rng) && sst.step(&mut rng) && sst.step(&mut rng));
assert_eq!((sst.node_count(), sst.active_count()), (4, 3));
let trajectory = sst.best_trajectory().expect("solved");
assert_eq!(trajectory.states, [[0.0], [1.0], [2.0]]);
assert!((trajectory.cost - 2.0).abs() < 1e-12);
}
#[test]
fn step_counts_cover_the_whole_range() {
let mut rng = rand_pcg::Pcg64Mcg::seed_from_u64(1);
let mut seen = [false; 5];
for _ in 0..200 {
let steps = random_step_count(5, &mut rng);
assert!((1..=5).contains(&steps));
seen[steps - 1] = true;
}
assert_eq!(seen, [true; 5]);
}
#[test]
fn saturating_count_clamps() {
assert_eq!(saturating_count(f64::NAN), 0);
assert_eq!(saturating_count(0.5), 0);
assert_eq!(saturating_count(7.9), 7);
assert_eq!(
saturating_count(1e30),
usize::try_from(u32::MAX).expect("fits")
);
}
}