genalg 0.1.0

A flexible, high-performance genetic algorithm library written in Rust
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
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
//! # BoundedBreedStrategy
//!
//! Similarly to `OrdinaryStrategy`, the `BoundedBreedStrategy` struct represents
//! a breeding strategy where the first parent is considered the winner
//! of the previous generation, and the remaining parents are used to create
//! new individuals through crossover and mutation.
//!
//! However, the `BoundedBreedStrategy` imposes bounds on the phenotypes during evolution.
//! The algorithm develops a phenotype within the specified bounds, ensuring that the resulting
//! phenotype satisfies the constraints set up by the `Magnitude` trait.
use std::{fmt::Debug, marker::PhantomData};

use rayon::prelude::*;
use tracing::{debug, info};

use crate::{
    breeding::BreedStrategy,
    error::{GeneticError, Result},
    evolution::options::EvolutionOptions,
    evolution::options::LogLevel,
    phenotype::Phenotype,
    rng::RandomNumberGenerator,
};

/// Trait for phenotypes that have a measurable magnitude with defined bounds.
///
/// This trait is used by the `BoundedBreedStrategy` to ensure that phenotypes
/// stay within specified bounds during evolution.
pub trait Magnitude<Pheno: Phenotype> {
    /// Returns the current magnitude of the phenotype.
    fn magnitude(&self) -> f64;

    /// Returns the minimum allowed magnitude for the phenotype.
    fn min_magnitude(&self) -> f64;

    /// Returns the maximum allowed magnitude for the phenotype.
    fn max_magnitude(&self) -> f64;

    /// Checks if the phenotype's magnitude is within the allowed bounds.
    fn is_within_bounds(&self) -> bool {
        let mag = self.magnitude();
        mag >= self.min_magnitude() && mag <= self.max_magnitude() && mag.is_finite()
    }
}

/// Configuration for the `BoundedBreedStrategy`.
///
/// This struct holds the configuration parameters for the `BoundedBreedStrategy`.
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[derive(Debug, Clone)]
pub struct BoundedBreedConfig {
    /// The maximum number of attempts to develop a phenotype within bounds.
    pub max_development_attempts: usize,
}

impl Default for BoundedBreedConfig {
    fn default() -> Self {
        Self {
            max_development_attempts: 1000,
        }
    }
}

/// Builder for `BoundedBreedConfig`.
///
/// Provides a fluent interface for constructing `BoundedBreedConfig` instances.
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[derive(Debug, Clone, Default)]
pub struct BoundedBreedConfigBuilder {
    max_development_attempts: Option<usize>,
}

impl BoundedBreedConfigBuilder {
    /// Sets the maximum number of development attempts.
    pub fn max_development_attempts(mut self, value: usize) -> Self {
        self.max_development_attempts = Some(value);
        self
    }

    /// Builds the `BoundedBreedConfig` instance.
    pub fn build(self) -> BoundedBreedConfig {
        BoundedBreedConfig {
            max_development_attempts: self.max_development_attempts.unwrap_or(1000),
        }
    }
}

impl BoundedBreedConfig {
    /// Returns a builder for creating a `BoundedBreedConfig` instance.
    ///
    /// # Example
    ///
    /// ```rust
    /// use  genalg::breeding::{BoundedBreedConfig, BoundedBreedConfigBuilder};
    ///
    /// let config = BoundedBreedConfig::builder()
    ///     .max_development_attempts(2000)
    ///     .build();
    /// ```
    pub fn builder() -> BoundedBreedConfigBuilder {
        BoundedBreedConfigBuilder::default()
    }
}

/// # BoundedBreedStrategy
///
/// Similarly to `OrdinaryStrategy`, the `BoundedBreedStrategy` struct represents
/// a breeding strategy where the first parent is considered the winner
/// of the previous generation, and the remaining parents are used to create
/// new individuals through crossover and mutation.
///
/// However, the `BoundedBreedStrategy` imposes bounds on the phenotypes during evolution.
/// The algorithm develops a phenotype within the specified bounds, ensuring that the resulting
/// phenotype satisfies the constraints set up by the `Magnitude` trait.
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[derive(Debug, Clone)]
pub struct BoundedBreedStrategy<Pheno>
where
    Pheno: Phenotype + Magnitude<Pheno>,
{
    _marker: PhantomData<Pheno>,
    config: BoundedBreedConfig,
}

impl<Pheno> Default for BoundedBreedStrategy<Pheno>
where
    Pheno: Phenotype + Magnitude<Pheno>,
{
    fn default() -> Self {
        Self {
            _marker: PhantomData,
            config: BoundedBreedConfig::default(),
        }
    }
}

impl<Pheno> BoundedBreedStrategy<Pheno>
where
    Pheno: Phenotype + Magnitude<Pheno>,
{
    /// Creates a new `BoundedBreedStrategy` instance with the specified maximum development attempts.
    ///
    /// # Arguments
    ///
    /// * `max_development_attempts` - The maximum number of attempts to develop a phenotype within bounds.
    ///
    /// # Returns
    ///
    /// A new `BoundedBreedStrategy` instance.
    pub fn new(max_development_attempts: usize) -> Self {
        Self {
            _marker: PhantomData,
            config: BoundedBreedConfig {
                max_development_attempts,
            },
        }
    }

    /// Creates a new `BoundedBreedStrategy` instance with the specified configuration.
    ///
    /// # Arguments
    ///
    /// * `config` - The configuration for the strategy.
    ///
    /// # Returns
    ///
    /// A new `BoundedBreedStrategy` instance.
    ///
    /// # Example
    ///
    /// ```rust
    /// use  genalg::breeding::{BoundedBreedStrategy, BoundedBreedConfig, Magnitude};
    /// use genalg::phenotype::Phenotype;
    /// use genalg::rng::RandomNumberGenerator;
    ///
    /// #[derive(Clone, Debug)]
    /// struct MyPhenotype {
    ///     value: f64,
    /// }
    ///
    /// impl Phenotype for MyPhenotype {
    ///     fn crossover(&mut self, other: &Self) {
    ///         self.value = (self.value + other.value) / 2.0;
    ///     }
    ///
    ///     fn mutate(&mut self, rng: &mut RandomNumberGenerator) {
    ///         let values = rng.fetch_uniform(-0.1, 0.1, 1);
    ///         let delta = values.front().unwrap();
    ///         self.value += *delta as f64;
    ///     }
    /// }
    ///
    /// impl Magnitude<MyPhenotype> for MyPhenotype {
    ///     fn magnitude(&self) -> f64 {
    ///         self.value.abs()
    ///     }
    ///
    ///     fn min_magnitude(&self) -> f64 {
    ///         0.0
    ///     }
    ///
    ///     fn max_magnitude(&self) -> f64 {
    ///         100.0
    ///     }
    /// }
    ///
    /// let config = BoundedBreedConfig::builder()
    ///     .max_development_attempts(2000)
    ///     .build();
    ///
    /// let strategy = BoundedBreedStrategy::<MyPhenotype>::with_config(config);
    /// ```
    pub fn with_config(config: BoundedBreedConfig) -> Self {
        Self {
            _marker: PhantomData,
            config,
        }
    }

    /// Creates a new `BoundedBreedStrategy` instance with custom parameters.
    ///
    /// # Note
    ///
    /// This constructor is maintained for backward compatibility.
    /// The parallel threshold should now be set in `EvolutionOptions`.
    ///
    /// # Arguments
    ///
    /// * `max_development_attempts` - The maximum number of attempts to develop a phenotype within bounds.
    /// * `parallel_threshold` - This parameter is ignored. Set the threshold in `EvolutionOptions` instead.
    ///
    /// # Returns
    ///
    /// A new `BoundedBreedStrategy` instance.
    #[deprecated(
        since = "0.1.0",
        note = "Set parallel_threshold in EvolutionOptions instead"
    )]
    pub fn new_with_params(max_development_attempts: usize, _parallel_threshold: usize) -> Self {
        Self::new(max_development_attempts)
    }
}

impl<Pheno> BreedStrategy<Pheno> for BoundedBreedStrategy<Pheno>
where
    Pheno: Phenotype + Magnitude<Pheno> + Send + Sync,
{
    /// Breeds offspring from a set of parent phenotypes, ensuring the offspring
    /// stays within the specified phenotype bounds.
    ///
    /// This method uses a winner-takes-all approach, selecting the first parent
    /// as the winner and evolving it to create offspring.
    ///
    /// # Arguments
    ///
    /// * `parents` - A slice of parent phenotypes.
    /// * `evol_options` - Evolution options controlling the breeding process.
    /// * `rng` - A random number generator for introducing randomness.
    ///
    /// # Returns
    ///
    /// A `Result` containing a vector of offspring phenotypes or a `GeneticError` if breeding fails.
    ///
    /// # Errors
    ///
    /// This method will return an error if:
    /// - The parents slice is empty
    /// - A phenotype cannot be developed within the specified bounds
    ///
    /// # Performance
    ///
    /// This method uses parallel processing for developing phenotypes when the number of
    /// offspring is large enough to benefit from parallelism. Each phenotype is developed
    /// in parallel using Rayon's parallel iterator.
    fn breed(
        &self,
        parents: &[Pheno],
        evol_options: &EvolutionOptions,
        rng: &mut RandomNumberGenerator,
    ) -> Result<Vec<Pheno>> {
        // Check if parents slice is empty
        if parents.is_empty() {
            return Err(GeneticError::EmptyPopulation);
        }

        let winner_previous_generation = parents[0].clone();

        // Prepare the children to be developed
        let mut children_to_develop: Vec<(Pheno, bool)> = Vec::new();

        // Add the winner of the previous generation (no initial mutation)
        children_to_develop.push((winner_previous_generation.clone(), false));

        // Create children through crossover with other parents
        for parent in parents.iter().skip(1) {
            let mut child = winner_previous_generation.clone();
            child.crossover(parent);
            children_to_develop.push((child, true));
        }

        // Create additional children through mutation only
        for _ in parents.len()..evol_options.get_num_offspring() {
            children_to_develop.push((winner_previous_generation.clone(), true));
        }

        // Develop all children (in parallel if there are enough)
        let parallel_threshold = evol_options.get_parallel_threshold();
        let log_level = evol_options.get_log_level();

        if children_to_develop.len() >= parallel_threshold {
            // Parallel development
            children_to_develop
                .into_par_iter()
                .map(|(pheno, initial_mutate)| {
                    self.develop_thread_local(pheno, initial_mutate, log_level)
                })
                .collect()
        } else {
            // Sequential development for small populations
            let mut developed_children = Vec::with_capacity(children_to_develop.len());

            for (pheno, initial_mutate) in children_to_develop {
                developed_children.push(self.develop(pheno, rng, initial_mutate, log_level)?);
            }

            Ok(developed_children)
        }
    }
}

impl<Pheno> BoundedBreedStrategy<Pheno>
where
    Pheno: Phenotype + Magnitude<Pheno>,
{
    /// Develops a phenotype within the specified bounds, ensuring that the resulting
    /// phenotype satisfies the magnitude constraints.
    ///
    /// # Arguments
    ///
    /// * `pheno` - The initial phenotype to be developed.
    /// * `rng` - A random number generator for introducing randomness.
    /// * `initial_mutate` - A flag indicating whether to apply initial mutation.
    /// * `log_level` - The log level for development progress logging.
    ///
    /// # Returns
    ///
    /// A `Result` containing the developed phenotype or a `GeneticError` if development fails.
    ///
    /// # Errors
    ///
    /// This method will return an error if:
    /// - The phenotype cannot be developed within the specified bounds after the maximum number of attempts
    /// - The phenotype's magnitude is not a finite number
    ///
    /// # Details
    ///
    /// This method attempts to develop a phenotype within the specified magnitude bounds.
    /// If `initial_mutate` is true, an initial mutation is applied to the input phenotype.
    /// The development process involves repeated mutation attempts until a phenotype
    /// within the specified bounds is achieved. If after the maximum number of attempts,
    /// a valid phenotype is not obtained, an error is returned.
    fn develop(
        &self,
        pheno: Pheno,
        rng: &mut RandomNumberGenerator,
        initial_mutate: bool,
        log_level: &LogLevel,
    ) -> Result<Pheno> {
        let mut phenotype = pheno;

        // Apply initial mutation if requested
        if initial_mutate {
            phenotype.mutate(rng);
        }

        // Check if the phenotype is already within bounds
        if phenotype.is_within_bounds() {
            return Ok(phenotype);
        }

        // Check if the magnitude is a valid number
        let mag = phenotype.magnitude();
        if !mag.is_finite() {
            return Err(GeneticError::InvalidNumericValue(format!(
                "Phenotype magnitude is not a finite number: {}",
                mag
            )));
        }

        // Try to develop the phenotype within bounds
        for attempt in 1..=self.config.max_development_attempts {
            phenotype.mutate(rng);

            if phenotype.is_within_bounds() {
                return Ok(phenotype);
            }

            // Check for NaN or infinity after mutation
            let mag = phenotype.magnitude();
            if !mag.is_finite() {
                return Err(GeneticError::InvalidNumericValue(format!(
                    "Phenotype magnitude became non-finite during development: {}",
                    mag
                )));
            }

            // If we've tried many times without success, log the progress
            if attempt % (self.config.max_development_attempts / 10) == 0 {
                match log_level {
                    LogLevel::Debug => {
                        debug!(
                            attempt,
                            max_attempts = self.config.max_development_attempts,
                            magnitude = phenotype.magnitude(),
                            min_bound = phenotype.min_magnitude(),
                            max_bound = phenotype.max_magnitude(),
                            "Development attempt progress"
                        );
                    }
                    LogLevel::Info => {
                        if attempt == self.config.max_development_attempts / 2 {
                            info!(
                                progress = "50%",
                                magnitude = phenotype.magnitude(),
                                min_bound = phenotype.min_magnitude(),
                                max_bound = phenotype.max_magnitude(),
                                "Development halfway complete"
                            );
                        }
                    }
                    LogLevel::None => {}
                }
            }
        }

        // If we've exhausted all attempts, return an error
        Err(GeneticError::MaxAttemptsReached(format!(
            "Failed to develop phenotype within bounds after {} attempts. Current magnitude: {}, min: {}, max: {}",
            self.config.max_development_attempts,
            phenotype.magnitude(),
            phenotype.min_magnitude(),
            phenotype.max_magnitude()
        )))
    }

    /// Develops a phenotype within the specified bounds, ensuring that the resulting
    /// phenotype satisfies the magnitude constraints.
    ///
    /// # Arguments
    ///
    /// * `pheno` - The initial phenotype to be developed.
    /// * `initial_mutate` - A flag indicating whether to apply initial mutation.
    /// * `log_level` - The log level for development progress logging.
    ///
    /// # Returns
    ///
    /// A `Result` containing the developed phenotype or a `GeneticError` if development fails.
    ///
    /// # Errors
    ///
    /// This method will return an error if:
    /// - The phenotype cannot be developed within the specified bounds after the maximum number of attempts
    /// - The phenotype's magnitude is not a finite number
    ///
    /// # Details
    ///
    /// This method attempts to develop a phenotype within the specified magnitude bounds.
    /// If `initial_mutate` is true, an initial mutation is applied to the input phenotype.
    /// The development process involves repeated mutation attempts until a phenotype
    /// within the specified bounds is achieved. If after the maximum number of attempts,
    /// a valid phenotype is not obtained, an error is returned.
    fn develop_thread_local(
        &self,
        pheno: Pheno,
        initial_mutate: bool,
        log_level: &LogLevel,
    ) -> Result<Pheno> {
        let mut phenotype = pheno;

        // Apply initial mutation if requested
        if initial_mutate {
            phenotype.mutate_thread_local();
        }

        // Check if the phenotype is already within bounds
        if phenotype.is_within_bounds() {
            return Ok(phenotype);
        }

        // Check if the magnitude is a valid number
        let mag = phenotype.magnitude();
        if !mag.is_finite() {
            return Err(GeneticError::InvalidNumericValue(format!(
                "Phenotype magnitude is not a finite number: {}",
                mag
            )));
        }

        // Try to develop the phenotype within bounds
        for attempt in 1..=self.config.max_development_attempts {
            phenotype.mutate_thread_local();

            if phenotype.is_within_bounds() {
                return Ok(phenotype);
            }

            // Check for NaN or infinity after mutation
            let mag = phenotype.magnitude();
            if !mag.is_finite() {
                return Err(GeneticError::InvalidNumericValue(format!(
                    "Phenotype magnitude became non-finite during development: {}",
                    mag
                )));
            }

            // If we've tried many times without success, log the progress
            if attempt % (self.config.max_development_attempts / 10) == 0 {
                match log_level {
                    LogLevel::Debug => {
                        debug!(
                            attempt,
                            max_attempts = self.config.max_development_attempts,
                            magnitude = phenotype.magnitude(),
                            min_bound = phenotype.min_magnitude(),
                            max_bound = phenotype.max_magnitude(),
                            "Development attempt progress"
                        );
                    }
                    LogLevel::Info => {
                        if attempt == self.config.max_development_attempts / 2 {
                            info!(
                                progress = "50%",
                                magnitude = phenotype.magnitude(),
                                min_bound = phenotype.min_magnitude(),
                                max_bound = phenotype.max_magnitude(),
                                "Development halfway complete"
                            );
                        }
                    }
                    LogLevel::None => {}
                }
            }
        }

        // If we've exhausted all attempts, return an error
        Err(GeneticError::MaxAttemptsReached(format!(
            "Failed to develop phenotype within bounds after {} attempts. Current magnitude: {}, min: {}, max: {}",
            self.config.max_development_attempts,
            phenotype.magnitude(),
            phenotype.min_magnitude(),
            phenotype.max_magnitude()
        )))
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::{
        evolution::options::{EvolutionOptions, LogLevel},
        phenotype::Phenotype,
        rng::RandomNumberGenerator,
    };

    #[derive(Clone, Debug)]
    struct TestPhenotype {
        value: f64,
        min_bound: f64,
        max_bound: f64,
    }

    impl TestPhenotype {
        fn new(value: f64, min_bound: f64, max_bound: f64) -> Self {
            Self {
                value,
                min_bound,
                max_bound,
            }
        }
    }

    impl Phenotype for TestPhenotype {
        fn crossover(&mut self, other: &Self) {
            self.value = (self.value + other.value) / 2.0;
        }

        fn mutate(&mut self, rng: &mut RandomNumberGenerator) {
            let delta = *rng.fetch_uniform(-0.1, 0.1, 1).front().unwrap() as f64;
            self.value += delta;
        }
    }

    impl Magnitude<TestPhenotype> for TestPhenotype {
        fn magnitude(&self) -> f64 {
            self.value.abs()
        }

        fn min_magnitude(&self) -> f64 {
            self.min_bound
        }

        fn max_magnitude(&self) -> f64 {
            self.max_bound
        }
    }

    #[test]
    fn test_develop_within_bounds() {
        let mut rng = RandomNumberGenerator::new();
        let strategy = BoundedBreedStrategy::default();
        let pheno = TestPhenotype::new(5.0, 4.0, 6.0);

        let result = strategy.develop(pheno, &mut rng, false, &LogLevel::None);
        assert!(result.is_ok());

        let developed = result.unwrap();
        assert!(developed.magnitude() >= developed.min_magnitude());
        assert!(developed.magnitude() <= developed.max_magnitude());
    }

    #[test]
    fn test_develop_outside_bounds() {
        let mut rng = RandomNumberGenerator::new();
        // Use a small number of attempts to make the test faster
        let strategy = BoundedBreedStrategy::new(10);
        // Create a phenotype that's very unlikely to mutate into the valid range
        let pheno = TestPhenotype::new(100.0, 0.0, 0.1);

        let result = strategy.develop(pheno, &mut rng, false, &LogLevel::None);
        assert!(result.is_err());

        match result {
            Err(GeneticError::MaxAttemptsReached(_)) => (),
            _ => panic!("Expected MaxAttemptsReached error"),
        }
    }

    #[test]
    fn test_breed_empty_parents() {
        let mut rng = RandomNumberGenerator::new();
        let evol_options = EvolutionOptions::default();
        let strategy = BoundedBreedStrategy::<TestPhenotype>::default();

        let parents = Vec::<TestPhenotype>::new();

        let result = strategy.breed(&parents, &evol_options, &mut rng);
        assert!(result.is_err());

        match result {
            Err(GeneticError::EmptyPopulation) => (),
            _ => panic!("Expected EmptyPopulation error"),
        }
    }
}