pub struct BinaryGeneticAlgorithm<B: Backend> { /* private fields */ }Expand description
Binary-coded canonical Genetic Algorithm.
§Example
use burn::backend::Flex;
use rlevo_evolution::algorithms::ga_binary::{BinaryGaConfig, BinaryGeneticAlgorithm};
let strategy = BinaryGeneticAlgorithm::<Flex>::new();
let params = BinaryGaConfig::default_for(32, 16);
let _ = (strategy, params);Implementations§
Trait Implementations§
Source§impl<B: Clone + Backend> Clone for BinaryGeneticAlgorithm<B>
impl<B: Clone + Backend> Clone for BinaryGeneticAlgorithm<B>
Source§fn clone(&self) -> BinaryGeneticAlgorithm<B>
fn clone(&self) -> BinaryGeneticAlgorithm<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 BinaryGeneticAlgorithm<B>
Source§impl<B: Default + Backend> Default for BinaryGeneticAlgorithm<B>
impl<B: Default + Backend> Default for BinaryGeneticAlgorithm<B>
Source§fn default() -> BinaryGeneticAlgorithm<B>
fn default() -> BinaryGeneticAlgorithm<B>
Source§impl<B: Backend> Strategy<B> for BinaryGeneticAlgorithm<B>
impl<B: Backend> Strategy<B> for BinaryGeneticAlgorithm<B>
Source§fn init(
&self,
params: &BinaryGaConfig,
rng: &mut dyn Rng,
device: &<B as BackendTypes>::Device,
) -> BinaryGaState<B>
fn init( &self, params: &BinaryGaConfig, rng: &mut dyn Rng, device: &<B as BackendTypes>::Device, ) -> BinaryGaState<B>
Build the initial state.
Samples an (pop_size, D) binary population uniformly at random
(each gene independently Bernoulli(0.5)) 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: &BinaryGaConfig,
state: &BinaryGaState<B>,
rng: &mut dyn Rng,
device: &<B as BackendTypes>::Device,
) -> (Tensor<B, 2, Int>, BinaryGaState<B>)
fn ask( &self, params: &BinaryGaConfig, state: &BinaryGaState<B>, rng: &mut dyn Rng, device: &<B as BackendTypes>::Device, ) -> (Tensor<B, 2, Int>, BinaryGaState<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 one full selection → crossover →
mutation pipeline, 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— per-gene coin flip (probabilitycrossover_p);SeedPurpose::Mutation— per-gene bit-flip (probabilitymutation_rate).
§Panics
Propagates the programming-error panics of the delegated host operators
once state.fitness is non-empty:
tournament_indices_host
panics if the fitness slice is empty or tournament_size == 0, and the
crossover / mutation operators assume shape-consistent inputs.
validate() rules
these out for a well-formed config, but BinaryGaState / BinaryGaConfig
fields are public and can be mutated into an invalid state after
validation.
Source§fn tell(
&self,
params: &BinaryGaConfig,
offspring: Tensor<B, 2, Int>,
fitness: Tensor<B, 1>,
state: BinaryGaState<B>,
_rng: &mut dyn Rng,
) -> (BinaryGaState<B>, StrategyMetrics)
fn tell( &self, params: &BinaryGaConfig, offspring: Tensor<B, 2, Int>, fitness: Tensor<B, 1>, state: BinaryGaState<B>, _rng: &mut dyn Rng, ) -> (BinaryGaState<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 method performs elitist replacement: the
elitism_k highest-fitness parents survive directly, and the remaining
pop_size − elitism_k slots are filled with the best offspring.
Both selections use crate::ops::selection::truncation_indices_host.
fitness must have shape (pop_size,) in the canonical maximise
convention — higher is better. The harness canonicalises a Minimize
objective (negation) before this call, so the strategy stays
sense-unaware per ADR 0023.
§Panics
Propagates the programming-error panics of the delegated host path on the
elitist-replacement branch:
truncation_indices_host
and the bare state.fitness[i] indexing assume a pop_size-consistent
fitness buffer. validate() rules this out for a well-formed config, but
BinaryGaState / BinaryGaConfig fields are public and can be mutated
into an inconsistent state after validation. Also panics if fitness
cannot be read back as f32.
Source§fn best(&self, state: &BinaryGaState<B>) -> Option<(Tensor<B, 2, Int>, f32)>
fn best(&self, state: &BinaryGaState<B>) -> Option<(Tensor<B, 2, Int>, f32)>
Return the best-so-far genome and its fitness.
Returns None before the first tell call.
The fitness value uses the canonical maximise convention (higher is better).
Source§type Params = BinaryGaConfig
type Params = BinaryGaConfig
Source§type State = BinaryGaState<B>
type State = BinaryGaState<B>
Auto Trait Implementations§
impl<B> Freeze for BinaryGeneticAlgorithm<B>
impl<B> RefUnwindSafe for BinaryGeneticAlgorithm<B>
impl<B> Send for BinaryGeneticAlgorithm<B>
impl<B> Sync for BinaryGeneticAlgorithm<B>
impl<B> Unpin for BinaryGeneticAlgorithm<B>
impl<B> UnsafeUnpin for BinaryGeneticAlgorithm<B>
impl<B> UnwindSafe for BinaryGeneticAlgorithm<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