Skip to main content

Envelope

Struct Envelope 

Source
pub struct Envelope<'a, S: Storage = Heap> { /* private fields */ }
Expand description

The lower and upper level envelopes of a timeline with additional flexible activities.

The timeline’s own impacts count as fixed. Create it with Envelope::new.

§Examples

use resource_timeline::envelope::{Envelope, Flexible, Verdict};
use resource_timeline::{ActivityId, Bounds, Impact, Timeline, Window};

// A battery at 10 units, reserve 0, charged by 6 units at tick 10.
let mut battery = Timeline::depletable(0, 10, Bounds::new(0, 100)?, 8)?;
battery.add(ActivityId(0), Impact::Step { at: 10, delta: 6 })?;

// Two heaters, each drawing 6 units, may start anywhere in [0, 20].
let heat = [Impact::Step { at: 0, delta: -6 }];
let window = Window::new(0, 20)?;
let flexible = [
    Flexible { activity: ActivityId(1), window, impacts: &heat },
    Flexible { activity: ActivityId(2), window, impacts: &heat },
];

let envelope = Envelope::new(&battery, &flexible)?;
assert_eq!(envelope.lower_at(0)?, -2); // both heaters start before the charge
assert_eq!(envelope.upper_at(0)?, 10); // neither has started yet
match envelope.check()? {
    Verdict::Possible(violation) => assert_eq!(violation.at, 0),
    verdict => panic!("unexpected {verdict:?}"),
}

// With the second heater held back until after the charge, every schedule is safe.
let late = Window::new(10, 20)?;
let flexible = [flexible[0], Flexible { window: late, ..flexible[1] }];
assert_eq!(Envelope::new(&battery, &flexible)?.check()?, Verdict::Safe);

Implementations§

Source§

impl<'a, S: Storage> Envelope<'a, S>

Source

pub fn new( timeline: &'a Timeline<S>, flexible: &'a [Flexible<'a>], ) -> Result<Self, Error>

Combines the fixed impacts of timeline with the flexible activities.

The windows are independent until with_precedences couples them; with_contingent adds activities with uncertain durations.

§Errors
Source

pub fn with_contingent( self, contingent: &'a [Contingent<'a>], ) -> Result<Self, Error>

Adds activities whose durations are uncertain.

The envelope then covers every start in each window combined with every duration in each duration range. Claims and rates that last as long as the activity are anchored at its end, see Uncertain. Such a member’s extreme at a tick is found by evaluating its profile at the vertices of the (start, duration) box, see the member module.

§Errors
§Examples
use resource_timeline::envelope::{Anchor, Contingent, Envelope, Uncertain, Verdict};
use resource_timeline::{ActivityId, Bounds, Impact, Timeline, Window};

// A battery at 30 units; a heater draws 2 units per tick while it runs, 5 to 15 ticks.
let battery = Timeline::depletable(0, 30, Bounds::new(0, 100)?, 8)?;
let heat = [Uncertain::Rate { start: Anchor::Start(0), end: Anchor::End(0), per_tick: -2 }];
let heater = [Contingent {
    activity: ActivityId(1),
    window: Window::new(0, 10)?,
    duration: Window::new(5, 15)?,
    impacts: &heat,
}];

let envelope = Envelope::new(&battery, &[])?.with_contingent(&heater)?;
assert_eq!(envelope.lower_at(25)?, 0); // the longest run has drawn 30
assert_eq!(envelope.upper_at(25)?, 20); // the shortest run has drawn 10
assert_eq!(envelope.check()?, Verdict::Safe);

// A 16-tick run would drain the battery: possible, not certain.
let longer = [Contingent { duration: Window::new(5, 16)?, ..heater[0] }];
let envelope = Envelope::new(&battery, &[])?.with_contingent(&longer)?;
assert!(matches!(envelope.check()?, Verdict::Possible(_)));
Source

pub fn with_precedences(self, precedences: &[Precedence]) -> Result<Self, Error>

Restricts the schedules to those that respect precedences between the activities.

Start windows and contingent duration ranges are tightened by propagation, and the activities that take part in a precedence get a joint, exact extreme: by maximum-weight closure if all are stepwise, by a discretised minimum cut if some have rates or uncertain durations and their domains fit the cut budget; see the precedence module. Above the budget the latter keep separate extremes over their tightened ranges, which may add up to a level no single schedule reaches, so Verdict::Possible may then be a false alarm (never Verdict::Safe a false all-clear). The envelopes are never looser than without precedences. With the cut, every tick a coupled time point can take becomes an evaluation tick, so check costs grow with those windows.

§Errors
§Examples
use resource_timeline::envelope::{Envelope, Flexible, Precedence, Verdict};
use resource_timeline::{ActivityId, Bounds, Impact, Timeline, Window};

// One transmitter; two downlinks of 10 ticks, each anywhere in [0, 20].
let transmitter = Timeline::claimable(0, 1, 8)?;
let downlink = [Impact::Claim { start: 0, end: 10, amount: 1 }];
let window = Window::new(0, 20)?;
let flexible = [
    Flexible { activity: ActivityId(1), window, impacts: &downlink },
    Flexible { activity: ActivityId(2), window, impacts: &downlink },
];

// Independent windows let the downlinks overlap.
let independent = Envelope::new(&transmitter, &flexible)?;
assert!(matches!(independent.check()?, Verdict::Possible(_)));

// "2 starts after 1 has ended" rules every overlap out.
let ordered = independent.with_precedences(&[Precedence::after_end(
    ActivityId(1),
    ActivityId(2),
    0,
)])?;
assert_eq!(ordered.check()?, Verdict::Safe);
assert_eq!(ordered.window(ActivityId(1)), Some(Window::new(0, 10)?));
Source

pub fn with_cut_budget(self, nodes: usize) -> Result<Self, Error>

Sets the largest graph, in nodes, that the exact coupled extreme of precedence-linked activities with rates or uncertain durations may build per tick (one node per tick of each coupled start and end domain). Above it those activities get separate, sound extremes. The default is 4096.

§Errors

The errors of with_precedences, re-run with the new budget.

Source

pub fn duration(&self, activity: ActivityId) -> Option<Window>

The duration range of the first activity with id activity, tightened by the precedences ([0, 0] for a flexible activity), or None if there is no such activity.

Source

pub fn window(&self, activity: ActivityId) -> Option<Window>

The start window of the first activity with id activity, tightened by the precedences, or None if there is no such activity.

Source

pub fn lower_at(&self, t: Tick) -> Result<Level, Error>

The lowest level any admitted schedule has at tick t.

Costs O(n + f k^2) for n fixed impacts and f independent activities with k impacts each, plus a minimum cut over the pending events of coupled activities.

§Errors
Source

pub fn upper_at(&self, t: Tick) -> Result<Level, Error>

The highest level any admitted schedule has at tick t.

§Errors

As for lower_at.

Source

pub fn check(&self) -> Result<Verdict, Error>

Checks the envelopes against the timeline’s bounds and end-state targets.

The envelopes are evaluated at every tick where their shape can change and at the tick before. Without rates they are constant in between, so the reported tick is the earliest one with a possible (or certain) violation. With rates the upper envelope is convex and the lower one concave between those ticks, so Verdict::Safe is still exact, but a reported tick may lie after the first one, and a certain violation that exists only strictly between evaluation ticks is reported as possible. A certain violation anywhere takes precedence over possible ones.

§Errors

Trait Implementations§

Source§

impl<'a, S: Clone + Storage> Clone for Envelope<'a, S>

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<'a, S: Debug + Storage> Debug for Envelope<'a, S>

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<'a, S> Forget for Envelope<'a, S>

§

impl<'a, S> Freeze for Envelope<'a, S>
where &'a Timeline<S>: Freeze,

§

impl<'a, S> Move for Envelope<'a, S>

§

impl<'a, S> RefUnwindSafe for Envelope<'a, S>
where &'a Timeline<S>: RefUnwindSafe,

§

impl<'a, S> Send for Envelope<'a, S>
where &'a Timeline<S>: Send,

§

impl<'a, S> Sync for Envelope<'a, S>
where &'a Timeline<S>: Sync,

§

impl<'a, S> Unpin for Envelope<'a, S>
where &'a Timeline<S>: Unpin,

§

impl<'a, S> UnsafeUnpin for Envelope<'a, S>
where &'a Timeline<S>: UnsafeUnpin,

§

impl<'a, S> UnwindSafe for Envelope<'a, S>
where &'a Timeline<S>: UnwindSafe,

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.