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 crateRestartEvent;
use crateTerminationCause;
use crateGenerationStats;
use crateChromosomeT;
use 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);
/// ```
/// 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);
/// }
/// }
/// ```
/// 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;
/// ```
;
/// 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);
/// }
/// }
/// ```
/// 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);
/// }
/// }
/// ```
/// 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 {}
/// ```
/// 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 {}
/// ```
/// 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 {}
/// ```
/// 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 {}
/// ```
/// 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 {}
/// ```
/// 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 {}
/// ```
/// 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 {}
/// ```
/// 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 {}
/// ```
/// 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 use LogObserver;
pub use TracingObserver;
pub use MetricsObserver;
pub use CompositeObserver;