genetic_algorithm_traits/individual.rs
1/// A single instance in the genetic algorithm.
2/// In a TSP for example, this would be and individual route.
3pub trait Individual<'a>: Clone {
4 /// The Type of cost data this individual is compatible to compute its
5 /// fitness on.
6 type IndividualCost: 'a;
7 /// Randomly change the object and therefore it's fitness
8 /// This is a key step of the genetic algorithm.
9 ///
10 /// # Arguments
11 ///
12 /// * `prob` - The probability with which the individual will mutate.
13 fn mutate(self, prob: f32) -> Self;
14 /// The `crossover` takes two individual and combines their characteristics.
15 /// The implementation depends on the problem to be solve with genetic algorithm but
16 /// both from a performance and runtime perspective, this is one of the most important
17 /// and time-consuming methods.
18 ///
19 /// # Arguments
20 ///
21 /// * `other` - The other individual that should be `crossover`ed with.
22 ///
23 fn crossover(&self, other: &Self) -> Self;
24 /// How `fit` is your individual, e.g. how well does it solve the problem you are
25 /// trying to solve with genetic algorithms. This is the metric that is maximised, e.g.
26 /// overall individuals with a very high fitness should be found.
27 ///
28 /// # Arguments
29 ///
30 /// * `cost_data` - The data that might be needed to compute your fitness. If you use
31 /// genetic algorithm to solve a traveling salesman problem, the `cost_data` will typically
32 /// contain your distance matrix.
33 ///
34 fn fitness(&self, cost_data: &Self::IndividualCost) -> f64;
35}