robust-planning 0.1.0

Motion planning over reachable sets under bounded uncertainty: Robust-RRT, Robust RRT* and reachability-guided A* with conservative safety proofs
Documentation
//! Robust-RRT: an RRT whose nodes carry the reachable set they are reached with.
//!
//! Each iteration steers the nearest node toward a goal-biased sample, propagates that node's
//! reachable set along the edge and keeps the edge only if every set covering it is proven
//! disjoint from every obstacle. Uncertainty therefore builds up along a branch. A node
//! reaches the goal only when its whole set is proven inside the goal region.
//!
//! ```mermaid
//! graph LR
//!     Sample["sample (goal-biased)"] --> Nearest
//!     Nearest --> Steer["steer: control"]
//!     Steer --> Propagate["propagate set"]
//!     Propagate --> Check["check cover"]
//!     Check -->|safe| Add["add node"]
//!     Check -->|indeterminate| Sample
//!     Add --> Goal["set inside goal?"]
//! ```

use core::fmt;

use polyzonotope::{Containment, SparsePolyZonotope};
use sampling_planning::{
    Euclidean, LinearScan, NearestNeighbors, Path, Sampler, rand_core::Rng, unit_f64,
};

use crate::{Goal, Node, Obstacles, PolytopeGoal, Reach, SafetyVerdict, Steer, check_edge, node};

/// Everything [`RobustRrt`] needs to know about the world.
///
/// The goal defaults to a [`PolytopeGoal`]; any [`Goal`] works.
#[derive(Debug, Clone)]
pub struct Problem<R, Sa, const N: usize, G = PolytopeGoal<N>> {
    /// The dynamics model that steers and propagates.
    pub reach: R,
    /// The obstacles and their split budget.
    pub obstacles: Obstacles,
    /// Draws the states the tree grows toward.
    pub sampler: Sa,
    /// The nominal start state, which steering starts from.
    pub start: [f64; N],
    /// Every state the system may start in. Must be proven safe.
    pub start_set: SparsePolyZonotope,
    /// The goal region. A node reaches it when its set is proven inside; goal-biased
    /// samples draw [`Goal::center`].
    pub goal: G,
}

/// Settings for [`RobustRrt`].
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct Config {
    /// Probability in `[0, 1]` of extending toward the goal's [`Goal::center`] instead of
    /// a random sample.
    pub goal_bias: f64,
}

impl Default for Config {
    /// A goal bias of 10 %.
    fn default() -> Self {
        Self { goal_bias: 0.1 }
    }
}

/// Why a [`RobustRrt`] could not be created.
#[derive(Debug, Clone, PartialEq)]
pub enum Error {
    /// The start set is not proven safe; carries the verdict.
    StartNotSafe(SafetyVerdict),
    /// [`Config::goal_bias`] is not in `[0, 1]`.
    GoalBiasOutOfRange,
}

impl fmt::Display for Error {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::StartNotSafe(verdict) => write!(f, "start set is not proven safe ({verdict:?})"),
            Self::GoalBiasOutOfRange => f.write_str("goal bias must be in [0, 1]"),
        }
    }
}

impl core::error::Error for Error {}

/// The Robust-RRT planner.
///
/// Every edge in the tree is proven safe over its whole duration, every intermediate time
/// step included, from the reachable set of its parent. Runs are reproducible: all randomness
/// comes from the caller's RNG.
pub struct RobustRrt<R: Reach<N>, Sa, const N: usize, G = PolytopeGoal<N>> {
    /// The problem being solved.
    problem: Problem<R, Sa, N, G>,
    /// Settings.
    config: Config,
    /// Tree nodes by id; node `0` is the root.
    nodes: Vec<Node<R::Control, N>>,
    /// Index over the nominal states of all nodes, using node ids.
    nn: LinearScan<[f64; N]>,
    /// The first node whose set is proven inside the goal region.
    goal_node: Option<usize>,
    /// Scratch buffer for the cover sets of the edge being checked.
    cover: Vec<SparsePolyZonotope>,
}

impl<R, Sa, const N: usize, G> RobustRrt<R, Sa, N, G>
where
    R: Reach<N> + Steer<N, R::Control>,
    Sa: Sampler<[f64; N]>,
    G: Goal<N>,
{
    /// Checks the start set and plants the root.
    ///
    /// # Errors
    ///
    /// - [`Error::StartNotSafe`] if the start set is not proven disjoint from every obstacle.
    /// - [`Error::GoalBiasOutOfRange`] if the goal bias is not in `[0, 1]`.
    pub fn new(problem: Problem<R, Sa, N, G>, config: Config) -> Result<Self, Error> {
        if !(0.0..=1.0).contains(&config.goal_bias) {
            return Err(Error::GoalBiasOutOfRange);
        }

        let verdict = problem.obstacles.check_set(&problem.start_set);

        if !verdict.is_safe() {
            return Err(Error::StartNotSafe(verdict));
        }

        let root = Node {
            nominal: problem.start,
            set: problem.start_set.clone(),
            parent: None,
            control: None,
        };

        let mut planner = Self {
            problem,
            config,
            nodes: Vec::new(),
            nn: LinearScan::default(),
            goal_node: None,
            cover: Vec::new(),
        };
        planner.add(root);

        Ok(planner)
    }

    /// Runs one iteration. Returns whether a node was added.
    ///
    /// # Panics
    ///
    /// Panics if the model's sets do not have the obstacles' dimension.
    pub fn step<Rg: Rng + ?Sized>(&mut self, rng: &mut Rg) -> bool {
        let target = self.draw(rng);
        let nearest = self
            .nn
            .nearest(&Euclidean::<N>, &target)
            .expect("the tree always holds the root");
        let parent = &self.nodes[nearest];
        let (control, nominal) = self.problem.reach.steer(&parent.nominal, &target);
        let (verdict, set) = check_edge(
            &self.problem.reach,
            &self.problem.obstacles,
            &parent.set,
            &control,
            &mut self.cover,
        );

        if !verdict.is_safe() {
            return false;
        }

        self.add(Node {
            nominal,
            set,
            parent: Some(nearest),
            control: Some(control),
        });

        true
    }

    /// Runs until the goal is reached or at most `max_iterations` iterations.
    ///
    /// Returns the path to the goal, or `None` if none was found within the budget.
    pub fn solve<Rg: Rng + ?Sized>(
        &mut self,
        max_iterations: usize,
        rng: &mut Rg,
    ) -> Option<Path<[f64; N]>> {
        for _ in 0..max_iterations {
            if self.goal_node.is_some() {
                break;
            }

            self.step(rng);
        }

        self.path()
    }

    /// All tree nodes by id; node `0` is the root.
    #[must_use]
    pub fn nodes(&self) -> &[Node<R::Control, N>] {
        &self.nodes
    }

    /// The first node whose set was proven inside the goal region, if any.
    #[must_use]
    pub const fn goal_node(&self) -> Option<usize> {
        self.goal_node
    }

    /// The problem being solved.
    #[must_use]
    pub const fn problem(&self) -> &Problem<R, Sa, N, G> {
        &self.problem
    }

    /// The node ids from the root to `node`, inclusive.
    ///
    /// # Panics
    ///
    /// Panics if `node` is not a node id.
    #[must_use]
    pub fn branch(&self, node: usize) -> Vec<usize> {
        node::branch(&self.nodes, node)
    }

    /// The nominal path to the goal node, or `None` if the goal was not reached yet.
    ///
    /// The cost is the Euclidean length of the nominal path.
    #[must_use]
    pub fn path(&self) -> Option<Path<[f64; N]>> {
        Some(node::nominal_path(&self.nodes, self.goal_node?))
    }

    /// Draws the goal center with the configured probability, otherwise a sampler state.
    fn draw<Rg: Rng + ?Sized>(&mut self, rng: &mut Rg) -> [f64; N] {
        if unit_f64(rng) < self.config.goal_bias {
            self.problem.goal.center()
        } else {
            self.problem.sampler.sample(rng)
        }
    }

    /// Adds `node` to the tree and records it if it is the first to reach the goal.
    fn add(&mut self, node: Node<R::Control, N>) {
        let id = self.nodes.len();

        if self.goal_node.is_none() && self.reaches_goal(&node.set) {
            self.goal_node = Some(id);
        }

        NearestNeighbors::<Euclidean<N>>::insert(&mut self.nn, id, node.nominal);
        self.nodes.push(node);
    }

    /// Whether `set` is proven inside the goal region.
    fn reaches_goal(&self, set: &SparsePolyZonotope) -> bool {
        self.problem.goal.containment(set) == Containment::Inside
    }
}