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>
impl<'a, S: Storage> Envelope<'a, S>
Sourcepub fn new(
timeline: &'a Timeline<S>,
flexible: &'a [Flexible<'a>],
) -> Result<Self, Error>
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
- The validation errors of
Timeline::addfor any flexible impact at the earliest or latest start of its window, includingError::BeforeStartwhen the earliest start would place an impact before the timeline start. Error::Overflowif a shifted tick does not fit aTick.Error::OutOfMemoryif the activity list cannot be allocated.
Sourcepub fn with_contingent(
self,
contingent: &'a [Contingent<'a>],
) -> Result<Self, Error>
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
Error::NegativeDurationif a duration range starts below zero.Error::EmptyIntervalif a claim or rate interval is inverted for some duration (an empty interval at the shortest duration is allowed).- The other validation errors of
Timeline::addfor any impact at the corners of the start window and the duration range. - The errors of
with_precedencesif precedences were given before. Error::Overflowif a tick does not fit aTick.
§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(_)));Sourcepub fn with_precedences(self, precedences: &[Precedence]) -> Result<Self, Error>
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
Error::UnknownActivityif a precedence names an activity that is not part of the envelope. Add contingent activities withwith_contingentfirst; precedences given earlier are re-applied when activities are added later.Error::InconsistentPrecedencesif no schedule satisfies the windows and the precedences together.Error::OutOfMemoryif the network cannot be allocated.
§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)?));Sourcepub fn with_cut_budget(self, nodes: usize) -> Result<Self, Error>
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.
Sourcepub fn duration(&self, activity: ActivityId) -> Option<Window>
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.
Sourcepub fn window(&self, activity: ActivityId) -> Option<Window>
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.
Sourcepub fn lower_at(&self, t: Tick) -> Result<Level, Error>
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
Error::BeforeStartiftprecedes the timeline start.Error::Overflowif the value does not fit aLevel.Error::OutOfMemoryif the event graph of coupled activities cannot be allocated.
Sourcepub fn check(&self) -> Result<Verdict, Error>
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
Error::Overflowif an envelope value does not fit aLevel.Error::OutOfMemoryif the evaluation ticks cannot be allocated.