1use crate::{Generation, Limit, context::EvolutionContext};
2use radiate_core::{Chromosome, EngineState, MetricSet, Objective, Score};
3use std::{fmt::Debug, sync::Arc};
4
5#[derive(Clone, Debug)]
6pub struct EngineStateChange {
7 pub index: usize,
8 pub from: EngineState,
9 pub to: EngineState,
10}
11
12#[derive(Clone, PartialEq, Eq, Debug)]
13pub struct CheckpointSaved {
14 pub index: usize,
15 pub path: String,
16}
17
18#[derive(Clone, PartialEq, Eq, Debug)]
19pub struct Warning(pub String);
20
21#[derive(Clone, Debug)]
22pub struct LimitTriggered(pub usize, pub Limit);
23
24#[derive(Clone, Debug)]
25pub struct EpochStart(pub usize);
26
27impl<C, T> From<&EvolutionContext<C, T>> for EpochStart
28where
29 C: Chromosome,
30 T: Clone,
31{
32 fn from(ctx: &EvolutionContext<C, T>) -> Self {
33 EpochStart(ctx.index)
34 }
35}
36
37#[derive(Clone)]
38pub struct Improvement<T> {
39 pub index: usize,
40 pub best: T,
41 pub score: Score,
42}
43
44impl<T> Debug for Improvement<T> {
45 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
46 write!(f, "Improved(index={}, score={:?})", self.index, self.score)
47 }
48}
49
50impl<C, T> From<&EvolutionContext<C, T>> for Improvement<T>
51where
52 C: Chromosome,
53 T: Clone,
54{
55 fn from(ctx: &EvolutionContext<C, T>) -> Self {
56 Improvement {
57 index: ctx.index,
58 best: ctx.best.clone(),
59 score: ctx.score.clone().unwrap_or_default(),
60 }
61 }
62}
63
64#[derive(Clone)]
65pub struct EpochComplete<T> {
66 pub index: usize,
67 pub best: T,
68 pub metrics: MetricSet,
69 pub score: Score,
70 pub objective: Objective,
71}
72
73impl<C, T> From<&EvolutionContext<C, T>> for EpochComplete<T>
74where
75 C: Chromosome,
76 T: Clone,
77{
78 fn from(ctx: &EvolutionContext<C, T>) -> Self {
79 EpochComplete {
80 index: ctx.index,
81 best: ctx.best.clone(),
82 metrics: ctx.metrics.clone(),
83 score: ctx.score.clone().unwrap_or_default(),
84 objective: ctx.objective.clone(),
85 }
86 }
87}
88
89impl<T> Debug for EpochComplete<T> {
90 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
91 write!(
92 f,
93 "EpochCompleted(index={}, score={:?}, objective={:?})",
94 self.index, self.score, self.objective
95 )
96 }
97}
98
99#[derive(Clone, Debug)]
100pub struct EngineStart;
101
102#[derive(Clone)]
103pub struct EngineStop<T> {
104 pub index: usize,
105 pub best: T,
106 pub metrics: MetricSet,
107 pub score: Score,
108}
109
110impl<C, T> From<&EvolutionContext<C, T>> for EngineStop<T>
111where
112 C: Chromosome,
113 T: Clone,
114{
115 fn from(ctx: &EvolutionContext<C, T>) -> Self {
116 EngineStop {
117 index: ctx.index,
118 best: ctx.best.clone(),
119 metrics: ctx.metrics.clone(),
120 score: ctx.score.clone().unwrap_or_default(),
121 }
122 }
123}
124
125impl<T> Debug for EngineStop<T> {
126 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
127 write!(
128 f,
129 "EngineStopped(index={}, score={:?})",
130 self.index, self.score
131 )
132 }
133}
134
135#[derive(Clone, Debug)]
136pub struct GenerationSnapshot<C, T>
137where
138 C: Chromosome + 'static,
139 T: Clone + Send + Sync + 'static,
140{
141 pub generation: Arc<Generation<C, T>>,
142}
143
144impl<C, T> From<&EvolutionContext<C, T>> for GenerationSnapshot<C, T>
145where
146 C: Chromosome + 'static,
147 T: Clone + Send + Sync + 'static,
148 Generation<C, T>: for<'a> From<&'a EvolutionContext<C, T>>,
149{
150 fn from(ctx: &EvolutionContext<C, T>) -> Self {
151 GenerationSnapshot {
152 generation: Arc::new(Generation::from(ctx)),
153 }
154 }
155}