radiate_engines/builder/
problem.rs1use crate::GeneticEngineBuilder;
2use radiate_core::{
3 Chromosome, Codec, Genotype, Problem, Score,
4 fitness::{BatchFitnessFunction, FitnessFunction},
5};
6use std::sync::Arc;
7
8type FitnessFn<T> = dyn Fn(T) -> Score + Send + Sync;
9type BatchFitnessFn<T> = dyn Fn(Vec<T>) -> Vec<Score> + Send + Sync;
10type RawFitnessFn<C> = dyn Fn(&Genotype<C>) -> Score + Send + Sync;
11type RawBatchFitnessFn<C> = dyn Fn(Vec<&Genotype<C>>) -> Vec<Score> + Send + Sync;
12
13#[derive(Clone)]
14pub struct ProblemParams<C, T>
15where
16 C: Chromosome,
17 T: Clone,
18{
19 pub codec: Option<Arc<dyn Codec<C, T>>>,
20 pub problem: Option<Arc<dyn Problem<C, T>>>,
21 pub fitness_fn: Option<Arc<FitnessFn<T>>>,
22 pub batch_fitness_fn: Option<Arc<BatchFitnessFn<T>>>,
23 pub raw_fitness_fn: Option<Arc<RawFitnessFn<C>>>,
24 pub raw_batch_fitness_fn: Option<Arc<RawBatchFitnessFn<C>>>,
25}
26
27impl<C, T> GeneticEngineBuilder<C, T>
28where
29 C: Chromosome + PartialEq + Clone,
30 T: Clone + Send,
31{
32 pub fn codec<D: Codec<C, T> + 'static>(mut self, codec: D) -> Self {
34 self.params.problem_params.codec = Some(Arc::new(codec));
35 self
36 }
37
38 pub fn problem<P: Problem<C, T> + 'static>(mut self, problem: P) -> Self {
40 self.params.problem_params.problem = Some(Arc::new(problem));
41 self
42 }
43
44 pub fn fitness_fn<S: Into<Score>>(
51 mut self,
52 fitness_func: impl FitnessFunction<T, S> + 'static,
53 ) -> Self {
54 let other = move |x| fitness_func.evaluate(x).into();
55 self.params.problem_params.fitness_fn = Some(Arc::new(other));
56 self
57 }
58
59 pub fn batch_fitness_fn<S: Into<Score>>(
66 mut self,
67 batch_fitness_func: impl BatchFitnessFunction<T, S> + 'static,
68 ) -> Self {
69 let other = move |x: Vec<T>| {
70 batch_fitness_func
71 .evaluate(x)
72 .into_iter()
73 .map(|s| s.into())
74 .collect()
75 };
76 self.params.problem_params.batch_fitness_fn = Some(Arc::new(other));
77 self
78 }
79
80 pub fn raw_fitness_fn<S: Into<Score>>(
81 mut self,
82 raw_fitness_func: impl for<'a> FitnessFunction<&'a Genotype<C>, S> + 'static,
83 ) -> Self {
84 let other = move |x: &Genotype<C>| raw_fitness_func.evaluate(x).into();
85 self.params.problem_params.raw_fitness_fn = Some(Arc::new(other));
86 self
87 }
88
89 pub fn raw_batch_fitness_fn<S: Into<Score>>(
90 mut self,
91 raw_batch_fitness_func: impl for<'a> BatchFitnessFunction<&'a Genotype<C>, S> + 'static,
92 ) -> Self {
93 let other = move |x: Vec<&Genotype<C>>| {
94 raw_batch_fitness_func
95 .evaluate(x)
96 .into_iter()
97 .map(|s| s.into())
98 .collect()
99 };
100 self.params.problem_params.raw_batch_fitness_fn = Some(Arc::new(other));
101 self
102 }
103}