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.

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 raw (minimization-convention) scalar — the smallest value seen across all completed generations.

Dyn Compatibility§

This trait is dyn compatible.

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

Implementors§

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 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,