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
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
//! Structured lifecycle observer for the GA execution loop.
//!
//! This module provides the [`GaObserver`] trait, which exposes 12 hooks that
//! fire at precise points in the GA run loop. Observers:
//!
//! - Are stored as `Arc<dyn GaObserver + Send + Sync>` (sharable across island threads)
//! - Receive `&self` (not `&mut self`), enabling interior mutability patterns
//! - Cover 13 lifecycle, operator-timing, and special-event hooks
//! - Require `Send + Sync` for safe use in rayon parallel regions
//!
//! # Hooks
//!
//! | Hook | When it fires |
//! |------|--------------|
//! | `on_run_start` | Once before the first generation |
//! | `on_generation_start` | Start of each generation, before any operators |
//! | `on_selection_complete` | After parent selection |
//! | `on_crossover_complete` | After crossover produces offspring |
//! | `on_mutation_complete` | After mutation is applied |
//! | `on_fitness_evaluation_complete` | After fitness evaluation of new population |
//! | `on_survivor_selection_complete` | After survivor selection prunes population |
//! | `on_new_best` | When the population's best fitness improves |
//! | `on_stagnation` | Each time the stagnation counter increments |
//! | `on_extension_triggered` | When an extension strategy fires |
//! | `on_generation_end` | End of each generation, after statistics collected |
//! | `on_run_end` | Once after the GA loop exits |
//! | `on_restart` | When the CMA-ES engine triggers an automatic restart |

use crate::cma::restart::RestartEvent;
use crate::ga::TerminationCause;
use crate::stats::GenerationStats;
use crate::traits::ChromosomeT;
use std::time::Duration;

/// Payload for the [`GaObserver::on_extension_triggered`] hook.
///
/// Stack-allocated and `Copy`-able — zero heap allocation.
///
/// # Examples
///
/// ```rust
/// use genetic_algorithms::observer::ExtensionEvent;
///
/// let event = ExtensionEvent {
///     generation: 42,
///     diversity: 0.15,
///     extension_type: "MassExtinction",
///     threshold: 0.2,
/// };
/// assert_eq!(event.generation, 42);
/// ```
#[derive(Debug, Clone, Copy)]
pub struct ExtensionEvent {
    /// The generation at which the extension fired.
    pub generation: usize,
    /// Population diversity at the time of extension.
    pub diversity: f64,
    /// Name of the extension strategy (e.g. `"MassExtinction"`).
    pub extension_type: &'static str,
    /// Diversity threshold that triggered the extension.
    pub threshold: f64,
}

/// Structured lifecycle observer for [`Ga<U>`](crate::ga::Ga).
///
/// All methods have default no-op implementations — implement only the hooks
/// you need. The `Send + Sync` supertraits are required for safe sharing
/// across rayon threads (island model) via `Arc`.
///
/// # GaObserver vs the removed Reporter trait
///
/// | Aspect | Reporter (removed v3.0) | GaObserver |
/// |--------|------------------------|------------|
/// | Storage | `Box<dyn Reporter + Send>` | `Arc<dyn GaObserver + Send + Sync>` |
/// | Mutability | `&mut self` | `&self` |
/// | Hooks | 4 lifecycle | 13 (lifecycle + operator + special) |
/// | Thread safety | `Send` only | `Send + Sync` |
///
/// See [`MIGRATION.md`](https://docs.rs/genetic_algorithms) for migration recipes.
///
/// # Examples
///
/// ```rust,no_run
/// use genetic_algorithms::observer::GaObserver;
/// use genetic_algorithms::chromosomes::Binary;
/// use genetic_algorithms::stats::GenerationStats;
/// use genetic_algorithms::ga::TerminationCause;
///
/// struct MyObserver;
///
/// impl GaObserver<Binary> for MyObserver {
///     fn on_run_start(&self) { println!("GA started"); }
///     fn on_run_end(&self, cause: TerminationCause, _stats: &[GenerationStats]) {
///         println!("GA ended: {:?}", cause);
///     }
/// }
/// ```
pub trait GaObserver<U: ChromosomeT>: Send + Sync {
    /// Called once before the first generation.
    fn on_run_start(&self) {}
    /// Called at the start of each generation, before any operators run.
    fn on_generation_start(&self, _generation: usize) {}
    /// Called after parent selection completes.
    fn on_selection_complete(
        &self,
        _generation: usize,
        _duration: Duration,
        _population_size: usize,
    ) {
    }
    /// Called after crossover produces offspring.
    fn on_crossover_complete(
        &self,
        _generation: usize,
        _duration: Duration,
        _offspring_count: usize,
    ) {
    }
    /// Called after mutation is applied to offspring.
    fn on_mutation_complete(
        &self,
        _generation: usize,
        _duration: Duration,
        _population_size: usize,
    ) {
    }
    /// Called after fitness evaluation of the new population.
    fn on_fitness_evaluation_complete(
        &self,
        _generation: usize,
        _duration: Duration,
        _population_size: usize,
    ) {
    }
    /// Called after survivor selection prunes the population.
    fn on_survivor_selection_complete(
        &self,
        _generation: usize,
        _duration: Duration,
        _population_size: usize,
    ) {
    }
    /// Called when the population's best fitness improves.
    fn on_new_best(&self, _generation: usize, _best: &U) {}
    /// Called each time the stagnation counter increments.
    fn on_stagnation(&self, _generation: usize, _stagnation_count: usize) {}
    /// Called when an extension strategy fires due to low diversity.
    fn on_extension_triggered(&self, _event: ExtensionEvent) {}
    /// Called when the CMA-ES engine triggers an automatic restart.
    ///
    /// Fires once per restart event, after state has been reset and before the next
    /// restart's generation loop begins. Only relevant when a [`RestartStrategy`](crate::cma::RestartStrategy)
    /// is configured on [`CmaConfiguration`](crate::cma::CmaConfiguration).
    fn on_restart(&self, _event: &RestartEvent) {}
    /// Called at the end of each generation, after statistics are collected.
    fn on_generation_end(&self, _stats: &GenerationStats) {}
    /// Called once after the GA loop exits.
    fn on_run_end(&self, _cause: TerminationCause, _all_stats: &[GenerationStats]) {}
}

/// Zero-sized no-op observer. All hooks use their default empty bodies.
///
/// Useful as a compile-check type or as a placeholder.
///
/// # Examples
///
/// ```rust
/// use genetic_algorithms::observer::NoopObserver;
///
/// let _obs = NoopObserver;
/// ```
pub struct NoopObserver;

impl<U: ChromosomeT> GaObserver<U> for NoopObserver {}
impl<U: ChromosomeT> IslandGaObserver<U> for NoopObserver {}
impl<U: ChromosomeT> Nsga2Observer<U> for NoopObserver {}

/// Observer for [`IslandGa<U>`](crate::island::IslandGa) engine-specific events.
///
/// All methods have default no-op implementations. The `Send + Sync`
/// supertraits are required for safe sharing across rayon island threads via `Arc`.
///
/// # Examples
///
/// ```rust,no_run
/// use genetic_algorithms::observer::IslandGaObserver;
/// use genetic_algorithms::chromosomes::Binary;
///
/// struct MyIslandObserver;
///
/// impl IslandGaObserver<Binary> for MyIslandObserver {
///     fn on_island_run_start(&self, island_id: usize) {
///         println!("Island {} started", island_id);
///     }
/// }
/// ```
pub trait IslandGaObserver<U: ChromosomeT>: Send + Sync {
    /// Called when an island run starts.
    fn on_island_run_start(&self, _island_id: usize) {}
    /// Called when an island run ends.
    fn on_island_run_end(&self, _island_id: usize) {}
    /// Called at the end of each generation for each island.
    fn on_island_generation_end(
        &self,
        _island_id: usize,
        _generation: usize,
        _stats: &GenerationStats,
    ) {
    }
    /// Called when migration is triggered between islands.
    fn on_migration_triggered(&self, _generation: usize, _migration_count: usize) {}
}

/// Observer for [`Nsga2Ga<U>`](crate::nsga2::Nsga2Ga) engine-specific events.
///
/// All methods have default no-op implementations. The `Send + Sync`
/// supertraits are required for safe sharing across rayon threads via `Arc`.
///
/// # Examples
///
/// ```rust,no_run
/// use genetic_algorithms::observer::Nsga2Observer;
/// use genetic_algorithms::chromosomes::Range;
///
/// struct MyNsga2Observer;
///
/// impl Nsga2Observer<Range<f64>> for MyNsga2Observer {
///     fn on_pareto_front_assigned(&self, generation: usize, front_count: usize, _pop: usize) {
///         println!("Gen {}: {} Pareto fronts", generation, front_count);
///     }
/// }
/// ```
pub trait Nsga2Observer<U: ChromosomeT>: Send + Sync {
    /// Called after Pareto fronts are assigned.
    fn on_pareto_front_assigned(
        &self,
        _generation: usize,
        _front_count: usize,
        _population_size: usize,
    ) {
    }
    /// Called after non-dominated sorting completes.
    fn on_non_dominated_sort_complete(&self, _generation: usize, _duration_ms: f64) {}
    /// Called after crowding distance calculation completes.
    fn on_crowding_distance_calculated(&self, _generation: usize, _duration_ms: f64) {}
}

/// Observer for [`Nsga3Ga<U>`](crate::nsga3::Nsga3Ga) engine-specific events.
///
/// All methods have default no-op implementations. The `Send + Sync`
/// supertraits are required for safe sharing across rayon threads via `Arc`.
///
/// # Note: not in `AllObserver`
///
/// `AllObserver<U>` does NOT include `Nsga3Observer<U>` in Phase 35 — adding it
/// would be a breaking change for existing `AllObserver` implementors. Use
/// [`Nsga3Ga::with_observer`](crate::nsga3::Nsga3Ga::with_observer) to attach
/// an `Nsga3Observer` independently.
///
/// # Examples
///
/// ```rust,no_run
/// use genetic_algorithms::observer::Nsga3Observer;
/// use genetic_algorithms::chromosomes::Range;
///
/// struct MyNsga3Observer;
/// impl Nsga3Observer<Range<f64>> for MyNsga3Observer {}
/// ```
pub trait Nsga3Observer<U: ChromosomeT>: Send + Sync {
    /// Called after Pareto fronts are assigned for the current generation.
    fn on_pareto_front_assigned(
        &self,
        _generation: usize,
        _front_count: usize,
        _population_size: usize,
    ) {
    }
    /// Called after non-dominated sorting completes for the current generation.
    fn on_non_dominated_sort_complete(&self, _generation: usize, _duration_ms: f64) {}
}

/// Observer for [`MoeaDGa<U>`](crate::moead::MoeaDGa) engine-specific events.
///
/// All methods have default no-op implementations. The `Send + Sync`
/// supertraits are required for safe sharing across rayon threads via `Arc`.
///
/// # Note: not in `AllObserver`
///
/// `AllObserver<U>` does NOT include `MoeaDObserver<U>` in Phase 36 — adding it
/// would be a breaking change for existing `AllObserver` implementors. Use
/// [`MoeaDGa::with_observer`](crate::moead::MoeaDGa::with_observer) to attach
/// a `MoeaDObserver` independently.
///
/// # Examples
///
/// ```rust,no_run
/// use genetic_algorithms::observer::MoeaDObserver;
/// use genetic_algorithms::chromosomes::Range;
///
/// struct MyMoeaDObserver;
/// impl MoeaDObserver<Range<f64>> for MyMoeaDObserver {}
/// ```
pub trait MoeaDObserver<U: ChromosomeT>: Send + Sync {
    /// Called after Pareto fronts are assigned for the current generation.
    fn on_pareto_front_assigned(
        &self,
        _generation: usize,
        _front_count: usize,
        _population_size: usize,
    ) {
    }
    /// Called after non-dominated sorting completes for the current generation.
    fn on_non_dominated_sort_complete(&self, _generation: usize, _duration_ms: f64) {}
}

/// Observer for [`Spea2Ga<U>`](crate::spea2::Spea2Ga) engine-specific events.
///
/// All methods have default no-op implementations. The `Send + Sync`
/// supertraits are required for safe sharing across rayon threads via `Arc`.
///
/// # Note: not in `AllObserver`
///
/// `AllObserver<U>` does NOT include `Spea2Observer<U>` in Phase 37 — adding it
/// would be a breaking change for existing `AllObserver` implementors (D-07). Use
/// [`Spea2Ga::with_observer`](crate::spea2::Spea2Ga::with_observer) to attach
/// a `Spea2Observer` independently.
///
/// # Examples
///
/// ```rust,no_run
/// use genetic_algorithms::observer::Spea2Observer;
/// use genetic_algorithms::chromosomes::Range;
///
/// struct MySpea2Observer;
/// impl Spea2Observer<Range<f64>> for MySpea2Observer {}
/// ```
pub trait Spea2Observer<U: ChromosomeT>: Send + Sync {
    /// Called after strength + density fitness is assigned to all individuals
    /// in the combined population + archive set.
    fn on_fitness_assigned(
        &self,
        _generation: usize,
        _duration_ms: f64,
        _pop_size: usize,
        _archive_size: usize,
    ) {
    }
    /// Called after environmental selection completes and the archive is updated
    /// (including truncation if necessary).
    fn on_archive_updated(
        &self,
        _generation: usize,
        _archive_size: usize,
        _non_dominated_count: usize,
    ) {
    }
}

/// Observer for [`SmsEmoaGa<U>`](crate::sms_emoa::SmsEmoaGa) engine-specific events.
///
/// All methods have default no-op implementations. The `Send + Sync`
/// supertraits are required for safe sharing across rayon threads via `Arc`.
///
/// # Note: not in `AllObserver`
///
/// `AllObserver<U>` does NOT include `SmsEmoaObserver<U>` in Phase 38 — adding it
/// would be a breaking change for existing `AllObserver` implementors. Use
/// [`SmsEmoaGa::with_observer`](crate::sms_emoa::SmsEmoaGa::with_observer) to attach
/// a `SmsEmoaObserver` independently.
///
/// # Examples
///
/// ```rust,no_run
/// use genetic_algorithms::observer::SmsEmoaObserver;
/// use genetic_algorithms::chromosomes::Range;
///
/// struct MySmsEmoaObserver;
/// impl SmsEmoaObserver<Range<f64>> for MySmsEmoaObserver {}
/// ```
pub trait SmsEmoaObserver<U: ChromosomeT>: Send + Sync {
    /// Called after hypervolume contribution calculation completes for the current generation.
    fn on_hypervolume_contribution_assigned(
        &self,
        _generation: usize,
        _duration_ms: f64,
        _population_size: usize,
    ) {
    }
    /// Called after steady-state (mu+1) removal in the current generation.
    fn on_steady_state_removal(&self, _generation: usize, _population_size: usize) {}
}

/// Observer for [`IbeaGa<U>`](crate::ibea::IbeaGa) engine-specific events.
///
/// All methods have default no-op implementations. The `Send + Sync`
/// supertraits are required for safe sharing across rayon threads via `Arc`.
///
/// # Note: not in `AllObserver`
///
/// `AllObserver<U>` does NOT include `IbeaObserver<U>` in Phase 38 — adding it
/// would be a breaking change for existing `AllObserver` implementors. Use
/// [`IbeaGa::with_observer`](crate::ibea::IbeaGa::with_observer) to attach
/// an `IbeaObserver` independently.
///
/// # Examples
///
/// ```rust,no_run
/// use genetic_algorithms::observer::IbeaObserver;
/// use genetic_algorithms::chromosomes::Range;
///
/// struct MyIbeaObserver;
/// impl IbeaObserver<Range<f64>> for MyIbeaObserver {}
/// ```
pub trait IbeaObserver<U: ChromosomeT>: Send + Sync {
    /// Called after indicator-based fitness assignment completes for the current generation.
    fn on_indicator_fitness_assigned(
        &self,
        _generation: usize,
        _duration_ms: f64,
        _population_size: usize,
    ) {
    }
    /// Called after environmental selection removes an individual.
    fn on_environmental_selection(
        &self,
        _generation: usize,
        _population_size: usize,
        _removed_index: usize,
    ) {
    }
}

/// Observer for [`CmaEngine<U>`](crate::cma::CmaEngine) engine-specific events.
///
/// All methods have default no-op implementations. The `Send + Sync`
/// supertraits are required for safe sharing across rayon threads via `Arc`.
///
/// # Note: not in `AllObserver`
///
/// `AllObserver<U>` does NOT include `CmaObserver<U>` — adding it would be a
/// breaking change for existing `AllObserver` implementors. Attach a
/// `CmaObserver` independently to a `CmaEngine` if your build wires it through.
///
/// # Examples
///
/// ```rust,no_run
/// use genetic_algorithms::observer::CmaObserver;
/// use genetic_algorithms::chromosomes::Range;
///
/// struct MyCmaObserver;
/// impl CmaObserver<Range<f64>> for MyCmaObserver {}
/// ```
pub trait CmaObserver<U: ChromosomeT>: Send + Sync {
    /// Called after the global step size `sigma` is updated for the current generation.
    fn on_sigma_updated(&self, _generation: usize, _sigma: f64) {}
    /// Called after the covariance matrix is updated for the current generation.
    fn on_covariance_updated(&self, _generation: usize, _condition_number: f64) {}
    /// Called when eigendecomposition completes for the current generation,
    /// carrying the wall-clock duration in milliseconds.
    fn on_eigendecomposition_complete(&self, _generation: usize, _duration_ms: f64) {}
}

/// Observer for [`PsoEngine<U>`](crate::pso::PsoEngine) engine-specific events.
///
/// All methods have default no-op implementations. The `Send + Sync`
/// supertraits are required for safe sharing across rayon threads via `Arc`.
///
/// # Note: not in `AllObserver`
///
/// `AllObserver<U>` does NOT include `PsoObserver<U>` — adding it would be a
/// breaking change for existing `AllObserver` implementors. Attach a
/// `PsoObserver` independently to a `PsoEngine` if your build wires it through.
///
/// # Examples
///
/// ```rust,no_run
/// use genetic_algorithms::observer::PsoObserver;
/// use genetic_algorithms::chromosomes::Range;
///
/// struct MyPsoObserver;
/// impl PsoObserver<Range<f64>> for MyPsoObserver {}
/// ```
pub trait PsoObserver<U: ChromosomeT>: Send + Sync {
    /// Called after velocities are updated for the current generation.
    fn on_velocity_updated(&self, _generation: usize, _population_size: usize) {}
    /// Called after a particle's personal best is improved.
    fn on_personal_best_updated(&self, _generation: usize, _particle_index: usize, _fitness: f64) {}
    /// Called after the swarm's global (or neighborhood) best is updated.
    fn on_global_best_updated(&self, _generation: usize, _fitness: f64) {}
}

/// Observer for [`EdaEngine<U>`](crate::eda::EdaEngine) / [`EdaRealEngine<U>`](crate::eda::EdaRealEngine) engine-specific events.
///
/// All methods have default no-op implementations. The `Send + Sync`
/// supertraits are required for safe sharing across rayon threads via `Arc`.
///
/// # Note: not in `AllObserver`
///
/// `AllObserver<U>` does NOT include `EdaObserver<U>` — adding it would be a
/// breaking change for existing `AllObserver` implementors. Attach an
/// `EdaObserver` independently if your build wires it through.
///
/// # Examples
///
/// ```rust,no_run
/// use genetic_algorithms::observer::EdaObserver;
/// use genetic_algorithms::chromosomes::Binary;
///
/// struct MyEdaObserver;
/// impl EdaObserver<Binary> for MyEdaObserver {}
/// ```
pub trait EdaObserver<U: ChromosomeT>: Send + Sync {
    /// Called after the probabilistic model is re-estimated from the selected
    /// parents for the current generation.
    fn on_model_updated(&self, _generation: usize, _selected_count: usize) {}
    /// Called after offspring are sampled from the current model.
    fn on_offspring_sampled(&self, _generation: usize, _offspring_count: usize) {}
}

/// Combined observer bound for use with [`CompositeObserver`].
///
/// Any type that implements [`GaObserver<U>`], [`IslandGaObserver<U>`],
/// [`Nsga2Observer<U>`], and [`Send + Sync`] automatically satisfies this
/// supertrait via the blanket impl below.
///
/// `AllObserver<U>` has zero methods of its own — it is a pure supertrait
/// marker and is object-safe: `dyn AllObserver<U>` is valid.
///
/// # Examples
///
/// ```rust,no_run
/// use genetic_algorithms::observer::{GaObserver, IslandGaObserver, Nsga2Observer, AllObserver};
/// use genetic_algorithms::chromosomes::Binary;
///
/// struct MyAllObserver;
/// impl GaObserver<Binary> for MyAllObserver {}
/// impl IslandGaObserver<Binary> for MyAllObserver {}
/// impl Nsga2Observer<Binary> for MyAllObserver {}
/// // MyAllObserver now satisfies AllObserver<Binary> via blanket impl
/// ```
pub trait AllObserver<U: ChromosomeT>:
    GaObserver<U> + IslandGaObserver<U> + Nsga2Observer<U> + Send + Sync
{
}

impl<U, T> AllObserver<U> for T
where
    U: ChromosomeT,
    T: GaObserver<U> + IslandGaObserver<U> + Nsga2Observer<U> + Send + Sync,
{
}

#[cfg(feature = "logging")]
mod log;
#[cfg(feature = "logging")]
pub use log::LogObserver;

#[cfg(feature = "observer-tracing")]
mod tracing_observer;
#[cfg(feature = "observer-tracing")]
pub use tracing_observer::TracingObserver;

#[cfg(feature = "observer-metrics")]
mod metrics_observer;
#[cfg(feature = "observer-metrics")]
pub use metrics_observer::MetricsObserver;

mod composite;
pub use composite::CompositeObserver;