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::INFINITY; 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).
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 lowest-cost 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,) with values in the
minimization (cost) convention — lower is better.
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 minimization convention (lower 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