pub struct GeneticAlgorithm<B: Backend> { /* private fields */ }Expand description
Real-valued canonical Genetic Algorithm.
§Example
use burn::backend::Flex;
use rlevo_core::bounds::Bounds;
use rlevo_core::rate::NonNegativeRate;
use rlevo_evolution::algorithms::ga::{
GaConfig, GaCrossover, GaReplacement, GaSelection, GeneticAlgorithm,
};
let strategy = GeneticAlgorithm::<Flex>::new();
let params = GaConfig {
pop_size: 64,
genome_dim: 10,
bounds: Bounds::new(-5.12, 5.12),
mutation_sigma: NonNegativeRate::new(0.3),
selection: GaSelection::Tournament { size: 2 },
crossover: GaCrossover::BlxAlpha { alpha: NonNegativeRate::new(0.5) },
replacement: GaReplacement::Elitist { elitism_k: 2 },
};
let _ = (strategy, params);Implementations§
Trait Implementations§
Source§impl<B: Clone + Backend> Clone for GeneticAlgorithm<B>
impl<B: Clone + Backend> Clone for GeneticAlgorithm<B>
Source§fn clone(&self) -> GeneticAlgorithm<B>
fn clone(&self) -> GeneticAlgorithm<B>
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreimpl<B: Copy + Backend> Copy for GeneticAlgorithm<B>
Source§impl<B: Default + Backend> Default for GeneticAlgorithm<B>
impl<B: Default + Backend> Default for GeneticAlgorithm<B>
Source§fn default() -> GeneticAlgorithm<B>
fn default() -> GeneticAlgorithm<B>
Source§impl<B: Backend> Strategy<B> for GeneticAlgorithm<B>
impl<B: Backend> Strategy<B> for GeneticAlgorithm<B>
Source§fn init(
&self,
params: &GaConfig,
rng: &mut dyn Rng,
device: &<B as BackendTypes>::Device,
) -> GaState<B>
fn init( &self, params: &GaConfig, rng: &mut dyn Rng, device: &<B as BackendTypes>::Device, ) -> GaState<B>
Build the initial state.
Samples an (pop_size, genome_dim) real-valued population uniformly
within params.bounds using a host RNG derived from rng. Sets
fitness to empty and best_fitness to f32::NEG_INFINITY (the
worst value under the maximise convention); the first
tell call populates both.
Source§fn ask(
&self,
params: &GaConfig,
state: &GaState<B>,
rng: &mut dyn Rng,
device: &<B as BackendTypes>::Device,
) -> (Tensor<B, 2>, GaState<B>)
fn ask( &self, params: &GaConfig, state: &GaState<B>, rng: &mut dyn Rng, device: &<B as BackendTypes>::Device, ) -> (Tensor<B, 2>, GaState<B>)
Propose the next offspring population.
On the very first call (before any tell), state.fitness
is empty — the harness has not evaluated the seed population yet. In
that case the unchanged seed population is returned so the harness can
evaluate and pass it back to tell.
On subsequent calls the method runs selection → crossover → mutation,
deriving three independent host sub-streams from rng via
crate::rng::seed_stream:
SeedPurpose::Selection— two independent tournament draws (parents A and parents B);SeedPurpose::Crossover— BLX-α or uniform crossover;SeedPurpose::Mutation— isotropic Gaussian perturbation.
After mutation, offspring are clamped to params.bounds.
Source§fn tell(
&self,
params: &GaConfig,
population: Tensor<B, 2>,
fitness: Tensor<B, 1>,
state: GaState<B>,
_rng: &mut dyn Rng,
) -> (GaState<B>, StrategyMetrics)
fn tell( &self, params: &GaConfig, population: Tensor<B, 2>, fitness: Tensor<B, 1>, state: GaState<B>, _rng: &mut dyn Rng, ) -> (GaState<B>, StrategyMetrics)
Consume offspring fitness and produce the next generation’s state.
The first call (when state.fitness is empty) caches the seed
population’s fitness and increments the generation counter; no
replacement is performed.
On subsequent calls the replacement policy is applied:
GaReplacement::Generational— offspring completely replace parents.GaReplacement::Elitist— theelitism_khighest-fitness parents survive; the remainder come from offspring.
fitness must have shape (pop_size,) in the canonical maximise
convention — higher is better.
Auto Trait Implementations§
impl<B> Freeze for GeneticAlgorithm<B>
impl<B> RefUnwindSafe for GeneticAlgorithm<B>
impl<B> Send for GeneticAlgorithm<B>
impl<B> Sync for GeneticAlgorithm<B>
impl<B> Unpin for GeneticAlgorithm<B>
impl<B> UnsafeUnpin for GeneticAlgorithm<B>
impl<B> UnwindSafe for GeneticAlgorithm<B>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<C> CloneExpand for Cwhere
C: Clone,
impl<C> CloneExpand for Cwhere
C: Clone,
fn __expand_clone_method(&self, _scope: &mut Scope) -> C
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more