aios_cli/benchmark_next_app/baselines/
frequency.rs1use std::collections::HashMap;
4use std::time::Instant;
5
6use crate::benchmark_next_app::types::{
7 NextAppLabel, NextAppPredictor, PredictionResult, ScoredPrediction,
8};
9
10use super::simple::cmp_score_desc;
11
12#[derive(Default)]
14pub struct GlobalMajorityBackend {
15 counts: HashMap<String, u32>,
16}
17
18impl NextAppPredictor for GlobalMajorityBackend {
19 fn name(&self) -> &'static str {
20 "global_majority"
21 }
22
23 fn train(&mut self, train: &[NextAppLabel]) {
24 for label in train {
25 if let Some(next) = label.actual_next_app.clone() {
26 *self.counts.entry(next).or_insert(0) += 1;
27 }
28 }
29 }
30
31 fn predict(
32 &self,
33 _ctx: &aios_spec::StructuredContext,
34 _current_app: &str,
35 candidates: &[String],
36 ) -> PredictionResult {
37 let start = Instant::now();
38 PredictionResult {
39 ranked: rank_by_counts(candidates, &self.counts),
40 latency_us: start.elapsed().as_micros() as u64,
41 rationale_present: false,
42 }
43 }
44}
45
46#[derive(Default)]
48pub struct PerCurrentAppMajorityBackend {
49 counts: HashMap<String, HashMap<String, u32>>,
50}
51
52impl NextAppPredictor for PerCurrentAppMajorityBackend {
53 fn name(&self) -> &'static str {
54 "per_current_app_majority"
55 }
56
57 fn train(&mut self, train: &[NextAppLabel]) {
58 for label in train {
59 if let Some(next) = label.actual_next_app.clone() {
60 *self
61 .counts
62 .entry(label.current_app.clone())
63 .or_default()
64 .entry(next)
65 .or_insert(0) += 1;
66 }
67 }
68 }
69
70 fn predict(
71 &self,
72 _ctx: &aios_spec::StructuredContext,
73 current_app: &str,
74 candidates: &[String],
75 ) -> PredictionResult {
76 let start = Instant::now();
77 let ranked = match self.counts.get(current_app) {
78 Some(counts) => rank_by_counts(candidates, counts),
79 None => candidates
80 .iter()
81 .map(|package| ScoredPrediction {
82 package: package.clone(),
83 score: 0.0,
84 })
85 .collect(),
86 };
87 PredictionResult {
88 ranked,
89 latency_us: start.elapsed().as_micros() as u64,
90 rationale_present: false,
91 }
92 }
93}
94
95#[derive(Default)]
97pub struct MarkovBackend {
98 transitions: HashMap<String, HashMap<String, u32>>,
99 totals: HashMap<String, u32>,
100}
101
102impl MarkovBackend {
103 fn rank_by_probability(
104 candidates: &[String],
105 counts: &HashMap<String, u32>,
106 total: u32,
107 ) -> Vec<ScoredPrediction> {
108 let total_f = total.max(1) as f32;
109 let mut scored: Vec<ScoredPrediction> = candidates
110 .iter()
111 .map(|package| ScoredPrediction {
112 package: package.clone(),
113 score: counts.get(package).copied().unwrap_or(0) as f32 / total_f,
114 })
115 .collect();
116 scored.sort_by(|a, b| cmp_score_desc(a, b).then_with(|| a.package.cmp(&b.package)));
117 scored
118 }
119}
120
121impl NextAppPredictor for MarkovBackend {
122 fn name(&self) -> &'static str {
123 "markov"
124 }
125
126 fn train(&mut self, train: &[NextAppLabel]) {
127 for label in train {
128 if let Some(next) = label.actual_next_app.clone() {
129 *self
130 .transitions
131 .entry(label.current_app.clone())
132 .or_default()
133 .entry(next)
134 .or_insert(0) += 1;
135 *self.totals.entry(label.current_app.clone()).or_insert(0) += 1;
136 }
137 }
138 }
139
140 fn predict(
141 &self,
142 _ctx: &aios_spec::StructuredContext,
143 current_app: &str,
144 candidates: &[String],
145 ) -> PredictionResult {
146 let start = Instant::now();
147 let ranked = match self.transitions.get(current_app) {
148 Some(counts) => {
149 let total = self.totals.get(current_app).copied().unwrap_or(0);
150 Self::rank_by_probability(candidates, counts, total)
151 },
152 None => candidates
153 .iter()
154 .map(|package| ScoredPrediction {
155 package: package.clone(),
156 score: 0.0,
157 })
158 .collect(),
159 };
160 PredictionResult {
161 ranked,
162 latency_us: start.elapsed().as_micros() as u64,
163 rationale_present: false,
164 }
165 }
166}
167
168pub(crate) fn rank_by_counts(
169 candidates: &[String],
170 counts: &HashMap<String, u32>,
171) -> Vec<ScoredPrediction> {
172 let mut scored: Vec<ScoredPrediction> = candidates
173 .iter()
174 .map(|package| ScoredPrediction {
175 package: package.clone(),
176 score: counts.get(package).copied().unwrap_or(0) as f32,
177 })
178 .collect();
179 scored.sort_by(|a, b| cmp_score_desc(a, b).then_with(|| a.package.cmp(&b.package)));
180 scored
181}
182
183#[cfg(test)]
184mod tests {
185 use super::*;
186
187 #[test]
188 fn global_majority_ranks_trained_app_first() {
189 let mut backend = GlobalMajorityBackend::default();
190 backend.train(&[
191 next_app_label("A", "B"),
192 next_app_label("A", "B"),
193 next_app_label("A", "C"),
194 ]);
195 let result = backend.predict(&empty_ctx(), "A", &["B".into(), "C".into()]);
196 assert_eq!(result.ranked[0].package, "B");
197 assert_eq!(result.ranked[0].score, 2.0);
198 }
199
200 #[test]
201 fn markov_uses_conditional_probability() {
202 let mut backend = MarkovBackend::default();
203 backend.train(&[
204 next_app_label("A", "B"),
205 next_app_label("A", "B"),
206 next_app_label("A", "C"),
207 ]);
208 let result = backend.predict(&empty_ctx(), "A", &["B".into(), "C".into()]);
209 assert_eq!(result.ranked[0].score, 2.0 / 3.0);
210 assert_eq!(result.ranked[1].score, 1.0 / 3.0);
211 }
212
213 fn next_app_label(current: &str, next: &str) -> NextAppLabel {
214 NextAppLabel {
215 dataset_id: "d1".into(),
216 scenario: "s1".into(),
217 window_start_ms: 0,
218 window_end_ms: 1,
219 prediction_horizon_ms: 30_000,
220 current_app: current.into(),
221 observable_candidates: vec![next.into()],
222 actual_next_app: Some(next.into()),
223 eligible: true,
224 excluded_reason: None,
225 }
226 }
227
228 fn empty_ctx() -> aios_spec::StructuredContext {
229 aios_spec::StructuredContext {
230 window_id: "w1".into(),
231 window_start_ms: 0,
232 window_end_ms: 1,
233 duration_secs: 1,
234 events: vec![],
235 summary: aios_spec::ContextSummary {
236 foreground_apps: vec![],
237 notified_apps: vec![],
238 all_semantic_hints: vec![],
239 file_activity: vec![],
240 latest_system_status: None,
241 source_tier: aios_spec::SourceTier::PublicApi,
242 },
243 }
244 }
245}