pub trait EvolutionaryGenome:
Clone
+ Send
+ Sync
+ Serialize
+ DeserializeOwned
+ 'static {
type Allele: Clone + Send;
type Phenotype;
// Required methods
fn decode(&self) -> Self::Phenotype;
fn dimension(&self) -> usize;
fn generate<R: Rng>(rng: &mut R, bounds: &MultiBounds) -> Self;
fn distance(&self, other: &Self) -> f64;
fn try_distance(&self, other: &Self) -> Result<f64, GenomeError>;
// Provided methods
fn as_slice(&self) -> Option<&[Self::Allele]> { ... }
fn as_mut_slice(&mut self) -> Option<&mut [Self::Allele]> { ... }
}Expand description
Core genome abstraction for evolutionary algorithms.
This trait defines the interface for evolvable solution representations. Genomes must be cloneable, serializable, and thread-safe.
Genomes that additionally support the fugue trace encoding (for the
PPL-native inference layer) implement the
TraceGenome extension trait,
available behind the ppl feature.
Required Associated Types§
Required Methods§
Sourcefn generate<R: Rng>(rng: &mut R, bounds: &MultiBounds) -> Self
fn generate<R: Rng>(rng: &mut R, bounds: &MultiBounds) -> Self
Generate a random genome within the given bounds.
§Interpretation of bounds
MultiBounds semantically describes a set of per-dimension numeric
[min, max] intervals, but only the real-valued genome types
(RealVector,
DynamicRealVector)
actually consult the min/max fields. For every other built-in genome
type, only bounds.dimension() (the count of intervals) is
consulted — it is repurposed as a stand-in for the genome’s structural
size, and the min/max values are ignored:
BitString:dimension()is the number of bits. PreferBitString::generate_with_len.Permutation:dimension()is the permutation length. PreferPermutation::generate_with_len.TreeGenome:dimension()is remapped to a maximum tree depth. PreferTreeGenome::generate_with_depth.
When you are not generating real-valued genomes, use the per-type honest
constructors listed above; they make the size/depth parameter explicit
instead of overloading MultiBounds.
Sourcefn distance(&self, other: &Self) -> f64
fn distance(&self, other: &Self) -> f64
Distance metric between two genomes.
This is a required method: there is deliberately no default
implementation, because a silent fallback (e.g. always 0.0) would make
every pair of genomes look identical and silently break diversity-driven
mechanisms (niching, crowding, speciation).
§Panics
Implementations panic when the two genomes are structurally incompatible
(for fixed-structure genomes, this means different lengths / dimensions).
A structural mismatch is an invariant violation by the caller rather than
a recoverable condition. Use try_distance when a
fallible comparison is required.
(Genome types whose comparison is meaningfully defined across differing
structures — e.g. DynamicRealVector,
which adds a length penalty, and TreeGenome,
which compares size/depth — never panic.)
Sourcefn try_distance(&self, other: &Self) -> Result<f64, GenomeError>
fn try_distance(&self, other: &Self) -> Result<f64, GenomeError>
Fallible distance metric.
Returns Err(GenomeError::DimensionMismatch { .. }) (or another
GenomeError) when the two genomes are structurally incompatible,
instead of panicking as distance does. For genome
types whose distance is defined across differing structures this always
returns Ok.
Provided Methods§
Sourcefn as_slice(&self) -> Option<&[Self::Allele]>
fn as_slice(&self) -> Option<&[Self::Allele]>
Get the genome’s genes as a slice (for numeric genomes)
Sourcefn as_mut_slice(&mut self) -> Option<&mut [Self::Allele]>
fn as_mut_slice(&mut self) -> Option<&mut [Self::Allele]>
Get the genome’s genes as a mutable slice (for numeric genomes)
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety".