pub struct Stnu { /* private fields */ }Expand description
A Simple Temporal Network with Uncertainty.
§Examples
use temporal_control::{Budget, Controllability, Stnu, Ticks};
// Warm-up takes 5..15 ticks; analysis must start within 2 ticks of its end.
let mut stnu = Stnu::new();
let start = stnu.add_time_point().expect("fits");
let warm = stnu.add_contingent_link(start, Ticks(5), Ticks(15)).expect("valid");
let analysis = stnu.add_time_point().expect("fits");
stnu.add_constraint(warm, analysis, Some(Ticks(0)), Some(Ticks(2))).expect("exists");
let verdict = stnu.dispatchable(&Budget::UNLIMITED).expect("within budget");
assert!(matches!(verdict, Controllability::Controllable(_)));Implementations§
Source§impl Stnu
impl Stnu
Sourcepub fn with_reaction(&self, reaction: Reaction) -> Result<Self, Error>
pub fn with_reaction(&self, reaction: Reaction) -> Result<Self, Error>
The network whose same-tick dynamic controllability equals this network’s under
reaction.
For Reaction::SameTick this is a copy. For Reaction::NextTick every contingent
point C is replaced by its observation C' = C + 1, which the agent sees at tick
C', i.e. one tick after C happened: a link from an executable point becomes
[lo + 1, hi + 1], a link from a contingent point keeps its bounds (both ends shift),
and every requirement is rewritten with C = C' - 1. Under same-tick reaction an agent
of the new network sees exactly what a next-tick agent of the old one sees, so the
transformation is exact; the tests check it against the brute-force game with the
next-tick move order. Point and constraint indices are unchanged, so a
SemiReducibleCycle of the result names the same
constraints and links.
Dispatching the result needs an executive that reports each contingent event one tick
late (C observed at t is handed to the dispatcher at t + 1); the dispatcher itself
is unchanged.
§Examples
use temporal_control::{Budget, Controllability, Reaction, Stnu, Ticks};
// X exactly at the end of a [1, 10] activity: needs same-tick reaction.
let mut stnu = Stnu::new();
let a = stnu.add_time_point().expect("fits");
let c = stnu.add_contingent_link(a, Ticks(1), Ticks(10)).expect("valid");
let x = stnu.add_time_point().expect("fits");
stnu.add_constraint(c, x, Some(Ticks(0)), Some(Ticks(0))).expect("exists");
let dc = |s: &Stnu| {
let verdict = s.check_controllability(&Budget::UNLIMITED).expect("fits");
matches!(verdict, Controllability::Controllable(()))
};
assert!(dc(&stnu));
assert!(!dc(&stnu.with_reaction(Reaction::NextTick).expect("no overflow")));§Errors
Error::Overflow if a shifted bound overflows i64.
Source§impl Stnu
impl Stnu
Sourcepub fn add_time_point(&mut self) -> Result<TimePoint, Error>
pub fn add_time_point(&mut self) -> Result<TimePoint, Error>
Adds an executable time point.
§Errors
Error::Overflow if the number of time points would exceed usize::MAX.
Sourcepub fn add_contingent_link(
&mut self,
activation: TimePoint,
lo: Ticks,
hi: Ticks,
) -> Result<TimePoint, Error>
pub fn add_contingent_link( &mut self, activation: TimePoint, lo: Ticks, hi: Ticks, ) -> Result<TimePoint, Error>
Adds a contingent link from activation and returns its new contingent time point.
§Errors
Error::UnknownTimePointifactivationis not in this network.Error::InvalidContingentLinkunless0 < lo <= hi.Error::Overflowif the number of time points would exceedusize::MAX.
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 requirement lo <= t_to - t_from <= hi. See Stn::add_constraint.
§Errors
As for Stn::add_constraint.
Sourcepub fn requirements(&self) -> &Stn
pub fn requirements(&self) -> &Stn
Time points and requirement constraints, without the contingent links.
Sourcepub fn links(&self) -> &[ContingentLink]
pub fn links(&self) -> &[ContingentLink]
All contingent links, indexed by ContingentId.
Sourcepub fn time_point_count(&self) -> usize
pub fn time_point_count(&self) -> usize
Number of time points, executable and contingent.
Sourcepub fn check_controllability(
&self,
budget: &Budget,
) -> Result<Controllability<()>, Error>
pub fn check_controllability( &self, budget: &Budget, ) -> Result<Controllability<()>, Error>
Checks dynamic controllability with Morris’s O(n^3) backpropagation from negative
nodes (DcAlgorithm::Morris2014).
Independent of Stnu::dispatchable; both must agree, which the tests check. Use this
when only the verdict (and an explanation) is needed: it keeps O(m + n^2) memory in
the worst case instead of dense matrices. See the morris module docs for the
algorithm and the proof of its bound.
Budget::max_cells must admit (n + k)^2 cells, the worst case of derived edges.
shortcut: the allocations themselves (adjacency lists, derived edges, search state)
abort on allocator failure; make them fallible before running this onboard.
§Examples
use temporal_control::{Budget, Controllability, Stnu, Ticks};
// X must precede the end of a [1, 10] activity by 1..2 ticks: impossible to schedule.
let mut stnu = Stnu::new();
let a = stnu.add_time_point().expect("fits");
let c = stnu.add_contingent_link(a, Ticks(1), Ticks(10)).expect("valid");
let x = stnu.add_time_point().expect("fits");
stnu.add_constraint(x, c, Some(Ticks(1)), Some(Ticks(2))).expect("exists");
let verdict = stnu.check_controllability(&Budget::UNLIMITED).expect("within budget");
let Controllability::Uncontrollable(cycle) = verdict else {
panic!("not DC");
};
// X -> C: 2 (requirement), C -> A: -10 (C may take 10), A -> C: 1 (C may take 1),
// C -> X: -1 (requirement): the agent would have to know C before it happens.
assert!(cycle.weight() < Ticks::ZERO);
assert!(!cycle.contingent_links().is_empty());§Errors
Error::MemoryBudgetExceededif(n + k)^2exceedsBudget::max_cells.Error::BudgetExhaustedif the step budget runs out.Error::Overflowif a path weight overflowsi64.
Sourcepub fn check_controllability_with(
&self,
algorithm: DcAlgorithm,
budget: &Budget,
) -> Result<Controllability<()>, Error>
pub fn check_controllability_with( &self, algorithm: DcAlgorithm, budget: &Budget, ) -> Result<Controllability<()>, Error>
Checks dynamic controllability with the chosen algorithm.
Every algorithm returns the same verdict. A negative verdict carries the explanation of
DcAlgorithm::Morris2014, which then also runs (against a fresh copy of budget).
§Examples
use temporal_control::{Budget, Controllability, DcAlgorithm, Stnu, Ticks};
let mut stnu = Stnu::new();
let a = stnu.add_time_point().expect("fits");
let c = stnu.add_contingent_link(a, Ticks(1), Ticks(10)).expect("valid");
let x = stnu.add_time_point().expect("fits");
// X within 2 ticks after C: wait for C, then execute.
stnu.add_constraint(c, x, Some(Ticks(0)), Some(Ticks(2))).expect("exists");
for algorithm in [DcAlgorithm::Morris2014, DcAlgorithm::RulMinus] {
let verdict = stnu.check_controllability_with(algorithm, &Budget::UNLIMITED);
assert_eq!(verdict, Ok(Controllability::Controllable(())));
}§Errors
Error::MemoryBudgetExceededif an explanation is needed and(n + k)^2exceedsBudget::max_cells(or, forDcAlgorithm::Morris2014, always in that case).Error::BudgetExhaustedif the step budget runs out.Error::Overflowif a path weight overflowsi64.Error::Internalif the explaining checker disagrees with the chosen one.
Sourcepub fn dispatchable(
&self,
budget: &Budget,
) -> Result<Controllability<DispatchableNetwork>, Error>
pub fn dispatchable( &self, budget: &Budget, ) -> Result<Controllability<DispatchableNetwork>, Error>
Checks dynamic controllability and derives a dispatchable extended STNU with few edges
(DispatchAlgorithm::Minimal).
The result keeps the ordinary edges and waits (conditional upper-case edges) that a
Dispatcher executes. See DispatchAlgorithm for the cost.
A negative verdict carries the explanation of Stnu::check_controllability.
§Errors
As for Stnu::dispatchable_with.
Sourcepub fn dispatchable_with(
&self,
algorithm: DispatchAlgorithm,
budget: &Budget,
) -> Result<Controllability<DispatchableNetwork>, Error>
pub fn dispatchable_with( &self, algorithm: DispatchAlgorithm, budget: &Budget, ) -> Result<Controllability<DispatchableNetwork>, Error>
Checks dynamic controllability and derives a dispatchable extended STNU with the chosen
algorithm.
Every algorithm returns the same verdict, and every dispatchable network satisfies all
requirements under every dispatch the Dispatcher allows (checked
by simulation in the tests). A negative verdict carries the explanation of
Stnu::check_controllability, which then also runs against a fresh copy of budget
(except for DispatchAlgorithm::Morris2014 and DispatchAlgorithm::Minimal, whose
verdict comes from the same propagation).
§Examples
use temporal_control::{Budget, Controllability, DispatchAlgorithm, Stnu, Ticks};
// Analysis must start within 2 ticks after a 5..15 tick warm-up ends.
let mut stnu = Stnu::new();
let start = stnu.add_time_point().expect("fits");
let warm = stnu.add_contingent_link(start, Ticks(5), Ticks(15)).expect("valid");
let analysis = stnu.add_time_point().expect("fits");
stnu.add_constraint(warm, analysis, Some(Ticks(0)), Some(Ticks(2))).expect("exists");
let size = |algorithm| match stnu.dispatchable_with(algorithm, &Budget::UNLIMITED) {
Ok(Controllability::Controllable(net)) => net.edges().len() + net.waits().len(),
other => panic!("DC: {other:?}"),
};
assert!(size(DispatchAlgorithm::Minimal) <= size(DispatchAlgorithm::Closure));§Errors
Error::MemoryBudgetExceededif the matrices exceedBudget::max_cells.Error::OutOfMemoryif the allocator refuses them.Error::BudgetExhaustedif the step budget runs out.Error::Overflowif a path weight overflowsi64.Error::Internalif the explaining checker disagrees, or the minimisation finds an inconsistency in a network the check found DC.