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
//! # genetic_algorithms
//!
//! A modular, performant Rust library for evolutionary computation and metaheuristic optimization.
//! Provides 11 optimization engines, 45+ composable operators, a full lifecycle observer system,
//! and framework extensions — all generic over chromosome and gene types via traits.
//!
//! Published on [crates.io](https://crates.io/crates/genetic_algorithms) as `genetic_algorithms`.
//! API reference on [docs.rs](https://docs.rs/genetic_algorithms/latest/genetic_algorithms).
//!
//! ## Quick Start
//!
//! Minimize the Rastrigin function using 5-dimensional [`Range<f64>`](crate::genotypes::Range)
//! chromosomes with the standard [`Ga`](crate::ga::Ga) engine:
//!
//! ```rust,ignore
//! use genetic_algorithms::chromosomes::Range as RangeChromosome;
//! use genetic_algorithms::configuration::ProblemSolving;
//! use genetic_algorithms::ga::Ga;
//! use genetic_algorithms::genotypes::Range as RangeGenotype;
//! use genetic_algorithms::initializers::range_random_initialization;
//! use genetic_algorithms::operations::{Crossover, Mutation, Selection, Survivor};
//! use genetic_algorithms::traits::{ConfigurationT, CrossoverConfig, MutationConfig, SelectionConfig, StoppingConfig};
//! use genetic_algorithms::{CompositeObserver, LogObserver};
//! use std::sync::Arc;
//!
//! let fitness_fn = |dna: &[RangeGenotype<f64>]| -> f64 {
//! let a = 10.0;
//! let n = dna.len() as f64;
//! a * n + dna.iter()
//! .map(|g| g.value.powi(2) - a * (2.0 * std::f64::consts::PI * g.value).cos())
//! .sum::<f64>()
//! };
//!
//! let alleles = vec![RangeGenotype::new(0, vec![(-5.12, 5.12)], 0.0_f64)];
//! let alleles_clone = alleles.clone();
//!
//! let mut ga = Ga::new()
//! .with_genes_per_chromosome(5_usize)
//! .with_population_size(100)
//! .with_initialization_fn(move |genes_per_chromosome, _, _| {
//! range_random_initialization(genes_per_chromosome, Some(&alleles_clone), Some(false))
//! })
//! .with_fitness_fn(fitness_fn)
//! .with_selection_method(Selection::Tournament)
//! .with_crossover_method(Crossover::Uniform)
//! .with_mutation_method(Mutation::Gaussian)
//! .with_survivor_method(Survivor::Fitness)
//! .with_problem_solving(ProblemSolving::Minimization)
//! .with_max_generations(500)
//! .with_observer(Arc::new(CompositeObserver::new().add(Arc::new(LogObserver))))
//! .build()
//! .expect("Invalid configuration");
//!
//! let population = ga.run().expect("GA run failed");
//! println!("Best fitness: {:.4}", population.best_chromosome.fitness);
//! ```
//!
//! ## Engines (11 total)
//!
//! This crate offers 11 optimization engines, each designed for specific problem types:
//!
//! | Engine | Module | Objectives | Problem Type | Key Strength |
//! |--------|--------|------------|--------------|--------------|
//! | [`Ga<U>`](crate::ga::Ga) | `ga` | Single | Continuous / Combinatorial | General-purpose evolutionary optimization |
//! | [`DeEngine<U>`](crate::de::DeEngine) | `de` | Single | Continuous | Fast convergence on real-valued problems via differential mutation |
//! | [`ScatterEngine<U>`](crate::scatter::ScatterEngine) | `scatter` | Single | Continuous / Combinatorial | Reference-set based diversification |
//! | [`CellularEngine<U>`](crate::cellular::CellularEngine) | `cellular` | Single | Continuous / Combinatorial | Spatial diversity via toroidal grid neighborhoods |
//! | [`AlpsEngine<U>`](crate::alps::AlpsEngine) | `alps` | Single | Continuous / Combinatorial | Age-layered population structure for sustained diversity |
//! | [`IslandGa<U>`](crate::island::IslandGa) | `island` | Single | Any | Parallel sub-populations with configurable migration topologies |
//! | [`Nsga2Ga<U>`](crate::nsga2::Nsga2Ga) | `nsga2` | 2 | Continuous / Combinatorial | Fast Pareto ranking with crowding distance |
//! | [`Nsga3Ga<U>`](crate::nsga3::Nsga3Ga) | `nsga3` | 3+ | Continuous | Reference-point based niching for many-objective problems |
//! | [`MoeaDGa<U>`](crate::moead::MoeaDGa) | `moead` | 2+ | Continuous | Decomposition-based scalarization (Tchebycheff, PBI, weighted sum) |
//! | [`Spea2Ga<U>`](crate::spea2::Spea2Ga) | `spea2` | 2+ | Continuous / Combinatorial | Strength-Pareto archive with k-nearest density estimation |
//! | [`SmsEmoaGa<U>`](crate::sms_emoa::SmsEmoaGa) / [`IbeaGa<U>`](crate::ibea::IbeaGa) | `sms_emoa` / `ibea` | 2+ | Continuous | Hypervolume contribution / indicator-based selection |
//!
//! ### Single-Objective Engines
//!
//! - [`Ga<U>`](crate::ga::Ga) — Standard single-population GA. The general-purpose engine with full
//! operator support (all selection, crossover, mutation, survivor strategies), adaptive GA mode,
//! elitism, extension strategies, niching/fitness sharing, and checkpointing.
//! - [`DeEngine<U>`](crate::de::DeEngine) — Differential Evolution engine with 5 mutation strategies
//! (rand/1, best/1, current-to-best/1, best/2, rand/2) and two adaptive variants (JADE, L-SHADE)
//! with binomial and exponential crossover. Best for continuous, real-valued optimization.
//! - [`ScatterEngine<U>`](crate::scatter::ScatterEngine) — Scatter Search metaheuristic that maintains
//! a diverse reference set, combines solutions via linear combination, and applies improvement
//! methods. Strong on combinatorial optimization with rugged landscapes.
//! - [`CellularEngine<U>`](crate::cellular::CellularEngine) — Cellular GA that arranges the population
//! on a 2D toroidal grid. Supports 4 neighborhood topologies (Von Neumann, Moore, Compact R2,
//! Linear) and synchronous or asynchronous update. Preserves spatial diversity.
//! - [`AlpsEngine<U>`](crate::alps::AlpsEngine) — Age-Layered Population Structure that organizes
//! individuals into age layers with 3 age schemes (Linear, Fibonacci, Polynomial). Enables
//! cross-layer mating and periodic injection for sustained exploration.
//! - [`IslandGa<U>`](crate::island::IslandGa) — Island model GA that evolves multiple sub-populations
//! in parallel with configurable migration topology (ring, fully connected, random), frequency,
//! and migrant selection. Works with any chromosome type and can wrap [`Nsga2Ga`](crate::nsga2::Nsga2Ga).
//!
//! ### Multi-Objective Engines
//!
//! - [`Nsga2Ga<U>`](crate::nsga2::Nsga2Ga) — NSGA-II: fast non-dominated sorting with crowding
//! distance for diversity. Efficient O(MN²) ranking. Standard choice for 2-objective problems.
//! - [`Nsga3Ga<U>`](crate::nsga3::Nsga3Ga) — NSGA-III: reference-point based selection using
//! Das-Dennis weights and ASF-based normalization. Scales to 3+ objectives where crowding
//! distance loses effectiveness. Supports 2 to 15+ objectives.
//! - [`MoeaDGa<U>`](crate::moead::MoeaDGa) — MOEA/D: decomposes multi-objective problems into
//! scalar sub-problems using Tchebycheff, PBI, or weighted sum aggregation. Neighbor-based
//! mating preserves convergence along the Pareto front.
//! - [`Spea2Ga<U>`](crate::spea2::Spea2Ga) — SPEA2: strength-Pareto approach with an external
//! archive, k-nearest neighbor density estimation, and truncation mechanism. Effective on
//! problems with irregular Pareto fronts.
//! - [`SmsEmoaGa<U>`](crate::sms_emoa::SmsEmoaGa) — SMS-EMOA: steady-state (mu+1) MOEA that
//! selects the individual with the smallest hypervolume contribution for removal. Precise
//! hypervolume-based convergence.
//! - [`IbeaGa<U>`](crate::ibea::IbeaGa) — IBEA: indicator-based MOEA using the I_epsilon+
//! binary indicator with exponential fitness scaling. No diversity preservation mechanism
//! required — the indicator implicitly balances convergence and diversity.
//!
//! ## Feature Flags
//!
//! | Flag | Description | Default |
//! |------|-------------|---------|
//! | `serde` | Serialization/deserialization for checkpoint save/load via `serde_json` | Off |
//! | `benchmarks` | Standard benchmark functions (Sphere, Rastrigin, Rosenbrock, ZDT, DTLZ) and multi-objective quality indicators | Off |
//! | `visualization` | PNG/SVG fitness plots, diversity charts, and histogram generation via `plotters` | Off |
//! | `observer-tracing` | Structured `tracing`-crate spans and events in the observer system | Off |
//! | `observer-metrics` | Per-generation metrics (gauges, counters, histograms) via the `metrics` facade | Off |
//!
//! ## Key Concepts
//!
//! ### Genotypes & Chromosomes
//!
//! The library separates the concept of a **gene** (a single atomic unit of information) from a
//! **chromosome** (a sequence of genes that represents a candidate solution). Genes implement
//! [`GeneT`](crate::traits::GeneT) and chromosomes implement
//! [`ChromosomeT`](crate::traits::ChromosomeT).
//!
//! Built-in gene types: [`Binary`](crate::genotypes::Binary) (boolean), [`Range<T>`](crate::genotypes::Range)
//! (bounded real/integer values), [`List<T>`](crate::genotypes::List) (finite symbolic alphabets).
//!
//! Built-in chromosome types: [`chromosomes::Binary`],
//! [`chromosomes::Range<T>`], [`chromosomes::ListChromosome<T>`].
//!
//! Custom chromosomes can be created by implementing [`ChromosomeT`](crate::traits::ChromosomeT).
//!
//! ### Configuration
//!
//! All engines use a fluent builder pattern via the
//! [`ConfigurationT`](crate::traits::ConfigurationT) trait. Each engine has its own configuration
//! struct ([`GaConfiguration`](crate::configuration::GaConfiguration),
//! [`DeConfiguration`](crate::de::DeConfiguration), etc.) with builder methods for limits, operators,
//! stopping criteria, and engine-specific parameters.
//!
//! See the [`configuration`] module for all shared configuration types.
//!
//! ### Operators
//!
//! Five operator categories are available, dispatched via enums with factory functions:
//!
//! - **Selection** ([`Selection`](crate::operations::Selection)): Random, RouletteWheel, SUS, Tournament,
//! Rank, Boltzmann, Truncation, Clearing
//! - **Crossover** ([`Crossover`](crate::operations::Crossover)): Cycle, MultiPoint, Uniform, SinglePoint,
//! Order (OX), PMX, SBX, BlendAlpha (BLX-alpha), Arithmetic, Edge Recombination, Clone, Rejuvenate
//! - **Mutation** ([`Mutation`](crate::operations::Mutation)): Swap, Inversion, Scramble, Value, BitFlip,
//! Creep, Gaussian, Polynomial, NonUniform, Insertion, Cauchy, LevyFlight, Uniform, ListValue
//! - **Survivor** ([`Survivor`](crate::operations::Survivor)): Fitness, Age, MuPlusLambda, MuCommaLambda,
//! DeterministicCrowding
//! - **Extension** ([`Extension`](crate::operations::Extension)): Noop, MassExtinction, MassGenesis,
//! MassDegeneration, MassDeduplication
//!
//! See the [`operations`] module for full documentation of each operator.
//!
//! ### Observer System
//!
//! The [`GaObserver<U>`] trait provides 11 lifecycle hooks that fire during engine
//! execution (selection, crossover, mutation, fitness evaluation, survivor selection, new best,
//! stagnation, extension trigger, generation end, run start/end). Built-in observers include
//! [`LogObserver`], [`CompositeObserver<U>`],
//! `MetricsObserver` (feature `observer-metrics`),
//! `TracingObserver` (feature `observer-tracing`),
//! and [`NoopObserver`]. Engine-specific sub-traits exist for
//! [`IslandGaObserver`], [`Nsga2Observer`],
//! [`Nsga3Observer`], [`Spea2Observer`],
//! [`MoeaDObserver`], [`SmsEmoaObserver`],
//! and [`IbeaObserver`].
//!
//! Zero overhead when no observer is attached (stored as `Option<Arc<dyn GaObserver<U>>>`).
//!
//! ### Constraints
//!
//! The [`ConstraintHandling`] system provides constraint enforcement
//! through penalty strategies ([`PenaltyStrategy`] — Static, Dynamic,
//! Adaptive, Death) and feasibility rules. See the [`constraints`] module.
//!
//! ### Hall of Fame
//!
//! The [`HallOfFame<U>`] archive stores elite solutions across generations,
//! with configurable capacity and optional diversity enforcement via
//! [`DistanceMetric`]. See the [`hall_of_fame`] module.
//!
//! ### Adaptive Operator Selection (AOS)
//!
//! The [`AosStrategy`] and [`AosState`] types implement
//! credit-assignment based operator selection. See the [`aos`] module.
//!
//! ### Initializers
//!
//! Population initialization functions in the [`initializers`] module:
//! `binary_random_initialization`, `range_random_initialization`,
//! `list_random_initialization`, `list_random_initialization_without_repetitions`,
//! `generic_random_initialization`, `generic_random_initialization_without_repetitions`.
//!
//! ## When to Use Which Engine
//!
//! | If your problem is... | Use this engine... | Because... |
//! |-----------------------|--------------------|------------|
//! | General single-objective, any variable type | [`Ga<U>`](crate::ga::Ga) | Full operator support, adaptive mode, niching, extensions |
//! | Continuous, real-valued, single-objective | [`DeEngine<U>`](crate::de::DeEngine) | 5 strategies + JADE/L-SHADE, fast convergence |
//! | Combinatorial with rugged landscape | [`ScatterEngine<U>`](crate::scatter::ScatterEngine) | Reference set diversification avoids local optima |
//! | Single-objective, needs spatial diversity | [`CellularEngine<U>`](crate::cellular::CellularEngine) | Grid-based neighborhoods preserve niche exploration |
//! | Single-objective, sustained exploration needed | [`AlpsEngine<U>`](crate::alps::AlpsEngine) | Age layers prevent premature convergence |
//! | Parallel / distributed single-objective | [`IslandGa<U>`](crate::island::IslandGa) | Independent sub-populations with periodic migration |
//! | Exactly 2 objectives | [`Nsga2Ga<U>`](crate::nsga2::Nsga2Ga) | Fast O(MN²) ranking, well-established baseline |
//! | 3+ objectives (many-objective) | [`Nsga3Ga<U>`](crate::nsga3::Nsga3Ga) | Reference points scale beyond crowding distance |
//! | 2+ objectives, continuous | [`MoeaDGa<U>`](crate::moead::MoeaDGa) | Decomposition is efficient and interpretable |
//! | 2+ objectives, irregular front | [`Spea2Ga<U>`](crate::spea2::Spea2Ga) | Archive + density estimation handles complex fronts |
//! | 2+ objectives, precise convergence | [`SmsEmoaGa<U>`](crate::sms_emoa::SmsEmoaGa) | Hypervolume contribution directly measures quality |
//! | 2+ objectives, flexible indicator | [`IbeaGa<U>`](crate::ibea::IbeaGa) | Indicator-based selection needs no extra diversity mechanism |
//!
//! ## Examples
//!
//! The `examples/` directory contains 19 runnable examples covering all engines and features.
//! Run any example with `cargo run --example <name> --features <features>`.
//!
//! See the [README](https://github.com/leimbernon/rust_genetic_algorithms#readme) for a full
//! examples catalog and the [docs/ directory](https://github.com/leimbernon/rust_genetic_algorithms/tree/main/docs)
//! for per-engine guides with complete usage examples.
//!
//! ## Further Reading
//!
//! - [README](https://github.com/leimbernon/rust_genetic_algorithms#readme) — Installation, full examples catalog, engines overview
//! - [docs/ directory](https://github.com/leimbernon/rust_genetic_algorithms/tree/main/docs) — Per-engine algorithm guides and framework extension docs
//! - [docs.rs/genetic_algorithms](https://docs.rs/genetic_algorithms/latest/genetic_algorithms) — Full API reference with module-level documentation
//! - [crates.io](https://crates.io/crates/genetic_algorithms) — Package registry and version history
extern crate core;
pub use TerminationCause;
pub use AllObserver;
pub use CompositeObserver;
pub use ExtensionEvent;
pub use GaObserver;
pub use IslandGaObserver;
pub use LogObserver;
pub use MoeaDObserver;
pub use IbeaObserver;
pub use SmsEmoaObserver;
pub use Spea2Observer;
pub use MetricsObserver;
pub use NoopObserver;
pub use Nsga2Observer;
pub use Nsga3Observer;
pub use TracingObserver;
pub use ConstraintHandling;
pub use PenaltyStrategy;
pub use ;
pub use ;