use std::time::Instant;
use std::collections::VecDeque;
pub struct TemporalLeadPredictor {
state_history: VecDeque<Vec<f64>>,
horizon_ns: u64,
speed_ratio: f64,
prediction_cache: Vec<(Instant, Vec<f64>)>,
}
impl TemporalLeadPredictor {
pub fn new(horizon_ns: u64, history_size: usize) -> Self {
Self {
state_history: VecDeque::with_capacity(history_size),
horizon_ns,
speed_ratio: 1.5, prediction_cache: Vec::new(),
}
}
pub fn predict_future(&mut self, current_state: Vec<f64>) -> Vec<f64> {
self.state_history.push_back(current_state.clone());
if self.state_history.len() > 100 {
self.state_history.pop_front();
}
if self.state_history.len() < 3 {
return current_state;
}
let n = self.state_history.len();
let mut future = vec![0.0; current_state.len()];
for dim in 0..current_state.len() {
let mut sum = 0.0;
let t_future = n as f64 + (self.horizon_ns as f64 / 1_000_000.0);
for i in n.saturating_sub(3)..n {
let mut prod = self.state_history[i][dim];
for j in n.saturating_sub(3)..n {
if i != j {
prod *= (t_future - j as f64) / (i as f64 - j as f64);
}
}
sum += prod;
}
future[dim] = sum;
}
self.prediction_cache.push((Instant::now(), future.clone()));
if self.prediction_cache.len() > 10 {
self.prediction_cache.remove(0);
}
future
}
pub fn temporal_advantage_ns(&self, distance_km: f64) -> i64 {
let light_travel_ns = (distance_km * 1_000_000.0 / 299_792.458) * 1_000_000.0;
let compute_time_ns = self.horizon_ns as f64 / self.speed_ratio;
(light_travel_ns - compute_time_ns) as i64
}
pub fn verify_prediction(&self, predicted: &[f64], actual: &[f64]) -> f64 {
if predicted.len() != actual.len() {
return 0.0;
}
let error: f64 = predicted.iter()
.zip(actual.iter())
.map(|(p, a)| (p - a).powi(2))
.sum::<f64>()
.sqrt();
1.0 / (1.0 + error)
}
}