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};
const TEST_BUDGET: Budget = Budget {
max_steps: 50_000_000,
max_cells: 1 << 20,
};
fn estnu(stnu: &Stnu) -> Option<DispatchableNetwork> {
estnu_with(stnu, DispatchAlgorithm::default())
}
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,
}
}
const DISPATCHERS: [DispatchAlgorithm; 3] = [
DispatchAlgorithm::Minimal,
DispatchAlgorithm::Morris2014,
DispatchAlgorithm::Closure,
];
const ALGORITHMS: [DcAlgorithm; 4] = [
DcAlgorithm::Morris2014,
DcAlgorithm::RulMinus,
DcAlgorithm::Rul2021,
DcAlgorithm::PerLabelBackprop,
];
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(())
)
}
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),
}
}
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:?}");
}
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}");
}
}
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
}
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
}
#[test]
fn must_precede_unknown_end_is_not_dc() {
let stnu = wait_example(1, 2, None);
assert!(estnu(&stnu).is_none());
let cycle = explanation(&stnu).expect("not DC");
assert_cycle_closes(&stnu, &cycle);
assert!(
cycle
.edges()
.any(|e| e == LabelledEdge::UpperCase(ContingentId(0)))
);
}
#[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, ×);
}
}
}
}
#[test]
fn wait_past_deadline_is_not_dc() {
assert!(estnu(&wait_example(-10, 2, Some(7))).is_none());
}
#[test]
fn same_tick_reaction_is_dc() {
assert!(estnu(&wait_example(0, 0, None)).is_some());
}
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
}
#[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, ×);
}
}
#[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());
}
#[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)));
}
}
}
}
#[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);
}
#[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)
);
}
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:?}"
);
}
}
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();
if let Some(net) = estnu_with(&spec.build(), DispatchAlgorithm::Closure) {
dc += 1;
with_waits += usize::from(!net.waits().is_empty());
}
}
(dc, with_waits)
}
#[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");
}
}
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
}
#[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);
}
}
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, ×);
}
}
}
proptest! {
#[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);
}
}
#[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);
}
}
#[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);
}
}
}
#[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));
}
}
}
}
#[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));
}
}
#[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);
}
}
}
#[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));
}
}
#[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));
}
}
#[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());
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));
}
}
}
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()
}
}