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
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
use rayon::iter::{IntoParallelRefIterator, ParallelIterator};
use tracing::{debug, info};

use super::{
    builder::EvolutionLauncherBuilder,
    challenge::Challenge,
    options::{EvolutionOptions, LogLevel},
};
use crate::{
    breeding::BreedStrategy,
    error::{GeneticError, Result},
    local_search::{LocalSearchApplicationStrategy, LocalSearchManager},
    phenotype::Phenotype,
    rng::RandomNumberGenerator,
    selection::SelectionStrategy,
    LocalSearch, OptionExt,
};

/// Represents the result of an evolution, containing a phenotype and its associated score.
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[derive(Debug, Clone, PartialEq, PartialOrd)]
pub struct EvolutionResult<Pheno: Phenotype> {
    /// The evolved phenotype.
    pub pheno: Pheno,
    /// The fitness score of the phenotype.
    pub score: f64,
}

/// Manages the evolution process using a specified breeding strategy, selection strategy, and challenge.
///
/// The `EvolutionLauncher` is the central component of the genetic algorithm framework.
/// It coordinates the entire evolutionary process, including:
/// - Population initialization
/// - Fitness evaluation
/// - Parent selection
/// - Breeding new individuals
/// - Local search (optional)
/// - Tracking the best solution
///
/// # Type Parameters
///
/// * `P` - The phenotype type that represents individuals in the population
/// * `B` - The breeding strategy type
/// * `S` - The selection strategy type
/// * `LS` - The local search algorithm type
/// * `F` - The challenge (fitness function) type
/// * `A` - The local search application strategy type
#[derive(Debug, Clone, PartialEq, PartialOrd)]
pub struct EvolutionLauncher<P, B, S, LS, F, A>
where
    P: Phenotype,
    B: BreedStrategy<P>,
    S: SelectionStrategy<P>,
    LS: LocalSearch<P, F> + Clone,
    F: Challenge<P> + Clone,
    A: LocalSearchApplicationStrategy<P> + Clone,
{
    breed_strategy: B,
    selection_strategy: S,
    local_search_manager: Option<LocalSearchManager<P, LS, A, F>>,
    challenge: F,
}

impl<P, B, S, LS, F, A> EvolutionLauncher<P, B, S, LS, F, A>
where
    P: Phenotype + Send + Sync,
    LS: LocalSearch<P, F> + Clone + Send + Sync,
    F: Challenge<P> + Clone + Send + Sync,
    A: LocalSearchApplicationStrategy<P> + Clone + Send + Sync,
    B: BreedStrategy<P> + Clone + Send + Sync,
    S: SelectionStrategy<P> + Clone + Send + Sync,
{
    /// Creates a new `EvolutionLauncher` instance with the specified breeding strategy, selection strategy, local search manager, and challenge.
    ///
    /// # Arguments
    ///
    /// * `breed_strategy` - The breeding strategy used for generating offspring during evolution.
    /// * `selection_strategy` - The selection strategy used for selecting parents for the next generation.
    /// * `local_search_manager` - An optional local search manager for refining solutions. Use `None` if local search is not needed.
    /// * `challenge` - The challenge used to evaluate the fitness of phenotypes.
    ///
    /// # Returns
    ///
    /// A new `EvolutionLauncher` instance.
    ///
    /// # Example
    ///
    /// ```rust
    /// # use genalg::{
    /// #     evolution::{Challenge, EvolutionLauncher, EvolutionOptions},
    /// #     phenotype::Phenotype,
    /// #     rng::RandomNumberGenerator,
    /// #     breeding::OrdinaryStrategy,
    /// #     selection::ElitistSelection,
    /// #     local_search::{HillClimbing, AllIndividualsStrategy},
    /// # };
    /// #
    /// # #[derive(Clone, Debug)]
    /// # struct MyPhenotype { value: f64 }
    /// # impl Phenotype for MyPhenotype {
    /// #     fn crossover(&mut self, other: &Self) {}
    /// #     fn mutate(&mut self, _rng: &mut RandomNumberGenerator) {}
    /// # }
    /// #
    /// # #[derive(Clone)]
    /// # struct MyChallenge;
    /// # impl Challenge<MyPhenotype> for MyChallenge {
    /// #     fn score(&self, _phenotype: &MyPhenotype) -> f64 { 0.0 }
    /// # }
    ///
    /// let breed_strategy = OrdinaryStrategy::default();
    /// let selection_strategy = ElitistSelection::default();
    /// let challenge = MyChallenge;
    ///
    /// // Create a launcher without local search
    /// let launcher: EvolutionLauncher<
    ///     MyPhenotype,
    ///     OrdinaryStrategy,
    ///     ElitistSelection,
    ///     HillClimbing,
    ///     MyChallenge,
    ///     AllIndividualsStrategy
    /// > = EvolutionLauncher::new(
    ///     breed_strategy,
    ///     selection_strategy,
    ///     None, // No local search
    ///     challenge
    /// );
    /// ```
    pub fn new(
        breed_strategy: B,
        selection_strategy: S,
        local_search_manager: Option<LocalSearchManager<P, LS, A, F>>,
        challenge: F,
    ) -> Self {
        Self {
            breed_strategy,
            selection_strategy,
            local_search_manager,
            challenge,
        }
    }

    /// Returns a builder for creating an `EvolutionLauncher` instance.
    ///
    /// The builder pattern provides a more flexible way to create an `EvolutionLauncher`
    /// by allowing you to set components one at a time.
    ///
    /// # Returns
    ///
    /// A new `EvolutionLauncherBuilder` instance.
    ///
    /// # Example
    ///
    /// ```rust
    /// # use genalg::{
    /// #     evolution::{Challenge, EvolutionLauncher, EvolutionOptions},
    /// #     phenotype::Phenotype,
    /// #     rng::RandomNumberGenerator,
    /// #     breeding::OrdinaryStrategy,
    /// #     selection::ElitistSelection,
    /// #     local_search::{HillClimbing, AllIndividualsStrategy},
    /// #     error::Result,
    /// # };
    /// #
    /// # #[derive(Clone, Debug)]
    /// # struct MyPhenotype { value: f64 }
    /// # impl Phenotype for MyPhenotype {
    /// #     fn crossover(&mut self, other: &Self) {}
    /// #     fn mutate(&mut self, _rng: &mut RandomNumberGenerator) {}
    /// # }
    /// #
    /// # #[derive(Clone)]
    /// # struct MyChallenge;
    /// # impl Challenge<MyPhenotype> for MyChallenge {
    /// #     fn score(&self, _phenotype: &MyPhenotype) -> f64 { 0.0 }
    /// # }
    ///
    /// fn create_launcher() -> Result<EvolutionLauncher<
    ///     MyPhenotype,
    ///     OrdinaryStrategy,
    ///     ElitistSelection,
    ///     HillClimbing,
    ///     MyChallenge,
    ///     AllIndividualsStrategy
    /// >> {
    ///     let breed_strategy = OrdinaryStrategy::default();
    ///     let selection_strategy = ElitistSelection::default();
    ///     let challenge = MyChallenge;
    ///     
    ///     // Create a local search strategy and application strategy
    ///     let hill_climbing = HillClimbing::new(10, 10)?;
    ///     let application_strategy = AllIndividualsStrategy::new();
    ///     
    ///     // Use the builder pattern
    ///     EvolutionLauncher::builder()
    ///         .with_breed_strategy(breed_strategy)
    ///         .with_selection_strategy(selection_strategy)
    ///         .with_local_search_manager(hill_climbing, application_strategy)
    ///         .with_challenge(challenge)
    ///         .build()
    /// }
    /// ```
    pub fn builder() -> EvolutionLauncherBuilder<P, B, S, LS, F, A> {
        EvolutionLauncherBuilder::new()
    }

    /// Configures the evolution process with the provided options and starting value.
    ///
    /// This method returns an `EvolutionProcess` that can be further configured
    /// before running the evolution.
    ///
    /// # Arguments
    ///
    /// * `options` - Evolution options controlling the evolution process.
    /// * `starting_value` - The initial phenotype from which evolution begins.
    ///
    /// # Returns
    ///
    /// An `EvolutionProcess` that can be used to run the evolution.
    ///
    /// # Example
    ///
    /// ```rust
    /// # use genalg::{
    /// #     evolution::{Challenge, EvolutionLauncher, EvolutionOptions, LogLevel},
    /// #     phenotype::Phenotype,
    /// #     rng::RandomNumberGenerator,
    /// #     breeding::OrdinaryStrategy,
    /// #     selection::ElitistSelection,
    /// #     local_search::{HillClimbing, AllIndividualsStrategy},
    /// # };
    /// # #[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;
    /// #     }
    /// # }
    /// # #[derive(Clone)]
    /// # struct MyChallenge { target: f64 }
    /// # impl Challenge<MyPhenotype> for MyChallenge {
    /// #     fn score(&self, phenotype: &MyPhenotype) -> f64 { 1.0 / (phenotype.value - self.target).abs().max(0.001) }
    /// # }
    /// # let breed_strategy = OrdinaryStrategy::default();
    /// # let selection_strategy = ElitistSelection::default();
    /// # let challenge = MyChallenge { target: 42.0 };
    /// # let options = EvolutionOptions::default();
    /// # let starting_value = MyPhenotype { value: 0.0 };
    /// let launcher: EvolutionLauncher<
    ///     MyPhenotype,
    ///     OrdinaryStrategy,
    ///     ElitistSelection,
    ///     HillClimbing,
    ///     MyChallenge,
    ///     AllIndividualsStrategy
    /// > = EvolutionLauncher::new(breed_strategy, selection_strategy, None, challenge);
    ///
    /// // Configure the evolution process
    /// let process = launcher.configure(options, starting_value);
    /// ```
    pub fn configure(
        &self,
        options: EvolutionOptions,
        starting_value: P,
    ) -> EvolutionProcess<P, B, S, LS, F, A> {
        EvolutionProcess {
            launcher: self,
            options,
            starting_value,
            use_local_search: false,
            seed: None,
        }
    }

    /// Evolves a population of phenotypes over multiple generations.
    ///
    /// This is an internal method used by `EvolutionProcess::run()`.
    /// For public use, use the `configure()` method instead.
    ///
    /// # Arguments
    ///
    /// * `options` - Evolution options controlling the evolution process.
    /// * `rng` - A random number generator for introducing randomness.
    /// * `starting_value` - The initial phenotype from which evolution begins.
    /// * `use_local_search` - Whether to apply local search during evolution.
    ///
    /// # Returns
    ///
    /// A `Result` containing the best-evolved phenotype and its associated score,
    /// or a `GeneticError` if evolution fails.
    ///
    /// # Process
    ///
    /// The evolution process follows these steps for each generation:
    /// 1. Evaluate the fitness of all candidates
    /// 2. Select parents for the next generation based on fitness
    /// 3. Breed new candidates from the selected parents
    /// 4. Apply local search to refine solutions (if enabled)
    /// 5. Repeat until the maximum number of generations is reached
    fn evolve(
        &self,
        options: &EvolutionOptions,
        rng: &mut RandomNumberGenerator,
        starting_value: P,
        use_local_search: bool,
    ) -> Result<EvolutionResult<P>> {
        if options.get_population_size() == 0 {
            return Err(GeneticError::Configuration(
                "Population size cannot be zero".to_string(),
            ));
        }

        if options.get_num_offspring() == 0 {
            return Err(GeneticError::Configuration(
                "Number of offspring cannot be zero".to_string(),
            ));
        }

        let mut fitness: Vec<EvolutionResult<P>> = Vec::new();

        // Initialize the population
        let mut candidates: Vec<P> = Vec::new();
        let mut parents: Vec<P> = vec![starting_value];
        self.breed(&mut candidates, &parents, options, rng, 0)?;

        for generation in 0..options.get_num_generations() {
            let population: Vec<P> = candidates.to_vec();

            // 1. Evaluation
            if candidates.len() >= options.get_parallel_threshold() {
                fitness = self.fitness_parallel(&candidates, &self.challenge)?;
            } else {
                fitness = self.fitness_sequential(&candidates, &self.challenge)?;
            }

            // Extract scores from the newly calculated fitness
            let scores: Vec<f64> = fitness.iter().map(|f| f.score).collect();

            match options.get_log_level() {
                LogLevel::Info => info!(generation, "Evolution progress"),
                LogLevel::Debug => {
                    fitness.iter().for_each(|result| {
                        debug!(
                            generation,
                            phenotype = ?result.pheno,
                            score = result.score,
                            "Evolution detailed progress"
                        );
                    });
                }
                LogLevel::None => {}
            }

            // 2. Selection
            parents = self.selection_strategy.select(
                &population,
                &scores,
                options.get_population_size(),
            )?;

            // 3. Breeding
            self.breed(&mut candidates, &parents, options, rng, generation)?;

            // 4. Local Search
            if use_local_search {
                match &self.local_search_manager {
                    Some(manager) => {
                        manager.apply(&mut candidates, &scores, &self.challenge)?;
                    }
                    None => {
                        return Err(GeneticError::Configuration(
                            "Local search algorithm not provided".to_string(),
                        ))
                    }
                }
            }

            if fitness.is_empty() {
                return Err(GeneticError::Evolution(format!(
                    "No viable candidates produced in generation {}",
                    generation
                )));
            }
        }

        fitness.sort_by(|a, b| {
            b.score.partial_cmp(&a.score).unwrap_or_else(|| {
                if b.score.is_nan() {
                    std::cmp::Ordering::Less
                } else if a.score.is_nan() {
                    std::cmp::Ordering::Greater
                } else {
                    std::cmp::Ordering::Equal
                }
            })
        });

        fitness.first().cloned().ok_or_else_genetic(|| {
            GeneticError::Evolution(
                "Evolution completed but no viable candidates were produced".to_string(),
            )
        })
    }

    /// Breeds new candidates using the breeding strategy.
    ///
    /// This internal method is called during the evolution process to generate
    /// new candidates from the selected parents.
    ///
    /// # Arguments
    ///
    /// * `candidates` - The vector to store the newly bred candidates.
    /// * `parents` - The selected parents for breeding.
    /// * `options` - Evolution options controlling the breeding process.
    /// * `rng` - A random number generator for introducing randomness.
    /// * `generation` - The current generation number (for error reporting).
    ///
    /// # Returns
    ///
    /// A `Result` indicating success or failure of the breeding process.
    fn breed(
        &self,
        candidates: &mut Vec<P>,
        parents: &[P],
        options: &EvolutionOptions,
        rng: &mut RandomNumberGenerator,
        generation: usize,
    ) -> std::result::Result<(), GeneticError> {
        match self.breed_strategy.breed(parents, options, rng) {
            Ok(bred_candidates) => {
                candidates.extend(bred_candidates);
                Ok(())
            }
            Err(e) => Err(GeneticError::Breeding(format!(
                "Failed to breed candidates in generation {}: {}",
                generation, e
            ))),
        }
    }

    /// Evaluates the fitness of candidates in parallel.
    ///
    /// This method is used when the number of candidates exceeds the parallel threshold.
    ///
    /// # Arguments
    ///
    /// * `candidates` - The candidates to evaluate.
    /// * `challenge` - The challenge used to evaluate fitness.
    ///
    /// # Returns
    ///
    /// A `Result` containing the evaluated candidates with their fitness scores.
    fn fitness_parallel(&self, candidates: &[P], challenge: &F) -> Result<Vec<EvolutionResult<P>>> {
        candidates
            .par_iter()
            .map(|candidate| {
                let score = challenge.score(candidate);

                // Check for invalid fitness scores
                if !score.is_finite() {
                    return Err(GeneticError::FitnessCalculation(format!(
                        "Non-finite fitness score encountered: {}",
                        score
                    )));
                }

                Ok(EvolutionResult {
                    pheno: candidate.clone(),
                    score,
                })
            })
            .collect()
    }

    /// Evaluates the fitness of candidates sequentially.
    ///
    /// This method is used when the number of candidates is below the parallel threshold.
    ///
    /// # Arguments
    ///
    /// * `candidates` - The candidates to evaluate.
    /// * `challenge` - The challenge used to evaluate fitness.
    ///
    /// # Returns
    ///
    /// A `Result` containing the evaluated candidates with their fitness scores.
    fn fitness_sequential(
        &self,
        candidates: &[P],
        challenge: &F,
    ) -> Result<Vec<EvolutionResult<P>>> {
        candidates
            .iter()
            .map(|candidate| {
                let score = challenge.score(candidate);

                if !score.is_finite() {
                    return Err(GeneticError::FitnessCalculation(format!(
                        "Non-finite fitness score encountered: {}",
                        score
                    )));
                }

                Ok(EvolutionResult {
                    pheno: candidate.clone(),
                    score,
                })
            })
            .collect()
    }
}

/// Process for running an evolution with the specified configuration.
///
/// The `EvolutionProcess` struct is a builder for an evolution run.
/// It allows configuring various aspects of the evolution process
/// before running it.
///
/// # Type Parameters
///
/// * `P` - The phenotype type that represents individuals in the population
/// * `B` - The breeding strategy type
/// * `S` - The selection strategy type
/// * `LS` - The local search algorithm type
/// * `F` - The challenge (fitness function) type
/// * `A` - The local search application strategy type
pub struct EvolutionProcess<'a, P, B, S, LS, F, A>
where
    P: Phenotype,
    B: BreedStrategy<P>,
    S: SelectionStrategy<P>,
    LS: LocalSearch<P, F> + Clone,
    F: Challenge<P> + Clone,
    A: LocalSearchApplicationStrategy<P> + Clone,
{
    launcher: &'a EvolutionLauncher<P, B, S, LS, F, A>,
    options: EvolutionOptions,
    starting_value: P,
    use_local_search: bool,
    seed: Option<u64>,
}

impl<P, B, S, LS, F, A> EvolutionProcess<'_, P, B, S, LS, F, A>
where
    P: Phenotype + Send + Sync,
    LS: LocalSearch<P, F> + Clone + Send + Sync,
    F: Challenge<P> + Clone + Send + Sync,
    A: LocalSearchApplicationStrategy<P> + Clone + Send + Sync,
    B: BreedStrategy<P> + Clone + Send + Sync,
    S: SelectionStrategy<P> + Clone + Send + Sync,
{
    /// Sets a seed for the random number generator.
    ///
    /// Using a seed ensures reproducible evolution runs.
    ///
    /// # Arguments
    ///
    /// * `seed` - The seed for the random number generator.
    ///
    /// # Returns
    ///
    /// The `EvolutionProcess` with the seed set.
    ///
    /// # Example
    ///
    /// ```rust
    /// # use genalg::{
    /// #     evolution::{Challenge, EvolutionLauncher, EvolutionOptions, LogLevel},
    /// #     phenotype::Phenotype,
    /// #     rng::RandomNumberGenerator,
    /// #     breeding::OrdinaryStrategy,
    /// #     selection::ElitistSelection,
    /// #     local_search::{HillClimbing, AllIndividualsStrategy, LocalSearchManager},
    /// # };
    /// # #[derive(Clone, Debug)]
    /// # struct MyPhenotype { value: f64 }
    /// # impl Phenotype for MyPhenotype {
    /// #     fn crossover(&mut self, other: &Self) {}
    /// #     fn mutate(&mut self, _rng: &mut RandomNumberGenerator) {}
    /// # }
    /// # #[derive(Clone)]
    /// # struct MyChallenge;
    /// # impl Challenge<MyPhenotype> for MyChallenge {
    /// #     fn score(&self, _phenotype: &MyPhenotype) -> f64 { 0.0 }
    /// # }
    /// # let hill_climbing = HillClimbing::new(10, 10).unwrap();
    /// # let application_strategy = AllIndividualsStrategy::new();
    /// # let local_search_manager = LocalSearchManager::new(hill_climbing, application_strategy);
    /// # let breed_strategy = OrdinaryStrategy::default();
    /// # let selection_strategy = ElitistSelection::default();
    /// # let challenge = MyChallenge;
    /// # let options = EvolutionOptions::default();
    /// # let starting_value = MyPhenotype { value: 0.0 };
    /// # let launcher: EvolutionLauncher<MyPhenotype, OrdinaryStrategy, ElitistSelection, HillClimbing, MyChallenge, AllIndividualsStrategy> = EvolutionLauncher::new(breed_strategy, selection_strategy, Some(local_search_manager), challenge);
    ///
    /// // Configure the evolution process and set a specific seed
    /// let process = launcher
    ///     .configure(options, starting_value)
    ///     .with_seed(42);
    /// ```
    pub fn with_seed(mut self, seed: u64) -> Self {
        self.seed = Some(seed);
        self
    }

    /// Enables local search in the evolution process.
    ///
    /// When local search is enabled, the evolution process will apply
    /// the configured local search algorithm to refine solutions.
    ///
    /// # Returns
    ///
    /// The `EvolutionProcess` with local search enabled.
    ///
    /// # Example
    ///
    /// ```rust
    /// # use genalg::{
    /// #     evolution::{Challenge, EvolutionLauncher, EvolutionOptions, LogLevel},
    /// #     phenotype::Phenotype,
    /// #     rng::RandomNumberGenerator,
    /// #     breeding::OrdinaryStrategy,
    /// #     selection::ElitistSelection,
    /// #     local_search::{HillClimbing, AllIndividualsStrategy, LocalSearchManager},
    /// # };
    /// # #[derive(Clone, Debug)]
    /// # struct MyPhenotype { value: f64 }
    /// # impl Phenotype for MyPhenotype {
    /// #     fn crossover(&mut self, other: &Self) {}
    /// #     fn mutate(&mut self, _rng: &mut RandomNumberGenerator) {}
    /// # }
    /// # #[derive(Clone)]
    /// # struct MyChallenge;
    /// # impl Challenge<MyPhenotype> for MyChallenge {
    /// #     fn score(&self, _phenotype: &MyPhenotype) -> f64 { 0.0 }
    /// # }
    /// # let hill_climbing = HillClimbing::new(10, 10).unwrap();
    /// # let application_strategy = AllIndividualsStrategy::new();
    /// # let local_search_manager = LocalSearchManager::new(hill_climbing, application_strategy);
    /// # let breed_strategy = OrdinaryStrategy::default();
    /// # let selection_strategy = ElitistSelection::default();
    /// # let challenge = MyChallenge;
    /// # let options = EvolutionOptions::default();
    /// # let starting_value = MyPhenotype { value: 0.0 };
    /// # let launcher: EvolutionLauncher<MyPhenotype, OrdinaryStrategy, ElitistSelection, HillClimbing, MyChallenge, AllIndividualsStrategy> = EvolutionLauncher::new(breed_strategy, selection_strategy, Some(local_search_manager), challenge);
    ///
    /// // Configure the evolution process with local search enabled
    /// let process = launcher
    ///     .configure(options, starting_value)
    ///     .with_local_search();
    /// ```
    pub fn with_local_search(mut self) -> Self {
        self.use_local_search = true;
        self
    }

    /// Runs the evolution process.
    ///
    /// This method runs the evolution process with the configured options
    /// and returns the best individual found.
    ///
    /// # Returns
    ///
    /// A `Result` containing the best individual found and its fitness score,
    /// or an error if the evolution process failed.
    ///
    /// # Example
    ///
    /// ```rust
    /// # use genalg::{
    /// #     evolution::{Challenge, EvolutionLauncher, EvolutionOptions, LogLevel},
    /// #     phenotype::Phenotype,
    /// #     rng::RandomNumberGenerator,
    /// #     breeding::OrdinaryStrategy,
    /// #     selection::ElitistSelection,
    /// #     local_search::{HillClimbing, AllIndividualsStrategy},
    /// # };
    /// # #[derive(Clone, Debug)]
    /// # struct MyPhenotype { value: f64 }
    /// # impl Phenotype for MyPhenotype {
    /// #     fn crossover(&mut self, other: &Self) {}
    /// #     fn mutate(&mut self, _rng: &mut RandomNumberGenerator) {}
    /// # }
    /// # #[derive(Clone)]
    /// # struct MyChallenge;
    /// # impl Challenge<MyPhenotype> for MyChallenge {
    /// #     fn score(&self, _phenotype: &MyPhenotype) -> f64 { 0.0 }
    /// # }
    /// # let breed_strategy = OrdinaryStrategy::default();
    /// # let selection_strategy = ElitistSelection::default();
    /// # let challenge = MyChallenge;
    /// # let options = EvolutionOptions::default();
    /// # let starting_value = MyPhenotype { value: 0.0 };
    /// # let launcher: EvolutionLauncher<MyPhenotype, OrdinaryStrategy, ElitistSelection, HillClimbing, MyChallenge, AllIndividualsStrategy> = EvolutionLauncher::new(breed_strategy, selection_strategy, None, challenge);
    ///
    /// // Run the evolution process
    /// let result = launcher
    ///     .configure(options, starting_value)
    ///     .run();
    /// ```
    pub fn run(self) -> Result<EvolutionResult<P>> {
        let mut rng = match self.seed {
            Some(seed) => RandomNumberGenerator::from_seed(seed),
            None => RandomNumberGenerator::new(),
        };

        // Use local search if specified
        let use_local_search = if self.launcher.local_search_manager.is_some() {
            self.use_local_search
        } else {
            false
        };

        // Run the evolution without caching
        self.launcher.evolve(
            &self.options,
            &mut rng,
            self.starting_value,
            use_local_search,
        )
    }
}