1use crate::Chromosome;
2use radiate_core::engine::Context;
3use radiate_core::objectives::Scored;
4use radiate_core::{Ecosystem, Epoch, Front, MetricSet, Phenotype, Score};
5use std::fmt::Debug;
6
7pub struct Generation<C, T>
8where
9 C: Chromosome,
10{
11 ecosystem: Ecosystem<C>,
12 best: T,
13 index: usize,
14 metrics: MetricSet,
15 score: Score,
16}
17
18impl<C: Chromosome, T> Generation<C, T> {
19 pub fn score(&self) -> &Score {
20 &self.score
21 }
22}
23
24impl<C: Chromosome, T> Epoch for Generation<C, T> {
25 type Chromosome = C;
26 type Value = T;
27
28 fn ecosystem(&self) -> &Ecosystem<C> {
29 &self.ecosystem
30 }
31
32 fn value(&self) -> &Self::Value {
33 &self.best
34 }
35
36 fn index(&self) -> usize {
37 self.index
38 }
39
40 fn metrics(&self) -> &MetricSet {
41 &self.metrics
42 }
43}
44
45impl<C: Chromosome, T> Scored for Generation<C, T> {
46 fn score(&self) -> Option<&Score> {
47 Some(&self.score)
48 }
49}
50
51impl<C: Chromosome, T: Clone> From<&Context<C, T>> for Generation<C, T> {
52 fn from(context: &Context<C, T>) -> Self {
53 Generation {
54 ecosystem: context.ecosystem.clone(),
55 best: context.best.clone(),
56 index: context.index,
57 metrics: context.metrics.clone(),
58 score: context.score.clone().unwrap(),
59 }
60 }
61}
62
63impl<C, T: Debug> Debug for Generation<C, T>
64where
65 C: Chromosome,
66{
67 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
68 write!(f, "EngineOutput {{\n")?;
69 write!(f, " best: {:?},\n", self.best)?;
70 write!(f, " score: {:?},\n", self.score)?;
71 write!(f, " index: {:?},\n", self.index)?;
72 write!(f, " size: {:?},\n", self.ecosystem.population.len())?;
73 write!(f, " duration: {:?},\n", self.time())?;
74
75 if let Some(species) = &self.ecosystem.species {
76 for s in species {
77 write!(f, " species: {:?},\n", s)?;
78 }
79 }
80
81 write!(f, " metrics: {:?},\n", self.metrics)?;
82 write!(f, "}}")
83 }
84}
85
86pub struct MultiObjectiveGeneration<C>
87where
88 C: Chromosome,
89{
90 ecosystem: Ecosystem<C>,
91 front: Front<Phenotype<C>>,
92 index: usize,
93 metrics: MetricSet,
94}
95
96impl<C: Chromosome> Epoch for MultiObjectiveGeneration<C>
97where
98 C: Chromosome,
99{
100 type Chromosome = C;
101 type Value = Front<Phenotype<C>>;
102
103 fn ecosystem(&self) -> &Ecosystem<C> {
104 &self.ecosystem
105 }
106
107 fn value(&self) -> &Self::Value {
108 &self.front
109 }
110
111 fn index(&self) -> usize {
112 self.index
113 }
114
115 fn metrics(&self) -> &MetricSet {
116 &self.metrics
117 }
118}
119
120impl<C: Chromosome, T: Clone> From<&Context<C, T>> for MultiObjectiveGeneration<C> {
121 fn from(context: &Context<C, T>) -> Self {
122 MultiObjectiveGeneration {
123 ecosystem: context.ecosystem.clone(),
124 front: context.front.read().unwrap().clone(),
125 index: context.index,
126 metrics: context.metrics.clone(),
127 }
128 }
129}