radiate_engines/builder/
objectives.rs

1use crate::GeneticEngineBuilder;
2use radiate_core::{Chromosome, Front, Objective, Optimize, Phenotype};
3use std::ops::Range;
4
5#[derive(Clone)]
6pub struct OptimizeParams<C: Chromosome> {
7    pub objectives: Objective,
8    pub front_range: Range<usize>,
9    pub front: Option<Front<Phenotype<C>>>,
10}
11
12impl<C, T> GeneticEngineBuilder<C, T>
13where
14    C: Chromosome + PartialEq + Clone,
15    T: Clone + Send,
16{
17    /// Set the optimization goal of the genetic engine to minimize the fitness function.
18    pub fn minimizing(mut self) -> Self {
19        self.params.optimization_params.objectives = Objective::Single(Optimize::Minimize);
20        self
21    }
22
23    /// Set the optimization goal of the genetic engine to maximize the fitness function.
24    pub fn maximizing(mut self) -> Self {
25        self.params.optimization_params.objectives = Objective::Single(Optimize::Maximize);
26        self
27    }
28
29    pub fn multi_objective(
30        mut self,
31        objectives: impl Into<Vec<Optimize>>,
32    ) -> GeneticEngineBuilder<C, T> {
33        self.params.optimization_params.objectives = Objective::Multi(objectives.into());
34        self
35    }
36
37    /// Set the minimum and maximum size of the pareto front. This is used for
38    /// multi-objective optimization problems where the goal is to find the best
39    /// solutions that are not dominated by any other solution.
40    pub fn front_size(mut self, range: Range<usize>) -> GeneticEngineBuilder<C, T> {
41        self.add_error_if(
42            || range.start > range.end,
43            "Front range start must be less than end",
44        );
45
46        self.params.optimization_params.front_range = range;
47        self
48    }
49}