temporal-control 0.1.1

Simple temporal networks with uncertainty: dynamic controllability checking, dispatchable networks and a no_std, no-alloc onboard dispatcher
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
//! Dynamic controllability by the RUL⁻ algorithm (Cairo, Hunsberger and Rizzi, TIME 2018).
//!
//! # Graph and rules
//!
//! The *LO-graph* has one ordinary edge per requirement bound and the lower-case edge
//! `A -> C: l` of every contingent link `(A, l, u, C)`; the upper-case edges `C -> A: -u` are
//! kept apart, and the link's ordinary bound edges are not part of it. Let `Δ_R = u_R - l_R`.
//! The three rules only generate ordinary edges:
//!
//! - **Relax⁻**: `P -v-> Q -w-> R` gives `P -(v+w)-> R` if `Q` is executable, `R` contingent
//!   and `w < Δ_R`.
//! - **Lower⁻**: `A_Q -q:l-> Q -w-> R` gives `A_Q -(l+w)-> R` if `Q != R` are contingent and
//!   `w < Δ_R`.
//! - **Upper⁻**: `P -v-> R -R:-u-> A_R` gives `P -m-> A_R` with `m = max(v - u, -l)` (not
//!   length-preserving: if `v < Δ_R`, `P` cannot execute before `A_R + l` anyway).
//!
//! # Algorithm (paper's Algorithm 1)
//!
//! One Bellman-Ford pass computes a potential `h` for the LO-graph (`h(P) >= h(Q) - w` for every
//! edge `P -w-> Q`; failure means a negative cycle). Then contingent points are processed with a
//! stack `S` and the set `R` of unprocessed ones, starting with the first link:
//!
//! 1. `CloseRelaxLower(R)`: Dijkstra backwards from `R` over the LO-graph, keyed by
//!    `h(P) + w(P, R)`, applying Relax⁻ and Lower⁻ and adding every improved edge into `R`.
//! 2. `ApplyUpper(R)`: Upper⁻ on every ordinary edge into `R`, adding edges into `A_R`.
//! 3. `UpdatePotential(A_R)`: a Dijkstra-like pass from `A_R` repairs `h` for the new edges;
//!    raising `h(A_R)` itself, or any edge still violating `h`, means a negative LO-cycle: not
//!    DC.
//! 4. If an unprocessed `R'` has an edge `A_R' -s-> R` with `s < Δ_R` (back-propagation from `R`
//!    is blocked by the upper-case edge of `R'`), push `R'`, or answer not DC if `R'` is
//!    already on `S`. Otherwise `R` is done: pop it, and if `S` is empty start the next
//!    unprocessed link.
//!
//! The network is DC iff this finishes. Each round ends by pushing a new point or by popping
//! one for good, so there are at most `2k` rounds.
//!
//! **Reading of the pseudo-code.** Algorithm 2 line 3 seeds the queue with every LO-edge into
//! `R`, which literally includes `R`'s own lower-case edge `A_R -r:l-> R`. Relaxing through it
//! would claim `R <= P + v + l_R` for an edge `P -v-> A_R`, which is unsound (nature may take
//! `u_R`), and no RUL⁻ rule has a lower-case edge as its second edge. So only ordinary edges
//! into `R` seed the queue and enter `w(·, R)`. The pseudo-code's initial `push Z onto S` is
//! read as pushing the first unprocessed contingent point.
//!
//! # Cost
//!
//! `O(n m)` for Bellman-Ford, then at most `2k` rounds. A round touches the `m + 2kn`
//! LO-edges (each round adds at most `n` edges into `R` and `n` into `A_R`) a constant number
//! of times. With a binary heap (one push per relaxation) a round costs
//! `O((m + k n) log n)`, so the total is `O(n m + k (m + k n) log n)`; the paper's
//! `O(m n + k^2 n + k n log n)` needs a heap with `O(1)` decrease-key. Memory: a dense
//! `k * n` table of the edges into contingent points, checked against
//! [`Budget::max_cells`], plus the LO-edges.

use alloc::collections::BinaryHeap;
use alloc::vec;
use alloc::vec::Vec;
use core::cmp::Reverse;

use super::{ContingentLink, Controllability, Stnu};
use crate::budget::Meter;
use crate::stn::steps;
use crate::{Budget, Error, Ticks, TimePoint};

mod rul2021;

/// The LO-graph, the potential and the edges into contingent points during one check.
struct Rul<'m> {
    /// The contingent links, with activation points normalised to be executable.
    links: Vec<ContingentLink>,
    /// Number of nodes: the time points plus one per link with a contingent activation point.
    n: usize,
    /// Link whose contingent point each time point is, if any.
    link_of: Vec<Option<usize>>,
    /// Incoming LO-edges `(tail, weight)` per node; grows with derived edges.
    incoming: Vec<Vec<(usize, Ticks)>>,
    /// Row-major `k * n`: the tightest ordinary edge `P -> C_r` at `[r * n + P]`, self-loops
    /// excluded; `None` is no edge.
    into: Vec<Option<Ticks>>,
    /// Potential: `h[P] >= h[Q] - w` for every LO-edge `P -w-> Q` between rounds.
    h: Vec<Ticks>,
    /// Step meter.
    meter: &'m mut Meter,
}

/// Checks dynamic controllability with RUL⁻; a negative verdict is explained by Morris 2014.
///
/// # Errors
///
/// See [`Stnu::check_controllability_with`].
pub(super) fn run(stnu: &Stnu, budget: &Budget) -> Result<Controllability<()>, Error> {
    let mut meter = budget.meter();
    let mut g = Rul::new(stnu, budget, &mut meter)?;

    if g.init_potential()? && g.process_links()? {
        return Ok(Controllability::Controllable(()));
    }

    super::morris::explain(stnu, budget).map(Controllability::Uncontrollable)
}

/// Checks dynamic controllability with RUL2021 ([`rul2021`]); a negative verdict is explained
/// by Morris 2014.
///
/// # Errors
///
/// See [`Stnu::check_controllability_with`].
pub(super) fn run_2021(stnu: &Stnu, budget: &Budget) -> Result<Controllability<()>, Error> {
    let mut meter = budget.meter();
    let mut g = Rul::new(stnu, budget, &mut meter)?;

    if g.init_potential()? && rul2021::run(&mut g)? {
        return Ok(Controllability::Controllable(()));
    }

    super::morris::explain(stnu, budget).map(Controllability::Uncontrollable)
}

impl<'m> Rul<'m> {
    /// Builds the LO-graph and the table of edges into contingent points.
    ///
    /// The paper assumes executable activation points "without loss of generality". A link
    /// activated by a contingent point `C'` gets a fresh executable node tied to `C'` by two
    /// zero-weight requirement edges instead; under instantaneous reaction that node executes
    /// at the tick `C'` is observed, so DC is preserved.
    ///
    /// # Errors
    ///
    /// [`Error::MemoryBudgetExceeded`] if `k * n` cells exceed the budget,
    /// [`Error::BudgetExhausted`] or [`Error::Overflow`].
    fn new(stnu: &Stnu, budget: &Budget, meter: &'m mut Meter) -> Result<Self, Error> {
        let points = stnu.time_point_count();
        let mut contingent = vec![false; points];

        for link in &stnu.links {
            contingent[link.contingent.0] = true;
        }

        let mut links = stnu.links.clone();
        let mut ties = Vec::new();

        for link in &mut links {
            if contingent[link.activation.0] {
                let node = points.checked_add(ties.len()).ok_or(Error::Overflow)?;
                ties.push((link.activation.0, node));
                link.activation = TimePoint(node);
            }
        }

        let n = points + ties.len();
        let cells = links
            .len()
            .checked_mul(n)
            .ok_or(Error::MemoryBudgetExceeded)?;
        budget.check_cells(cells)?;
        meter.spend(steps(cells)?.saturating_add(steps(n)?))?;

        let mut g = Self {
            n,
            link_of: vec![None; n],
            incoming: vec![Vec::new(); n],
            into: vec![None; cells],
            h: vec![Ticks::ZERO; n],
            links,
            meter,
        };

        for (r, link) in g.links.iter().enumerate() {
            g.link_of[link.contingent.0] = Some(r);
            g.incoming[link.contingent.0].push((link.activation.0, link.lo));
        }

        for (point, node) in ties {
            g.add_ordinary(point, node, Ticks::ZERO);
            g.add_ordinary(node, point, Ticks::ZERO);
        }

        for c in stnu.requirements.constraints() {
            if let Some(hi) = c.hi {
                g.add_ordinary(c.from.0, c.to.0, hi);
            }

            if let Some(lo) = c.lo {
                g.add_ordinary(c.to.0, c.from.0, lo.checked_neg().ok_or(Error::Overflow)?);
            }
        }

        Ok(g)
    }

    /// Adds the requirement edge `from -> to` to the LO-graph and to the table.
    fn add_ordinary(&mut self, from: usize, to: usize, weight: Ticks) {
        self.incoming[to].push((from, weight));

        if let Some(r) = self.link_of[to] {
            if from != to {
                lower(&mut self.into[r * self.n + from], weight);
            }
        }
    }

    /// Bellman-Ford for the initial potential; false on a negative LO-cycle.
    ///
    /// # Errors
    ///
    /// [`Error::BudgetExhausted`] or [`Error::Overflow`].
    fn init_potential(&mut self) -> Result<bool, Error> {
        for _ in 0..self.n {
            self.meter.spend(steps(self.edge_count())?)?;
            let mut changed = false;

            for q in 0..self.n {
                for &(p, w) in &self.incoming[q] {
                    let need = self.h[q].checked_sub(w).ok_or(Error::Overflow)?;

                    if need > self.h[p] {
                        self.h[p] = need;
                        changed = true;
                    }
                }
            }

            if !changed {
                return Ok(true);
            }
        }

        Ok(!self.violates_potential()?)
    }

    /// The main loop of Algorithm 1; false if the network is not DC.
    ///
    /// # Errors
    ///
    /// [`Error::BudgetExhausted`] or [`Error::Overflow`].
    fn process_links(&mut self) -> Result<bool, Error> {
        let k = self.links.len();
        let mut unprocessed = vec![true; k];
        let mut on_stack = vec![false; k];
        let mut stack = Vec::new();

        if k > 0 {
            stack.push(0);
            on_stack[0] = true;
        }

        while let Some(&r) = stack.last() {
            if !self.round(r)? {
                return Ok(false);
            }

            if let Some(blocker) = self.blocker(r, &unprocessed) {
                if on_stack[blocker] {
                    return Ok(false);
                }

                stack.push(blocker);
                on_stack[blocker] = true;
                continue;
            }

            unprocessed[r] = false;
            on_stack[r] = false;
            stack.pop();

            if stack.is_empty() {
                if let Some(next) = unprocessed.iter().position(|&u| u) {
                    stack.push(next);
                    on_stack[next] = true;
                }
            }
        }

        Ok(true)
    }

    /// One round for link `r`: generate edges into `C_r` and `A_r`, then repair the potential.
    /// False on a negative LO-cycle.
    ///
    /// # Errors
    ///
    /// [`Error::BudgetExhausted`] or [`Error::Overflow`].
    fn round(&mut self, r: usize) -> Result<bool, Error> {
        self.close_relax_lower(r)?;
        self.apply_upper(r)?;
        let activation = self.links[r].activation.0;

        Ok(self.update_potential(activation)? && !self.violates_potential()?)
    }

    /// The first unprocessed link `r'` with an edge `A_r' -s-> C_r` where `s < Δ_r`.
    fn blocker(&self, r: usize, unprocessed: &[bool]) -> Option<usize> {
        let delta = self.delta(r);

        (0..unprocessed.len()).find(|&b| {
            let a = self.links[b].activation.0;
            unprocessed[b] && self.into[r * self.n + a].is_some_and(|s| s < delta)
        })
    }

    /// `CloseRelaxLower`: all edges into `C_r` that Relax⁻ and Lower⁻ generate.
    ///
    /// # Errors
    ///
    /// [`Error::BudgetExhausted`] or [`Error::Overflow`].
    fn close_relax_lower(&mut self, r: usize) -> Result<(), Error> {
        let (n, c, delta) = (self.n, self.links[r].contingent.0, self.delta(r));
        let row = r * n;
        let mut queue = BinaryHeap::new();
        let mut settled = vec![false; n];
        let mut improved = vec![false; n];

        for p in 0..n {
            if let Some(w) = self.into[row + p] {
                queue.push(Reverse((self.key(p, w)?, p)));
            }
        }

        while let Some(Reverse((key, q))) = queue.pop() {
            let Some(wq) = self.into[row + q] else {
                return Err(Error::Internal);
            };

            if settled[q] || key != self.key(q, wq)? {
                continue;
            }

            settled[q] = true;

            if wq >= delta {
                continue;
            }

            for (p, v) in self.relax_lower(q, wq, c)? {
                if self.into[row + p].is_none_or(|old| v < old) {
                    self.into[row + p] = Some(v);
                    improved[p] = true;
                    queue.push(Reverse((self.key(p, v)?, p)));
                }
            }
        }

        for p in (0..n).filter(|&p| improved[p]) {
            let w = self.into[row + p].ok_or(Error::Internal)?;
            self.incoming[c].push((p, w));
        }

        Ok(())
    }

    /// `ApplyRelaxLower`: the edges `P -> c` from the path `P -> q -wq-> c`, `wq < Δ`.
    ///
    /// Lower⁻ if `q` is contingent, else Relax⁻ over every LO-edge into `q` not from `c`.
    ///
    /// # Errors
    ///
    /// [`Error::BudgetExhausted`] or [`Error::Overflow`].
    fn relax_lower(&mut self, q: usize, wq: Ticks, c: usize) -> Result<Vec<(usize, Ticks)>, Error> {
        if let Some(j) = self.link_of[q] {
            let link = &self.links[j];
            let v = link.lo.checked_add(wq).ok_or(Error::Overflow)?;
            let from = link.activation.0;
            return Ok(if from == c {
                Vec::new()
            } else {
                vec![(from, v)]
            });
        }

        self.meter.spend(steps(self.incoming[q].len())?)?;
        self.incoming[q]
            .iter()
            .filter(|&&(p, _)| p != c)
            .map(|&(p, w)| Ok((p, w.checked_add(wq).ok_or(Error::Overflow)?)))
            .collect()
    }

    /// `ApplyUpper`: Upper⁻ on every ordinary edge into `C_r`, adding edges into `A_r`.
    ///
    /// # Errors
    ///
    /// [`Error::BudgetExhausted`] or [`Error::Overflow`].
    fn apply_upper(&mut self, r: usize) -> Result<(), Error> {
        let link = self.links[r];
        let (delta, neg_lo) = (self.delta(r), link.lo.checked_neg().ok_or(Error::Overflow)?);
        self.meter.spend(steps(self.n)?)?;

        for p in 0..self.n {
            let Some(v) = self.into[r * self.n + p] else {
                continue;
            };

            let m = if v < delta {
                neg_lo
            } else {
                v.checked_sub(link.hi).ok_or(Error::Overflow)?
            };
            self.incoming[link.activation.0].push((p, m));
        }

        Ok(())
    }

    /// `UpdatePotential`: repairs `h` after new edges into `a`; false if `h[a]` itself must rise
    /// (a negative LO-cycle through `a`).
    ///
    /// Largest increase first: every edge not into `a` has non-negative reduced weight under
    /// the old `h`, so increases only shrink along a path, as in Dijkstra.
    ///
    /// # Errors
    ///
    /// [`Error::BudgetExhausted`] or [`Error::Overflow`].
    fn update_potential(&mut self, a: usize) -> Result<bool, Error> {
        let old = self.h.clone();
        let mut queue = BinaryHeap::new();
        queue.push((Ticks::ZERO, a));

        while let Some((raise, q)) = queue.pop() {
            if raise != self.h[q].checked_sub(old[q]).ok_or(Error::Overflow)? {
                continue;
            }

            self.meter.spend(steps(self.incoming[q].len())?)?;

            for i in 0..self.incoming[q].len() {
                let (p, w) = self.incoming[q][i];
                let need = self.h[q].checked_sub(w).ok_or(Error::Overflow)?;

                if need <= self.h[p] {
                    continue;
                }

                if p == a {
                    return Ok(false);
                }

                self.h[p] = need;
                queue.push((need.checked_sub(old[p]).ok_or(Error::Overflow)?, p));
            }
        }

        Ok(true)
    }

    /// `NegativeCycle`: whether some LO-edge violates the potential.
    ///
    /// # Errors
    ///
    /// [`Error::BudgetExhausted`] or [`Error::Overflow`].
    fn violates_potential(&mut self) -> Result<bool, Error> {
        self.meter.spend(steps(self.edge_count())?)?;

        for q in 0..self.n {
            for &(p, w) in &self.incoming[q] {
                if self.h[q].checked_sub(w).ok_or(Error::Overflow)? > self.h[p] {
                    return Ok(true);
                }
            }
        }

        Ok(false)
    }

    /// `Δ_r = u_r - l_r`, never negative.
    fn delta(&self, r: usize) -> Ticks {
        let link = &self.links[r];
        Ticks(link.hi.0 - link.lo.0)
    }

    /// Dijkstra key of `p` at distance `w` from the current target: `h[p] + w`.
    ///
    /// # Errors
    ///
    /// [`Error::Overflow`].
    fn key(&self, p: usize, w: Ticks) -> Result<Ticks, Error> {
        self.h[p].checked_add(w).ok_or(Error::Overflow)
    }

    /// Number of LO-edges.
    fn edge_count(&self) -> usize {
        self.incoming.iter().map(Vec::len).sum()
    }
}

/// Lowers `cell` to `value` if that is tighter.
fn lower(cell: &mut Option<Ticks>, value: Ticks) {
    if cell.is_none_or(|d| value < d) {
        *cell = Some(value);
    }
}