strange_loop/
temporal_lead.rs1use std::time::Instant;
4use std::collections::VecDeque;
5
6pub struct TemporalLeadPredictor {
8 state_history: VecDeque<Vec<f64>>,
10 horizon_ns: u64,
12 speed_ratio: f64,
14 prediction_cache: Vec<(Instant, Vec<f64>)>,
16}
17
18impl TemporalLeadPredictor {
19 pub fn new(horizon_ns: u64, history_size: usize) -> Self {
20 Self {
21 state_history: VecDeque::with_capacity(history_size),
22 horizon_ns,
23 speed_ratio: 1.5, prediction_cache: Vec::new(),
25 }
26 }
27
28 pub fn predict_future(&mut self, current_state: Vec<f64>) -> Vec<f64> {
30 self.state_history.push_back(current_state.clone());
31 if self.state_history.len() > 100 {
32 self.state_history.pop_front();
33 }
34
35 if self.state_history.len() < 3 {
37 return current_state;
38 }
39
40 let n = self.state_history.len();
41 let mut future = vec![0.0; current_state.len()];
42
43 for dim in 0..current_state.len() {
45 let mut sum = 0.0;
46 let t_future = n as f64 + (self.horizon_ns as f64 / 1_000_000.0);
47
48 for i in n.saturating_sub(3)..n {
50 let mut prod = self.state_history[i][dim];
51 for j in n.saturating_sub(3)..n {
52 if i != j {
53 prod *= (t_future - j as f64) / (i as f64 - j as f64);
54 }
55 }
56 sum += prod;
57 }
58 future[dim] = sum;
59 }
60
61 self.prediction_cache.push((Instant::now(), future.clone()));
63 if self.prediction_cache.len() > 10 {
64 self.prediction_cache.remove(0);
65 }
66
67 future
68 }
69
70 pub fn temporal_advantage_ns(&self, distance_km: f64) -> i64 {
72 let light_travel_ns = (distance_km * 1_000_000.0 / 299_792.458) * 1_000_000.0;
73 let compute_time_ns = self.horizon_ns as f64 / self.speed_ratio;
74 (light_travel_ns - compute_time_ns) as i64
75 }
76
77 pub fn verify_prediction(&self, predicted: &[f64], actual: &[f64]) -> f64 {
79 if predicted.len() != actual.len() {
80 return 0.0;
81 }
82
83 let error: f64 = predicted.iter()
84 .zip(actual.iter())
85 .map(|(p, a)| (p - a).powi(2))
86 .sum::<f64>()
87 .sqrt();
88
89 1.0 / (1.0 + error)
90 }
91}