Skip to main content

Strategy

Trait Strategy 

Source
pub trait Strategy<B: Backend>: Send + Sync {
    type Params: Clone + Debug + Send + Sync;
    type State: Clone + Debug + Send;
    type Genome: Clone + Send;

    // Required methods
    fn init(
        &self,
        params: &Self::Params,
        rng: &mut dyn Rng,
        device: &<B as BackendTypes>::Device,
    ) -> Self::State;
    fn ask(
        &self,
        params: &Self::Params,
        state: &Self::State,
        rng: &mut dyn Rng,
        device: &<B as BackendTypes>::Device,
    ) -> (Self::Genome, Self::State);
    fn tell(
        &self,
        params: &Self::Params,
        population: Self::Genome,
        fitness: Tensor<B, 1>,
        state: Self::State,
        rng: &mut dyn Rng,
    ) -> (Self::State, StrategyMetrics);
    fn best(&self, state: &Self::State) -> Option<(Self::Genome, f32)>;
}
Expand description

Central evolutionary-strategy abstraction.

The trait is intentionally pure — ask and tell return a new State rather than mutating through &mut self. That keeps strategies free of interior mutability (so many instances can run in parallel without locks) and makes Clone-based checkpointing straightforward.

§Example

The example below uses GeneticAlgorithm as a concrete strategy and drives one ask/tell cycle by hand. Concrete strategies expose their state fields directly; generic code over S: Strategy<B> must access state only through Strategy::best and the tuple returns of ask/tell.

use burn::backend::Flex;
use burn::tensor::TensorData;
use rlevo_evolution::Strategy;
use rlevo_evolution::algorithms::ga::{GaConfig, GeneticAlgorithm};
use rand::{rngs::StdRng, SeedableRng};

let device = Default::default();
let strategy = GeneticAlgorithm::<Flex>::new();
let params = GaConfig::default_for(64, 10);
let mut rng = StdRng::seed_from_u64(0);
let state = strategy.init(&params, &mut rng, &device);
// state.population is a GaState field; dims() is (pop_size, genome_dim).
assert_eq!(state.population.dims(), [64, 10]);

§Type Parameters

  • B: Burn backend.

§Associated Types

  • Params: Static configuration for a run (population size, σ, F, CR, …). Adaptive algorithms mutate their adaptive quantities inside State, not Params.
  • State: Generation-to-generation state (current population, σ, best-so-far, RNG-free sub-statistics). Must be clonable so the harness can snapshot before a risky step if needed.
  • Genome: Genome container produced by ask and consumed by tell. Typically a Tensor<B, 2> for real-valued strategies or a Tensor<B, 2, Int> for binary/integer kinds.

Required Associated Types§

Source

type Params: Clone + Debug + Send + Sync

Static parameters for a run.

Source

type State: Clone + Debug + Send

Generation-to-generation state.

Source

type Genome: Clone + Send

Genome container produced by ask.

Required Methods§

Source

fn init( &self, params: &Self::Params, rng: &mut dyn Rng, device: &<B as BackendTypes>::Device, ) -> Self::State

Build the initial state.

Samples the initial population, primes adaptive quantities, and sets the generation counter to zero.

Source

fn ask( &self, params: &Self::Params, state: &Self::State, rng: &mut dyn Rng, device: &<B as BackendTypes>::Device, ) -> (Self::Genome, Self::State)

Propose the next population.

Takes the current state and returns the genome to evaluate together with an updated state. The returned state typically carries pre-computed bookkeeping (e.g. the parent indices a tournament-based GA sampled) so tell can reuse them without re-sampling.

Source

fn tell( &self, params: &Self::Params, population: Self::Genome, fitness: Tensor<B, 1>, state: Self::State, rng: &mut dyn Rng, ) -> (Self::State, StrategyMetrics)

Consume fitness values and produce the next state.

fitness has shape (pop_size,) on the same device as the population. Strategies pull it to host only if they need to — e.g. for tournament index lookups.

§Invariants

When driven by EvolutionaryHarness, the fitness tensor is canonical (maximise) and sanitized (ADR 0034): every element is finite or f32::NEG_INFINITY — no NaN, no +∞. A tell impl may therefore build leaders / personal-best / global-best directly from it without a finite check. Callers that invoke tell directly, bypassing the harness, do not get this guarantee and must apply sanitize_fitness at every ordering/aggregation site (rules.md §3).

Source

fn best(&self, state: &Self::State) -> Option<(Self::Genome, f32)>

Best-so-far accessor.

Returns None before the first tell call. The tuple is (genome, fitness) where fitness is the canonical (maximise-convention) scalar — the largest value seen across all completed generations. The harness maps it back to the objective’s declared sense before surfacing it to callers.

Dyn Compatibility§

This trait is dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§

Source§

impl<B, S, L, F> Strategy<B> for MemeticWrapper<B, S, L, F>
where B: Backend, S: Strategy<B, Genome = Tensor<B, 2>>, L: LocalSearch<B>, F: BatchFitnessFn<B, Tensor<B, 2>>,

Source§

impl<B, S, M> Strategy<B> for WeightOnly<B, S, M>
where B: Backend, S: Strategy<B, Genome = Tensor<B, 2>>, M: Module<B> + Sync,

Source§

type Params = <S as Strategy<B>>::Params

Source§

type State = <S as Strategy<B>>::State

Source§

type Genome = Tensor<B, 2>

Source§

impl<B: Backend, F: FunctionSet> Strategy<B> for GepStrategy<B, F>
where B::Device: Clone,

Source§

impl<B: Backend, M: ProbabilityModel<B>> Strategy<B> for EdaStrategy<B, M>

Source§

impl<B: Backend> Strategy<B> for AntColonyPermutation<B>

Source§

impl<B: Backend> Strategy<B> for AntColonyReal<B>
where B::Device: Clone,

Source§

impl<B: Backend> Strategy<B> for ArtificialBeeColony<B>
where B::Device: Clone,

Source§

impl<B: Backend> Strategy<B> for BatAlgorithm<B>
where B::Device: Clone,

Source§

impl<B: Backend> Strategy<B> for BinaryGeneticAlgorithm<B>
where B::Device: Clone,

Source§

impl<B: Backend> Strategy<B> for CartesianGeneticProgramming<B>
where B::Device: Clone,

Source§

impl<B: Backend> Strategy<B> for CmaEs<B>
where B::Device: Clone,

Source§

impl<B: Backend> Strategy<B> for CmsaEs<B>
where B::Device: Clone,

Source§

impl<B: Backend> Strategy<B> for CuckooSearch<B>
where B::Device: Clone,

Source§

impl<B: Backend> Strategy<B> for DifferentialEvolution<B>
where B::Device: Clone,

Source§

impl<B: Backend> Strategy<B> for EvolutionStrategy<B>
where B::Device: Clone,

Source§

impl<B: Backend> Strategy<B> for EvolutionaryProgramming<B>
where B::Device: Clone,

Source§

impl<B: Backend> Strategy<B> for FireflyAlgorithm<B>
where B::Device: Clone,

Source§

impl<B: Backend> Strategy<B> for GeneticAlgorithm<B>
where B::Device: Clone,

Source§

impl<B: Backend> Strategy<B> for GreyWolfOptimizer<B>
where B::Device: Clone,

Source§

impl<B: Backend> Strategy<B> for ParticleSwarm<B>
where B::Device: Clone,

Source§

impl<B: Backend> Strategy<B> for SalpSwarm<B>
where B::Device: Clone,

Source§

impl<B: Backend> Strategy<B> for WhaleOptimization<B>
where B::Device: Clone,