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
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
//! Dynamic controllability: textbook examples, oracle cross-checks and simulated dispatch.

use proptest::prelude::*;
use temporal_control::{
    Bound, Budget, ContingentId, Controllability, CycleStep, DcAlgorithm, DispatchAlgorithm,
    DispatchableNetwork, Error, LabelledEdge, SemiReducibleCycle, Stnu, Ticks, TimePoint,
};

use crate::oracle::{self, Reaction};
use crate::sim::{self, Agent, Nature, Rng};
use crate::stnu_gen::{StnuSpec, medium_stnu, small_stnu};

/// Budget for checks on generated networks: generous, but a hang fails the test instead.
const TEST_BUDGET: Budget = Budget {
    max_steps: 50_000_000,
    max_cells: 1 << 20,
};

/// The default extended dispatchable network, or `None` if not DC. Panics on errors.
fn estnu(stnu: &Stnu) -> Option<DispatchableNetwork> {
    estnu_with(stnu, DispatchAlgorithm::default())
}

/// The extended dispatchable network of `algorithm`, or `None` if not DC. Panics on errors.
fn estnu_with(stnu: &Stnu, algorithm: DispatchAlgorithm) -> Option<DispatchableNetwork> {
    match stnu
        .dispatchable_with(algorithm, &TEST_BUDGET)
        .unwrap_or_else(|e| panic!("{algorithm:?}: {e}"))
    {
        Controllability::Controllable(net) => Some(net),
        Controllability::Uncontrollable(_) => None,
    }
}

/// Every algorithm behind [`Stnu::dispatchable_with`].
const DISPATCHERS: [DispatchAlgorithm; 3] = [
    DispatchAlgorithm::Minimal,
    DispatchAlgorithm::Morris2014,
    DispatchAlgorithm::Closure,
];

/// Every checker behind [`Stnu::check_controllability_with`].
const ALGORITHMS: [DcAlgorithm; 4] = [
    DcAlgorithm::Morris2014,
    DcAlgorithm::RulMinus,
    DcAlgorithm::Rul2021,
    DcAlgorithm::PerLabelBackprop,
];

/// Verdict of `algorithm`. Panics on budget exhaustion or an internal disagreement.
fn is_dc(stnu: &Stnu, algorithm: DcAlgorithm) -> bool {
    let verdict = stnu.check_controllability_with(algorithm, &TEST_BUDGET);
    matches!(
        verdict.unwrap_or_else(|e| panic!("{algorithm:?}: {e}")),
        Controllability::Controllable(())
    )
}

/// The explanation of [`Stnu::check_controllability`], or `None` if DC.
fn explanation(stnu: &Stnu) -> Option<SemiReducibleCycle> {
    match stnu
        .check_controllability(&TEST_BUDGET)
        .expect("Morris fits the test budget")
    {
        Controllability::Controllable(()) => None,
        Controllability::Uncontrollable(cycle) => Some(cycle),
    }
}

/// Asserts that `cycle` chains, closes and has the stated negative weight in `stnu`.
pub(crate) fn assert_cycle_closes(stnu: &Stnu, cycle: &SemiReducibleCycle) {
    let edges: Vec<_> = cycle
        .edges()
        .map(|e| e.resolve(stnu).expect("edge belongs to the network"))
        .collect();
    assert_ne!(edges.len(), 0, "empty cycle");
    assert_eq!(cycle.expanded_len(), Some(edges.len()));
    assert_derived_edges_hold(stnu, cycle);

    for (i, &(_, head, _)) in edges.iter().enumerate() {
        let (tail, _, _) = edges[(i + 1) % edges.len()];
        assert_eq!(head, tail, "edge {i} does not chain in {cycle:?}");
    }

    let weight: i64 = edges.iter().map(|e| e.2.0).sum();
    assert_eq!(Ticks(weight), cycle.weight());
    assert!(weight < 0, "{cycle:?}");
}

/// Asserts that every derived edge of `cycle` refers only to earlier ones and that its path
/// chains from its tail to its head with its stated weight.
fn assert_derived_edges_hold(stnu: &Stnu, cycle: &SemiReducibleCycle) {
    let ends = |step: &CycleStep| match *step {
        CycleStep::Edge(e) => e.resolve(stnu).unwrap(),
        CycleStep::Derived(i) => {
            let d = &cycle.derived()[i];
            (d.from, d.to, d.weight)
        }
    };

    for (i, d) in cycle.derived().iter().enumerate() {
        assert_ne!(d.steps.len(), 0, "empty derived edge {i}");
        let mut at = d.from;
        let mut weight = 0;

        for step in &d.steps {
            if let CycleStep::Derived(j) = step {
                assert!(*j < i, "derived edge {i} refers to later {j}");
            }

            let (tail, head, w) = ends(step);
            assert_eq!(tail, at, "derived edge {i} does not chain");
            at = head;
            weight += w.0;
        }

        assert_eq!((at, Ticks(weight)), (d.to, d.weight), "derived edge {i}");
    }
}

/// `stnu` with all time points and contingent links, but only the requirement bounds that
/// `cycle` names.
fn restrict(stnu: &Stnu, cycle: &SemiReducibleCycle) -> Stnu {
    let mut out = Stnu::new();

    for p in 0..stnu.time_point_count() {
        match stnu.links().iter().find(|l| l.contingent.0 == p) {
            Some(link) => out.add_contingent_link(link.activation, link.lo, link.hi),
            None => out.add_time_point(),
        }
        .unwrap();
    }

    for edge in cycle.edges() {
        if let LabelledEdge::Requirement(e) = edge {
            let c = stnu.requirements().constraint(e.constraint).unwrap();
            let (lo, hi) = match e.bound {
                Bound::Upper => (None, c.hi),
                Bound::Lower => (c.lo, None),
            };
            out.add_constraint(c.from, c.to, lo, hi).unwrap();
        }
    }

    out
}

/// Contingent `A => C` in `[1, 10]` and executable `X` with `C - X` in `[lo, hi]` plus an
/// optional deadline `X - A <= deadline`.
fn wait_example(lo: i64, hi: i64, deadline: Option<i64>) -> Stnu {
    let mut stnu = Stnu::new();
    let a = stnu.add_time_point().unwrap();
    let c = stnu.add_contingent_link(a, Ticks(1), Ticks(10)).unwrap();
    let x = stnu.add_time_point().unwrap();
    stnu.add_constraint(x, c, Some(Ticks(lo)), Some(Ticks(hi)))
        .unwrap();
    stnu.add_constraint(a, x, Some(Ticks(0)), deadline.map(Ticks))
        .unwrap();
    stnu
}

/// `X` must precede `C` by 1 to 2 ticks without knowing `C`: not DC.
#[test]
fn must_precede_unknown_end_is_not_dc() {
    let stnu = wait_example(1, 2, None);
    assert!(estnu(&stnu).is_none());

    // The cycle needs the maximum duration of the link: X cannot wait for C.
    let cycle = explanation(&stnu).expect("not DC");
    assert_cycle_closes(&stnu, &cycle);
    assert!(
        cycle
            .edges()
            .any(|e| e == LabelledEdge::UpperCase(ContingentId(0)))
    );
}

/// `X` within 2 ticks before or after `C` and `X <= A + 8`: DC by waiting for `C` until `A + 8`.
#[test]
fn wait_until_deadline_is_dc() {
    let stnu = wait_example(-10, 2, Some(8));

    for algorithm in DISPATCHERS {
        let net = estnu_with(&stnu, algorithm).expect("DC");
        let wait = net.waits().iter().find(|w| w.waiter == TimePoint(2));
        assert_eq!(wait.map(|w| w.delay), Some(Ticks(8)), "{algorithm:?}");

        for agent in [Agent::Asap, Agent::Latest, Agent::Random] {
            for nature in [Nature::Min, Nature::Max, Nature::Random] {
                let times = sim::run(&net, agent, nature, &mut Rng(11));
                assert_satisfies(&stnu, &times);
            }
        }
    }
}

/// With deadline `A + 7` the agent must commit before it can be sure: not DC.
#[test]
fn wait_past_deadline_is_not_dc() {
    assert!(estnu(&wait_example(-10, 2, Some(7))).is_none());
}

/// `X` exactly at `C` is DC only with same-tick reaction, which these semantics allow.
#[test]
fn same_tick_reaction_is_dc() {
    assert!(estnu(&wait_example(0, 0, None)).is_some());
}

/// The rover: warm-up `[5, 15]`, analysis starts at most 5 ticks after warm-up ends and takes
/// `[10, 25]`, transmit after analysis by a deadline from the start. DC iff deadline >= 40.
fn rover(deadline: i64) -> Stnu {
    let mut stnu = Stnu::new();
    let start = stnu.add_time_point().unwrap();
    let warm = stnu
        .add_contingent_link(start, Ticks(5), Ticks(15))
        .unwrap();
    let analysis = stnu.add_time_point().unwrap();
    let done = stnu
        .add_contingent_link(analysis, Ticks(10), Ticks(25))
        .unwrap();
    let transmit = stnu.add_time_point().unwrap();
    stnu.add_constraint(warm, analysis, Some(Ticks(0)), Some(Ticks(5)))
        .unwrap();
    stnu.add_constraint(done, transmit, Some(Ticks(0)), None)
        .unwrap();
    stnu.add_constraint(start, transmit, Some(Ticks(0)), Some(Ticks(deadline)))
        .unwrap();
    stnu
}

/// The rover deadline threshold, and dispatch under extreme durations.
#[test]
fn rover_deadline_threshold() {
    let late = rover(39);
    assert!(estnu(&late).is_none());
    let cycle = explanation(&late).expect("not DC");
    assert_cycle_closes(&late, &cycle);
    assert_eq!(cycle.weight(), Ticks(-1), "one tick short");
    assert!(estnu(&restrict(&late, &cycle)).is_none());

    let stnu = rover(40);
    let net = estnu(&stnu).expect("DC");

    for nature in [Nature::Min, Nature::Max, Nature::Random] {
        let times = sim::run(&net, Agent::Random, nature, &mut Rng(3));
        assert_satisfies(&stnu, &times);
    }
}

/// Without contingent links, DC equals STN consistency.
#[test]
fn no_links_matches_stn() {
    let mut stnu = Stnu::new();
    let a = stnu.add_time_point().unwrap();
    let b = stnu.add_time_point().unwrap();
    stnu.add_constraint(a, b, Some(Ticks(3)), Some(Ticks(2)))
        .unwrap();
    assert!(estnu(&stnu).is_none());
}

/// `X1` and `X2` follow `C` (`Z => C` in `[1, 7]`) closely and must happen by `Z + 6`. The
/// paper's diamond stand-in `X1 -> X2: 0` would let the minimisation drop `X2`'s deadline,
/// which this dispatcher (no propagation along waits) then never learns.
#[test]
fn minimal_keeps_deadlines_behind_waits() {
    let mut stnu = Stnu::new();
    let z = stnu.add_time_point().unwrap();
    let x1 = stnu.add_time_point().unwrap();
    let x2 = stnu.add_time_point().unwrap();
    let c = stnu.add_contingent_link(z, Ticks(1), Ticks(7)).unwrap();
    stnu.add_constraint(x1, c, Some(Ticks(-1)), Some(Ticks(1)))
        .unwrap();
    stnu.add_constraint(x2, c, Some(Ticks(0)), Some(Ticks(1)))
        .unwrap();

    for x in [x1, x2] {
        stnu.add_constraint(z, x, Some(Ticks(0)), Some(Ticks(6)))
            .unwrap();
    }

    let net = estnu(&stnu).expect("DC");

    for agent in [Agent::Asap, Agent::Latest, Agent::Random] {
        for nature in [Nature::Min, Nature::Max, Nature::Random] {
            for seed in 1..20 {
                assert_satisfies(&stnu, &sim::run(&net, agent, nature, &mut Rng(seed)));
            }
        }
    }
}

/// A chain `Z => C1 => C2`, each `[1, 2]`, with `C2 <= Z`: not DC. RUL⁻ needs its activation
/// points normalised to executable ones to see this.
#[test]
fn chained_links_are_checked() {
    let mut stnu = Stnu::new();
    let z = stnu.add_time_point().unwrap();
    let c1 = stnu.add_contingent_link(z, Ticks(1), Ticks(2)).unwrap();
    let c2 = stnu.add_contingent_link(c1, Ticks(1), Ticks(2)).unwrap();
    stnu.add_constraint(c2, z, Some(Ticks(0)), None).unwrap();

    for algorithm in ALGORITHMS {
        assert!(!is_dc(&stnu, algorithm), "{algorithm:?}");
    }

    let cycle = explanation(&stnu).expect("not DC");
    assert_cycle_closes(&stnu, &cycle);
}

/// Invalid contingent bounds are rejected.
#[test]
fn invalid_links_rejected() {
    let mut stnu = Stnu::new();
    let a = stnu.add_time_point().unwrap();
    assert_eq!(
        stnu.add_contingent_link(a, Ticks(0), Ticks(3)),
        Err(Error::InvalidContingentLink)
    );
    assert_eq!(
        stnu.add_contingent_link(a, Ticks(4), Ticks(3)),
        Err(Error::InvalidContingentLink)
    );
    assert_eq!(
        stnu.add_contingent_link(TimePoint(9), Ticks(1), Ticks(3)),
        Err(Error::UnknownTimePoint)
    );
}

/// Asserts that `times` satisfies every requirement and contingent bound of `stnu`.
fn assert_satisfies(stnu: &Stnu, times: &[Ticks]) {
    sim::assert_satisfies(stnu.requirements(), times);

    for link in stnu.links() {
        let d = times[link.contingent.0].0 - times[link.activation.0].0;
        assert!(
            link.lo.0 <= d && d <= link.hi.0,
            "{link:?} broken by {times:?}"
        );
    }
}

/// Counts `(DC, DC with waits)` among `samples` networks drawn from `strategy`.
fn coverage(strategy: &impl Strategy<Value = StnuSpec>, samples: usize) -> (usize, usize) {
    use proptest::strategy::ValueTree;
    use proptest::test_runner::TestRunner;

    let mut runner = TestRunner::deterministic();
    let (mut dc, mut with_waits) = (0, 0);

    for _ in 0..samples {
        let spec = strategy.new_tree(&mut runner).unwrap().current();

        // The closure keeps every wait, so it measures how often waits are needed at all.
        if let Some(net) = estnu_with(&spec.build(), DispatchAlgorithm::Closure) {
            dc += 1;
            with_waits += usize::from(!net.waits().is_empty());
        }
    }

    (dc, with_waits)
}

/// Both generators yield both verdicts, and DC networks with waits, often enough.
#[test]
fn generators_cover_verdicts_and_waits() {
    let samples = 400;

    for (name, (dc, waits)) in [
        ("small", coverage(&small_stnu(), samples)),
        ("medium", coverage(&medium_stnu(), samples)),
    ] {
        assert!(dc > samples / 5 && dc < samples * 4 / 5, "{name}: {dc} DC");
        assert!(waits > samples / 20, "{name}: {waits} with waits");
    }
}

/// Total edges plus waits of every algorithm's network over the DC samples of `strategy`.
fn sizes(strategy: &impl Strategy<Value = StnuSpec>, samples: usize) -> [usize; 3] {
    use proptest::strategy::ValueTree;
    use proptest::test_runner::TestRunner;

    let mut runner = TestRunner::deterministic();
    let mut total = [0; 3];

    for _ in 0..samples {
        let stnu = strategy.new_tree(&mut runner).unwrap().current().build();

        for (i, algorithm) in DISPATCHERS.into_iter().enumerate() {
            if let Some(net) = estnu_with(&stnu, algorithm) {
                total[i] += net.edges().len() + net.waits().len();
            }
        }
    }

    total
}

/// The minimised network is smaller than Morris's, which is smaller than the closure's, in
/// total over random networks (not necessarily for each one).
#[test]
fn minimisation_shrinks_networks() {
    for strategy in [small_stnu().boxed(), medium_stnu().boxed()] {
        let [minimal, morris, closure] = sizes(&strategy, 400);
        println!("edges + waits: minimal {minimal}, Morris {morris}, closure {closure}");
        assert!(minimal < morris && morris < closure);
    }
}

/// Runs the dispatcher on `spec` under every policy pair.
fn simulate_all(spec: &StnuSpec, net: &DispatchableNetwork, seed: u64) {
    let stnu = spec.build();
    let mut rng = Rng(seed);

    for agent in [Agent::Asap, Agent::Latest, Agent::Random] {
        for nature in [Nature::Min, Nature::Max, Nature::Random] {
            let times = sim::run(net, agent, nature, &mut rng);
            assert_satisfies(&stnu, &times);
        }
    }
}

proptest! {
    /// Every checker agrees with the brute-force game under same-tick reaction.
    #[test]
    fn checkers_match_oracle(spec in small_stnu()) {
        let expected = oracle::is_dc(&spec, Reaction::Instant);
        let stnu = spec.build();

        for algorithm in DISPATCHERS {
            prop_assert_eq!(estnu_with(&stnu, algorithm).is_some(), expected, "{:?}", algorithm);
        }

        for algorithm in ALGORITHMS {
            prop_assert_eq!(is_dc(&stnu, algorithm), expected, "{:?}", algorithm);
        }
    }

    /// Links with `lo == hi`, which Morris (2014), MMV (2001) and Hunsberger (2009) exclude but
    /// this crate accepts: every checker and dispatchable-network algorithm still matches the
    /// game under both reaction semantics, and the networks dispatch correctly.
    #[test]
    fn rigid_links_match_oracle(spec in small_stnu(), mask in 0u8.., seed in 1u64..) {
        let mut spec = spec;

        for (i, link) in spec.links.iter_mut().enumerate() {
            if mask & (1 << (i % 8)) != 0 {
                link.2 = link.1;
            }
        }

        let stnu = spec.build();
        let expected = oracle::is_dc(&spec, Reaction::Instant);

        for algorithm in DISPATCHERS {
            let net = estnu_with(&stnu, algorithm);
            prop_assert_eq!(net.is_some(), expected, "{:?}", algorithm);

            if let Some(net) = net {
                simulate_all(&spec, &net, seed);
            }
        }

        for algorithm in ALGORITHMS {
            prop_assert_eq!(is_dc(&stnu, algorithm), expected, "{:?}", algorithm);
        }

        let shifted = stnu
            .with_reaction(temporal_control::Reaction::NextTick)
            .expect("small bounds");
        let expected = oracle::is_dc(&spec, Reaction::NextTick);

        for algorithm in ALGORITHMS {
            prop_assert_eq!(is_dc(&shifted, algorithm), expected, "{:?}", algorithm);
        }
    }

    /// On networks too large for the oracle, every checker still agrees with the closure, and
    /// the closure's network dispatches without violations.
    #[test]
    fn checkers_agree_on_larger_networks(spec in medium_stnu(), seed in 1u64..) {
        let stnu = spec.build();
        let expected = estnu_with(&stnu, DispatchAlgorithm::Closure).is_some();

        for algorithm in ALGORITHMS {
            prop_assert_eq!(expected, is_dc(&stnu, algorithm), "{:?}", algorithm);
        }

        for algorithm in DISPATCHERS {
            let net = estnu_with(&stnu, algorithm);
            prop_assert_eq!(net.is_some(), expected, "{:?}", algorithm);

            if let Some(net) = net {
                simulate_all(&spec, &net, seed);
            }
        }
    }

    /// Next-tick reaction by transformation: every checker on the transformed network matches
    /// the game with the next-tick move order, and its dispatchable network (fed observations
    /// one tick late, i.e. as the shifted points) satisfies every requirement.
    #[test]
    fn next_tick_matches_oracle(spec in small_stnu(), seed in 1u64..) {
        let expected = oracle::is_dc(&spec, Reaction::NextTick);
        let shifted = spec
            .build()
            .with_reaction(temporal_control::Reaction::NextTick)
            .expect("small bounds");

        for algorithm in ALGORITHMS {
            prop_assert_eq!(is_dc(&shifted, algorithm), expected, "{:?}", algorithm);
        }

        let net = estnu(&shifted);
        prop_assert_eq!(net.is_some(), expected);

        if let Some(net) = net {
            let mut rng = Rng(seed);

            for agent in [Agent::Asap, Agent::Latest, Agent::Random] {
                for nature in [Nature::Min, Nature::Max, Nature::Random] {
                    assert_satisfies(&shifted, &sim::run(&net, agent, nature, &mut rng));
                }
            }
        }
    }

    /// Next-tick reaction is stricter: whatever it can control, same-tick reaction can too.
    #[test]
    fn next_tick_is_stricter(spec in small_stnu()) {
        if oracle::is_dc(&spec, Reaction::NextTick) {
            prop_assert!(oracle::is_dc(&spec, Reaction::Instant));
        }
    }

    /// Dispatching a DC network never violates a constraint, whatever nature and the agent do.
    #[test]
    fn dispatch_never_violates(spec in small_stnu(), seed in 1u64..) {
        for algorithm in DISPATCHERS {
            if let Some(net) = estnu_with(&spec.build(), algorithm) {
                simulate_all(&spec, &net, seed);
            }
        }
    }

    /// Under any budget the result is a budget error or the unlimited result.
    #[test]
    fn budget_never_changes_verdict(spec in small_stnu(), max_steps in 0u64..5_000) {
        let stnu = spec.build();
        let budget = Budget { max_steps, max_cells: usize::MAX };
        for algorithm in DISPATCHERS {
            let limited = stnu.dispatchable_with(algorithm, &budget);
            prop_assert!(limited == Err(Error::BudgetExhausted)
                || limited == stnu.dispatchable_with(algorithm, &TEST_BUDGET));
        }

        for algorithm in ALGORITHMS {
            let limited = stnu.check_controllability_with(algorithm, &budget);
            prop_assert!(limited == Err(Error::BudgetExhausted)
                || limited == stnu.check_controllability_with(algorithm, &TEST_BUDGET));
        }
    }

    /// A small network's explanation closes, and its requirement bounds alone (with all links
    /// and the oracle's horizon) are already not DC for the game.
    #[test]
    fn explanation_is_certificate_for_oracle(spec in small_stnu()) {
        if let Some(cycle) = explanation(&spec.build()) {
            assert_cycle_closes(&spec.build(), &cycle);
            prop_assert!(!oracle::is_dc(&restrict_spec(&spec, &cycle), Reaction::Instant));
        }
    }

    /// On larger networks, the cited requirement bounds alone are not DC for the closure.
    #[test]
    fn explanation_is_certificate(spec in medium_stnu()) {
        let stnu = spec.build();

        if let Some(cycle) = explanation(&stnu) {
            assert_cycle_closes(&stnu, &cycle);
            prop_assert!(estnu(&restrict(&stnu, &cycle)).is_none());

            // Compact: at most one entry per derived edge, each a path of at most n' steps.
            let nodes = stnu.time_point_count() + stnu.links().len();
            let compact = cycle.steps().len()
                + cycle.derived().iter().map(|d| d.steps.len()).sum::<usize>();
            prop_assert!(cycle.derived().len() <= nodes * nodes);
            prop_assert!(compact <= nodes * (nodes * nodes + 1));
        }
    }
}

/// `spec` with only the requirement bounds that `cycle` names, in the oracle's format.
///
/// The horizon constraints come back with [`StnuSpec::build`]; they only make the
/// network harder to control, so the check stays sound, just slightly weaker.
fn restrict_spec(spec: &StnuSpec, cycle: &SemiReducibleCycle) -> StnuSpec {
    let all: Vec<_> = spec.all_constraints().collect();
    let constraints = cycle
        .edges()
        .filter_map(|edge| match edge {
            LabelledEdge::Requirement(e) => {
                let (u, v, lo, hi) = all[e.constraint.0];
                Some(match e.bound {
                    Bound::Upper => (u, v, None, hi),
                    Bound::Lower => (u, v, lo, None),
                })
            }
            _ => None,
        })
        .collect();

    StnuSpec {
        constraints,
        ..spec.clone()
    }
}