genetic_algorithms 3.0.0

Library for solving genetic algorithm problems
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
//! Population — chromosome container for evolving generations.
//!
//! A [`Population`] holds the current generation of chromosomes, tracks the
//! best-so-far individual, and computes aggregate fitness statistics used by
//! adaptive GA operators.
//!
//! The struct supports standard Rust collection traits (`IntoIterator`,
//! `FromIterator`, `Index`, `IndexMut`), and implements `Clone`, `Debug`,
//! and optional `serde` serialization behind the `serde` feature flag.
//!
//! # Key items
//!
//! | Item | Description |
//! |------|-------------|
//! | [`Population<U>`] | Main container, generic over chromosome type |
//! | [`Population::best_chromosome`] | The best-so-far individual |
//! | `Population::fitness_stats` | Aggregate fitness statistics |
//!
//! # When to use
//! Used by all engines as the primary solution container. It is the return
//! type of `ga.run()` and provides access to the evolved solutions and their
//! fitness statistics.

use crate::configuration::ProblemSolving;
use crate::traits::ChromosomeT;
#[cfg(all(not(target_arch = "wasm32"), feature = "parallel"))]
use rayon::prelude::*;
use std::fmt;
use std::ops::{Index, IndexMut};

/// Population of chromosomes with aggregate statistics and best tracking.
///
/// Responsibilities:
/// - Store the evolving set of chromosomes.
/// - Maintain best chromosome according to the configured problem objective.
/// - Compute average (f_avg) and maximum (f_max) fitness (used by adaptive GA).
/// - Parallel fitness calculation to leverage multiple threads.
///
/// # Examples
///
/// ```rust,no_run
/// use genetic_algorithms::population::Population;
/// use genetic_algorithms::chromosomes::Binary;
///
/// // Create an empty population, then populate it.
/// let chromosomes: Vec<Binary> = Vec::new();
/// let pop = Population::new(chromosomes);
/// assert_eq!(pop.chromosomes.len(), 0);
///
/// // An empty population for use before the first generation:
/// let empty: Population<Binary> = Population::new_empty();
/// assert_eq!(empty.chromosomes.len(), 0);
/// ```
pub struct Population<U>
where
    U: ChromosomeT,
{
    /// The chromosomes (members) of the population.
    pub chromosomes: Vec<U>,

    /// Best chromosome in the population (by fitness, according to `ProblemSolving`).
    pub best_chromosome: U,
    pub(crate) best_chromosome_is_set: bool,

    /// Average fitness across the population.
    pub f_avg: f64,

    /// Largest fitness value in the population.
    pub f_max: f64,
}

impl<U> Population<U>
where
    U: ChromosomeT,
{
    /// Creates a new empty `Population`.
    pub fn new_empty() -> Population<U> {
        Population {
            chromosomes: vec![],
            f_avg: 0.0,
            f_max: 0.0,
            best_chromosome: U::new(),
            best_chromosome_is_set: false,
        }
    }

    /// Creates a `Population` with the given chromosomes as members.
    pub fn new(chromosomes: Vec<U>) -> Population<U> {
        Population {
            chromosomes,
            f_avg: 0.0,
            f_max: 0.0,
            best_chromosome: U::new(),
            best_chromosome_is_set: false,
        }
    }

    /// Recalculate `f_avg` and `f_max` (used by adaptive GA probabilities).
    pub fn recalculate_aga(&mut self) {
        if self.chromosomes.is_empty() {
            self.f_avg = 0.0;
            self.f_max = 0.0;
            return;
        }
        self.f_max = f64::NEG_INFINITY;
        self.f_avg = 0.0;
        for chromosome in self.chromosomes.as_slice() {
            self.f_max = if chromosome.fitness() > self.f_max {
                chromosome.fitness()
            } else {
                self.f_max
            };
            self.f_avg += chromosome.fitness();
        }
        self.f_avg /= self.chromosomes.len() as f64;
    }

    /// Appends a list of chromosomes into the current population.
    pub fn add_chromosomes(&mut self, chromosomes: &mut Vec<U>) {
        self.chromosomes.append(chromosomes);
    }

    /// Returns the number of chromosomes in the population.
    pub fn size(&self) -> usize {
        self.chromosomes.len()
    }

    /// Computes fitness for unevaluated chromosomes in parallel and updates the best chromosome.
    ///
    /// Uses rayon's parallel iterators for efficient work distribution.
    /// A chromosome is considered unevaluated if its fitness is `NaN`.
    pub fn fitness_calculation(
        &mut self,
        _number_of_threads: usize,
        problem_solving: ProblemSolving,
    ) {
        crate::log_debug!(target="population_events", method="fitness_calculation"; "Started the population fitness calculation");

        // Calculate fitness in parallel for chromosomes that have not yet been evaluated.
        // NaN fitness indicates a chromosome whose fitness has never been computed.
        #[cfg(all(not(target_arch = "wasm32"), feature = "parallel"))]
        self.chromosomes.par_iter_mut().for_each(|chromosome| {
            if chromosome.fitness().is_nan() {
                chromosome.calculate_fitness();
            }
        });
        #[cfg(any(target_arch = "wasm32", not(feature = "parallel")))]
        self.chromosomes.iter_mut().for_each(|chromosome| {
            if chromosome.fitness().is_nan() {
                chromosome.calculate_fitness();
            }
        });

        // Update best chromosome: scan for the best fitness among all
        // chromosomes and clone only the winner (instead of cloning every
        // chromosome in the loop).
        if let Some(best_idx) = find_best_index(&self.chromosomes, problem_solving) {
            if !self.best_chromosome_is_set {
                self.best_chromosome = self.chromosomes[best_idx].clone();
                self.best_chromosome_is_set = true;
            } else {
                let candidate_fitness = self.chromosomes[best_idx].fitness();
                let current_fitness = self.best_chromosome.fitness();
                let candidate_is_better = match problem_solving {
                    ProblemSolving::Maximization | ProblemSolving::FixedFitness => {
                        candidate_fitness > current_fitness
                    }
                    ProblemSolving::Minimization => candidate_fitness < current_fitness,
                };
                if candidate_is_better {
                    self.best_chromosome = self.chromosomes[best_idx].clone();
                }
            }
        }

        crate::log_debug!(target="ga_events", method="population_fitness_calculation"; "Population fitness calculation finished");
    }

    /// Update the best chromosome given a candidate, according to the problem objective.
    pub fn decide_best_chromosome(&mut self, new_chromosome: &U, problem_solving: ProblemSolving) {
        crate::log_debug!(target="population_events", method="decide_best_chromosome"; "Started the best chromosome method");

        if !self.best_chromosome_is_set {
            self.best_chromosome = new_chromosome.clone();
            self.best_chromosome_is_set = true;
        } else {
            crate::log_trace!(target="population_events", method="decide_best_chromosome"; "Best chromosome fitness: {} - New chromosome fitness: {}",
                self.best_chromosome.fitness(), new_chromosome.fitness());

            let is_self_better = match problem_solving {
                ProblemSolving::Maximization => {
                    self.best_chromosome.fitness() >= new_chromosome.fitness()
                }
                ProblemSolving::Minimization => {
                    self.best_chromosome.fitness() < new_chromosome.fitness()
                }
                _ => self.best_chromosome.fitness() >= new_chromosome.fitness(),
            };

            if !is_self_better {
                self.best_chromosome = new_chromosome.clone();
            };
        }

        crate::log_debug!(target="chromosome_events", method="get_best_chromosome"; "Best chromosome method finished");
    }
}

/// Finds the index of the best chromosome in a slice according to `problem_solving`.
///
/// Returns `None` for an empty slice.
fn find_best_index<U: ChromosomeT>(
    chromosomes: &[U],
    problem_solving: ProblemSolving,
) -> Option<usize> {
    if chromosomes.is_empty() {
        return None;
    }
    let mut best_idx = 0;
    let mut best_fit = chromosomes[0].fitness();
    for (i, c) in chromosomes.iter().enumerate().skip(1) {
        let fit = c.fitness();
        let is_better = match problem_solving {
            ProblemSolving::Maximization | ProblemSolving::FixedFitness => fit > best_fit,
            ProblemSolving::Minimization => fit < best_fit,
        };
        if is_better {
            best_idx = i;
            best_fit = fit;
        }
    }
    Some(best_idx)
}

// ---------------------------------------------------------------------------
// Standard trait implementations for Population<U>
// ---------------------------------------------------------------------------

impl<U: ChromosomeT + Clone> Clone for Population<U> {
    fn clone(&self) -> Self {
        Population {
            chromosomes: self.chromosomes.clone(),
            best_chromosome: self.best_chromosome.clone(),
            best_chromosome_is_set: self.best_chromosome_is_set,
            f_avg: self.f_avg,
            f_max: self.f_max,
        }
    }
}

impl<U: ChromosomeT + fmt::Debug> fmt::Debug for Population<U> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("Population")
            .field("size", &self.chromosomes.len())
            .field("f_avg", &self.f_avg)
            .field("f_max", &self.f_max)
            .field("best_chromosome_is_set", &self.best_chromosome_is_set)
            .finish()
    }
}

impl<U: ChromosomeT> Default for Population<U> {
    fn default() -> Self {
        Population::new_empty()
    }
}

impl<U: ChromosomeT> IntoIterator for Population<U> {
    type Item = U;
    type IntoIter = std::vec::IntoIter<U>;

    fn into_iter(self) -> Self::IntoIter {
        self.chromosomes.into_iter()
    }
}

impl<'a, U: ChromosomeT> IntoIterator for &'a Population<U> {
    type Item = &'a U;
    type IntoIter = std::slice::Iter<'a, U>;

    fn into_iter(self) -> Self::IntoIter {
        self.chromosomes.iter()
    }
}

impl<'a, U: ChromosomeT> IntoIterator for &'a mut Population<U> {
    type Item = &'a mut U;
    type IntoIter = std::slice::IterMut<'a, U>;

    fn into_iter(self) -> Self::IntoIter {
        self.chromosomes.iter_mut()
    }
}

impl<U: ChromosomeT> FromIterator<U> for Population<U> {
    fn from_iter<I: IntoIterator<Item = U>>(iter: I) -> Self {
        let chromosomes: Vec<U> = iter.into_iter().collect();
        Population::new(chromosomes)
    }
}

impl<U: ChromosomeT> Index<usize> for Population<U> {
    type Output = U;

    fn index(&self, index: usize) -> &Self::Output {
        &self.chromosomes[index]
    }
}

impl<U: ChromosomeT> IndexMut<usize> for Population<U> {
    fn index_mut(&mut self, index: usize) -> &mut Self::Output {
        &mut self.chromosomes[index]
    }
}

// ---------------------------------------------------------------------------
// Serde support (behind the "serde" feature flag)
// ---------------------------------------------------------------------------

#[cfg(feature = "serde")]
mod serde_impl {
    use super::*;
    use serde::de::{self, MapAccess, Visitor};
    use serde::ser::SerializeStruct;
    use serde::{Deserialize, Deserializer, Serialize, Serializer};
    use std::marker::PhantomData;

    impl<U> Serialize for Population<U>
    where
        U: ChromosomeT + Serialize,
    {
        fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
            let mut state = serializer.serialize_struct("Population", 5)?;
            state.serialize_field("chromosomes", &self.chromosomes)?;
            state.serialize_field("best_chromosome", &self.best_chromosome)?;
            state.serialize_field("best_chromosome_is_set", &self.best_chromosome_is_set)?;
            state.serialize_field("f_avg", &self.f_avg)?;
            state.serialize_field("f_max", &self.f_max)?;
            state.end()
        }
    }

    impl<'de, U> Deserialize<'de> for Population<U>
    where
        U: ChromosomeT + Deserialize<'de>,
    {
        fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
            #[derive(serde::Deserialize)]
            #[serde(field_identifier, rename_all = "snake_case")]
            enum Field {
                Chromosomes,
                BestChromosome,
                BestChromosomeIsSet,
                FAvg,
                FMax,
            }

            struct PopulationVisitor<U>(PhantomData<U>);

            impl<'de, U> Visitor<'de> for PopulationVisitor<U>
            where
                U: ChromosomeT + Deserialize<'de>,
            {
                type Value = Population<U>;

                fn expecting(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
                    f.write_str("struct Population")
                }

                fn visit_map<A: MapAccess<'de>>(self, mut map: A) -> Result<Self::Value, A::Error> {
                    let mut chromosomes: Option<Vec<U>> = None;
                    let mut best_chromosome: Option<U> = None;
                    let mut best_chromosome_is_set: Option<bool> = None;
                    let mut f_avg: Option<f64> = None;
                    let mut f_max: Option<f64> = None;

                    while let Some(key) = map.next_key()? {
                        match key {
                            Field::Chromosomes => {
                                chromosomes = Some(map.next_value()?);
                            }
                            Field::BestChromosome => {
                                best_chromosome = Some(map.next_value()?);
                            }
                            Field::BestChromosomeIsSet => {
                                best_chromosome_is_set = Some(map.next_value()?);
                            }
                            Field::FAvg => {
                                f_avg = Some(map.next_value()?);
                            }
                            Field::FMax => {
                                f_max = Some(map.next_value()?);
                            }
                        }
                    }

                    Ok(Population {
                        chromosomes: chromosomes
                            .ok_or_else(|| de::Error::missing_field("chromosomes"))?,
                        best_chromosome: best_chromosome
                            .ok_or_else(|| de::Error::missing_field("best_chromosome"))?,
                        best_chromosome_is_set: best_chromosome_is_set
                            .ok_or_else(|| de::Error::missing_field("best_chromosome_is_set"))?,
                        f_avg: f_avg.ok_or_else(|| de::Error::missing_field("f_avg"))?,
                        f_max: f_max.ok_or_else(|| de::Error::missing_field("f_max"))?,
                    })
                }
            }

            const FIELDS: &[&str] = &[
                "chromosomes",
                "best_chromosome",
                "best_chromosome_is_set",
                "f_avg",
                "f_max",
            ];
            deserializer.deserialize_struct("Population", FIELDS, PopulationVisitor(PhantomData))
        }
    }
}