radiate_rust/engines/
engine_context.rs

1use crate::engines::genome::genes::gene::Gene;
2use crate::engines::genome::population::Population;
3use crate::engines::schema::timer::Timer;
4
5use super::score::Score;
6
7pub struct EngineContext<G, A, T>
8where
9    G: Gene<G, A>
10{
11    pub population: Population<G, A>,
12    pub best: T,
13    pub index: i32,
14    pub timer: Timer,
15    pub score: Option<Score>
16}
17
18impl<G, A, T> EngineContext<G, A, T> 
19where
20    G: Gene<G, A>
21{
22    pub fn score(&self) -> &Score {
23        self.score.as_ref().unwrap()
24    }
25
26    pub fn seconds(&self) -> f64 {
27        self.timer.elapsed().as_secs_f64()
28    }
29}
30
31impl<G, A, T> Clone for EngineContext<G, A, T> 
32where
33    G: Gene<G, A>,
34    T: Clone
35{
36    fn clone(&self) -> Self {
37        EngineContext {
38            population: self.population.clone(),
39            best: self.best.clone(),
40            index: self.index,
41            timer: self.timer.clone(),
42            score: self.score.clone()
43        }
44    }
45}
46
47impl<G, A, T: std::fmt::Debug> std::fmt::Debug for EngineContext<G, A, T> 
48where
49    G: Gene<G, A>
50{
51    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
52        write!(f, "EngineOutput {{\n")?;
53        write!(f, "  best: {:?},\n", self.best)?;
54        write!(f, "  score: {:?},\n", self.score())?;
55        write!(f, "  index: {:?},\n", self.index)?;
56        write!(f, "  size: {:?},\n", self.population.len())?;
57        write!(f, "  duration: {:?},\n", self.timer.elapsed())?;
58        write!(f, "}}")
59    }
60}