radiate_core/fitness/
mod.rs

1mod composite;
2mod novelty;
3
4pub use composite::CompositeFitnessFn;
5pub use novelty::{Novelty, NoveltySearch};
6
7use crate::Score;
8
9pub trait FitnessFunction<T, S = f32>: Send + Sync
10where
11    S: Into<Score>,
12{
13    fn evaluate(&self, individual: T) -> S;
14}
15
16/// Fitness function for evaluating a batch of individuals
17/// Its important to note that the indices of the individuals in the input slice
18/// must match the indices of the corresponding fitness values in the output vector.
19pub trait BatchFitnessFunction<T, S = f32>: Send + Sync
20where
21    S: Into<Score>,
22{
23    fn evaluate(&self, individuals: &[T]) -> Vec<S>;
24}
25
26/// Blanket implement FitnessFunction for any function that takes a single argument.
27/// This covers the base case for any function supplied to an engine that takes a decoded phenotype.
28impl<T, S, F> FitnessFunction<T, S> for F
29where
30    F: Fn(T) -> S + Send + Sync,
31    S: Into<Score>,
32{
33    fn evaluate(&self, individual: T) -> S {
34        self(individual)
35    }
36}
37
38/// Blanket implement BatchFitnessFunction for any function that takes a slice of arguments.
39/// This covers the base case for any function supplied to an engine that takes a batch of decoded phenotypes.
40impl<T, S, F> BatchFitnessFunction<T, S> for F
41where
42    F: for<'a> Fn(&'a [T]) -> Vec<S> + Send + Sync,
43    S: Into<Score>,
44{
45    fn evaluate(&self, individuals: &[T]) -> Vec<S> {
46        self(individuals)
47    }
48}