pub struct Stn { /* private fields */ }Expand description
A Simple Temporal Network.
Construction only fails on handle validation and checked arithmetic.
Algorithms are read-only and take an explicit Budget.
Implementations§
Source§impl Stn
impl Stn
Sourcepub fn add_time_point(&mut self) -> Result<TimePoint, Error>
pub fn add_time_point(&mut self) -> Result<TimePoint, Error>
Adds a time point and returns its handle.
§Errors
Error::Overflow if the number of time points would exceed usize::MAX.
Sourcepub fn time_point_count(&self) -> usize
pub fn time_point_count(&self) -> usize
Number of time points.
Sourcepub fn constraints(&self) -> &[Constraint]
pub fn constraints(&self) -> &[Constraint]
All constraints, indexed by ConstraintId.
Sourcepub fn constraint(&self, id: ConstraintId) -> Option<&Constraint>
pub fn constraint(&self, id: ConstraintId) -> Option<&Constraint>
Looks up one constraint.
Sourcepub fn add_constraint(
&mut self,
from: TimePoint,
to: TimePoint,
lo: Option<Ticks>,
hi: Option<Ticks>,
) -> Result<ConstraintId, Error>
pub fn add_constraint( &mut self, from: TimePoint, to: TimePoint, lo: Option<Ticks>, hi: Option<Ticks>, ) -> Result<ConstraintId, Error>
Adds the constraint lo <= t_to - t_from <= hi.
Either bound may be None (unbounded). lo > hi is accepted and makes the network
inconsistent, which the checks then explain.
§Errors
Error::UnknownTimePointiffromortois not in this network.Error::Overflowifloisi64::MIN, whose negation is not representable.
Sourcepub fn check_consistency(
&self,
budget: &Budget,
) -> Result<Verdict<Schedule>, Error>
pub fn check_consistency( &self, budget: &Budget, ) -> Result<Verdict<Schedule>, Error>
Checks consistency with Bellman-Ford from a virtual source.
Runs in O(n * m) steps and O(n + m) memory. A consistent verdict carries a
Schedule that satisfies every constraint; an inconsistent verdict carries a
NegativeCycle.
§Errors
Error::BudgetExhaustedif the step budget runs out.Error::Overflowif a path weight overflowsi64.Error::Internalif cycle extraction breaks an invariant (a bug).
Sourcepub fn minimal_network(
&self,
budget: &Budget,
) -> Result<Verdict<MinimalNetwork>, Error>
pub fn minimal_network( &self, budget: &Budget, ) -> Result<Verdict<MinimalNetwork>, Error>
Computes the minimal network (all-pairs shortest paths) with Floyd-Warshall.
Runs in O(n^3) steps and n^2 cells. On inconsistency, the explanation is extracted
with Bellman-Ford using the remaining budget; if Bellman-Ford disagrees, the result is
Error::Internal.
§Errors
Error::MemoryBudgetExceededifn^2exceedsBudget::max_cells.Error::OutOfMemoryif the allocator refuses the matrix.Error::BudgetExhaustedif the step budget runs out.Error::Overflowif a path weight, or the negation of one, overflowsi64.Error::Internalon disagreement between the two algorithms (a bug).
Sourcepub fn partial_path_consistency(
&self,
budget: &Budget,
) -> Result<Verdict<ChordalNetwork>, Error>
pub fn partial_path_consistency( &self, budget: &Budget, ) -> Result<Verdict<ChordalNetwork>, Error>
Computes the minimal constraints of every constrained pair with P³C (Planken, de Weerdt
and van der Krogt, ICAPS 2008), without the full n^2 minimal network.
The result covers a chordal supergraph of the constraint graph: every pair that shares a
constraint, plus the fill edges of a minimum-degree triangulation. For each of those
pairs the bounds equal Stn::minimal_network’s. To include a pair that shares no
constraint, add one without bounds. Runs in O((n + m + t) log n) steps for m
constraints and t triangles of the chordal graph (O(n w*^2) for induced width w*,
at most O(n^3)); see the p3c module docs. On inconsistency the explanation comes
from Bellman-Ford using the remaining budget, which also decides the verdict if P³C
overflows first.
§Errors
Error::MemoryBudgetExceededif the chordal graph has more thanBudget::max_cellsdirected edges (two per pair).Error::BudgetExhaustedif the step budget runs out.Error::Overflowif a path length, or the negation of a bound, overflowsi64on a consistent network.Error::Internalif Bellman-Ford finds consistent a network P³C rejected (a bug).
§Examples
use temporal_control::{Budget, Stn, Ticks, TimePoint, Verdict};
// A chain a -> b -> c of 2..5 ticks each; the pair (a, c) is of interest.
let mut stn = Stn::new();
let [a, b, c] = [(); 3].map(|()| stn.add_time_point().expect("fits"));
stn.add_constraint(a, b, Some(Ticks(2)), Some(Ticks(5))).expect("exists");
stn.add_constraint(b, c, Some(Ticks(2)), Some(Ticks(5))).expect("exists");
stn.add_constraint(a, c, None, None).expect("exists");
let Verdict::Consistent(net) = stn.partial_path_consistency(&Budget::UNLIMITED).expect("fits")
else {
panic!("consistent");
};
assert_eq!((net.lower(a, c), net.upper(a, c)), (Some(Ticks(4)), Some(Ticks(10))));
assert!(!net.contains(TimePoint(0), TimePoint(7)));Sourcepub fn dispatchable(
&self,
budget: &Budget,
) -> Result<Verdict<DispatchableNetwork>, Error>
pub fn dispatchable( &self, budget: &Budget, ) -> Result<Verdict<DispatchableNetwork>, Error>
Computes the minimal network and filters it into a dispatchable network.
Costs O(n^3) steps and n^2 cells. See MinimalNetwork::dispatchable.
§Errors
As for Stn::minimal_network and MinimalNetwork::dispatchable. Each phase is
metered against the full budget, so the total can reach twice its step limit.
§Examples
use temporal_control::{Budget, Dispatcher, Stn, Ticks, TimePoint, Verdict};
// A chain a -> b -> c, each step 2..5 ticks: the implied a -> c edges are filtered out.
let mut stn = Stn::new();
let [a, b, c] = [(); 3].map(|()| stn.add_time_point().expect("fits"));
stn.add_constraint(a, b, Some(Ticks(2)), Some(Ticks(5))).expect("exists");
stn.add_constraint(b, c, Some(Ticks(2)), Some(Ticks(5))).expect("exists");
let Verdict::Consistent(net) = stn.dispatchable(&Budget::UNLIMITED).expect("fits") else {
panic!("consistent");
};
assert_eq!(net.edges().len(), 4);
let mut d: Dispatcher<3, 8> = net.dispatcher().expect("fits");
let mut out = [TimePoint(0); 3];
assert_eq!(d.step(Ticks(0), &[], &mut out), Ok(1));
assert_eq!(d.next_wakeup(), Some(Ticks(2)));