Skip to main content

Stnu

Struct Stnu 

Source
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

Source

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

Source

pub fn new() -> Self

Creates an empty network.

Source

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.

Adds a contingent link from activation and returns its new contingent time point.

§Errors
Source

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.

Source

pub fn requirements(&self) -> &Stn

Time points and requirement constraints, without the contingent links.

All contingent links, indexed by ContingentId.

Source

pub fn time_point_count(&self) -> usize

Number of time points, executable and contingent.

Source

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
Source

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
Source

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.

Source

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

Trait Implementations§

Source§

impl Clone for Stnu

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Stnu

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for Stnu

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

§

impl Forget for Stnu

§

impl Freeze for Stnu

§

impl Move for Stnu

§

impl RefUnwindSafe for Stnu

§

impl Send for Stnu

§

impl Sync for Stnu

§

impl Unpin for Stnu

§

impl UnsafeUnpin for Stnu

§

impl UnwindSafe for Stnu

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.