Skip to main content

rust_robotics_planning/experiments/moving_ai_runtime/
variance_triggered.rs

1use super::{RuntimeAggregationVariant, RuntimeSamplingPlan, VariantDescriptor};
2
3#[derive(Debug, Clone)]
4pub struct VarianceTriggeredRuntimeAggregation {
5    initial_slots: Vec<usize>,
6    ratio_margin_threshold: f64,
7}
8
9impl VarianceTriggeredRuntimeAggregation {
10    pub fn new(initial_slots: Vec<usize>, ratio_margin_threshold: f64) -> Self {
11        Self {
12            initial_slots,
13            ratio_margin_threshold,
14        }
15    }
16}
17
18impl RuntimeAggregationVariant for VarianceTriggeredRuntimeAggregation {
19    fn descriptor(&self) -> VariantDescriptor {
20        VariantDescriptor {
21            id: "variance-triggered",
22            design_style: "adaptive-two-stage",
23            source_path: concat!(
24                env!("CARGO_MANIFEST_DIR"),
25                "/src/experiments/moving_ai_runtime/variance_triggered.rs"
26            ),
27            knob_count: 2,
28            reports_dispersion: true,
29        }
30    }
31
32    fn selected_slots(&self, total_scenarios: usize) -> Vec<usize> {
33        let mut slots = self
34            .initial_slots
35            .iter()
36            .copied()
37            .filter(|slot| *slot < total_scenarios)
38            .collect::<Vec<_>>();
39        slots.sort_unstable();
40        slots.dedup();
41        if slots.is_empty() && total_scenarios > 0 {
42            vec![0]
43        } else {
44            slots
45        }
46    }
47
48    fn sampling_plan(&self, total_scenarios: usize) -> RuntimeSamplingPlan {
49        RuntimeSamplingPlan {
50            initial_slots: self.selected_slots(total_scenarios),
51            escalation_slots: (0..total_scenarios).collect(),
52            escalate_if_vote_split: true,
53            escalate_if_ratio_margin_below: Some(self.ratio_margin_threshold),
54        }
55    }
56}