Skip to main content

temporal_control/stn/
explanation.rs

1//! Structured explanations of inconsistency.
2
3use alloc::vec::Vec;
4
5use super::ConstraintId;
6use crate::Ticks;
7
8/// Which bound of a constraint a distance-graph edge encodes.
9#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
10pub enum Bound {
11    /// The upper bound `t_to - t_from <= hi`, edge `from -> to` with weight `hi`.
12    Upper,
13    /// The lower bound `t_to - t_from >= lo`, edge `to -> from` with weight `-lo`.
14    Lower,
15}
16
17/// One edge of a negative cycle, named by the constraint bound that supplied it.
18#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
19pub struct CycleEdge {
20    /// The constraint the edge comes from.
21    pub constraint: ConstraintId,
22    /// Which bound of that constraint.
23    pub bound: Bound,
24}
25
26impl CycleEdge {
27    /// Creates a cycle edge reference.
28    pub(crate) const fn new(constraint: ConstraintId, bound: Bound) -> Self {
29        Self { constraint, bound }
30    }
31}
32
33/// A negative cycle in the distance graph: the constraints that jointly cannot hold.
34///
35/// Edges are in traversal order: the head of each edge is the tail of the next, and the head of
36/// the last edge is the tail of the first. Summing the edge weights gives [`Self::weight`],
37/// which is strictly negative.
38#[derive(Debug, Clone, PartialEq, Eq, Hash)]
39pub struct NegativeCycle {
40    /// Edges in traversal order. Never empty.
41    edges: Vec<CycleEdge>,
42    /// Total weight of the cycle. Always negative.
43    weight: Ticks,
44}
45
46impl NegativeCycle {
47    /// Creates a cycle from its edges and total weight.
48    pub(crate) fn new(edges: Vec<CycleEdge>, weight: Ticks) -> Self {
49        Self { edges, weight }
50    }
51
52    /// Edges in traversal order.
53    #[must_use]
54    pub fn edges(&self) -> &[CycleEdge] {
55        &self.edges
56    }
57
58    /// Total cycle weight; negative.
59    #[must_use]
60    pub fn weight(&self) -> Ticks {
61        self.weight
62    }
63}