Skip to main content

rust_robotics_planning/experiments/moving_ai_runtime/
percentile_bucket.rs

1use super::{RuntimeAggregationVariant, VariantDescriptor};
2
3#[derive(Debug, Clone)]
4pub struct PercentileBucketRuntimeAggregation {
5    percentiles: Vec<f64>,
6}
7
8impl PercentileBucketRuntimeAggregation {
9    pub fn new(percentiles: Vec<f64>) -> Self {
10        Self { percentiles }
11    }
12}
13
14impl RuntimeAggregationVariant for PercentileBucketRuntimeAggregation {
15    fn descriptor(&self) -> VariantDescriptor {
16        VariantDescriptor {
17            id: "percentile-bucket",
18            design_style: "functional-percentile",
19            source_path: concat!(
20                env!("CARGO_MANIFEST_DIR"),
21                "/src/experiments/moving_ai_runtime/percentile_bucket.rs"
22            ),
23            knob_count: 1,
24            reports_dispersion: true,
25        }
26    }
27
28    fn selected_slots(&self, total_scenarios: usize) -> Vec<usize> {
29        if total_scenarios == 0 {
30            return Vec::new();
31        }
32        if total_scenarios == 1 {
33            return vec![0];
34        }
35
36        let max_index = (total_scenarios - 1) as f64;
37        let mut slots = self
38            .percentiles
39            .iter()
40            .copied()
41            .map(|percentile| percentile.clamp(0.0, 1.0))
42            .map(|percentile| (max_index * percentile).round() as usize)
43            .collect::<Vec<_>>();
44        slots.sort_unstable();
45        slots.dedup();
46        slots
47    }
48}