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
//! RealValued marker trait for compile-time enforcement on multi-parent crossover.
//!
//! Implementors of this trait gain access to `factory_multi_parent` dispatch for
//! `Crossover::Undx`, `Crossover::Spx`, and `Crossover::Pcx`.
//!
//! Binary and permutation chromosomes must **not** implement `RealValued` — doing so
//! would bypass the compile-time guard that restricts multi-parent crossover to
//! real-valued search spaces.
use crateLinearChromosome;
/// Marker trait for real-valued (continuous or integer-range) chromosomes.
///
/// Implementing `RealValued` signals that a chromosome's genes occupy a
/// continuous or bounded numeric space, making it eligible for multi-parent
/// crossover operators:
///
/// - [`Crossover::Undx`](crate::operations::Crossover::Undx) — Unimodal Normal Distribution Crossover
/// - [`Crossover::Spx`](crate::operations::Crossover::Spx) — Simplex Crossover
/// - [`Crossover::Pcx`](crate::operations::Crossover::Pcx) — Parent-Centric Crossover
///
/// # Who should implement this trait?
///
/// Implement `RealValued` on any chromosome whose genes carry numeric values
/// that support arithmetic operations (e.g., addition, subtraction, scalar
/// multiplication). The built-in [`Range<T>`](crate::chromosomes::Range) and
/// [`MultiRangeChromosome<T>`](crate::chromosomes::MultiRangeChromosome) types
/// implement this trait.
///
/// Binary chromosomes (`Binary`) and permutation chromosomes (`Unique<T>`)
/// must **not** implement `RealValued`.
///
/// # Examples
///
/// ```rust,no_run
/// use genetic_algorithms::traits::{LinearChromosome, RealValued};
/// # use genetic_algorithms::traits::{ChromosomeT, GeneT};
/// # use std::borrow::Cow;
///
/// // Custom real-valued chromosome implementing RealValued
/// # #[derive(Clone, Default)]
/// # struct MyGene;
/// # impl GeneT for MyGene {
/// # fn id(&self) -> i32 { 0 }
/// # fn set_id(&mut self, _: i32) -> &mut Self { self }
/// # }
/// # #[derive(Clone, Default)]
/// # struct MyChromosome;
/// # impl ChromosomeT for MyChromosome {
/// # type Gene = MyGene;
/// # fn fitness(&self) -> f64 { 0.0 }
/// # fn set_fitness(&mut self, _: f64) -> &mut Self { self }
/// # fn age(&self) -> usize { 0 }
/// # fn set_age(&mut self, _: usize) -> &mut Self { self }
/// # fn calculate_fitness(&mut self) {}
/// # }
/// # impl LinearChromosome for MyChromosome {
/// # fn dna(&self) -> &[MyGene] { &[] }
/// # fn dna_mut(&mut self) -> &mut [MyGene] { &mut [] }
/// # fn set_dna<'a>(&mut self, _: Cow<'a, [MyGene]>) -> &mut Self { self }
/// # fn set_fitness_fn<F>(&mut self, _: F) -> &mut Self where F: Fn(&[MyGene]) -> f64 + Send + Sync + 'static { self }
/// # }
/// impl RealValued for MyChromosome {}
/// ```