genetic_algorithm::strategy

Trait StrategyReporter

Source
pub trait StrategyReporter:
    Clone
    + Send
    + Sync {
    type Genotype: Genotype;

    // Provided methods
    fn flush(&mut self, _output: &mut Vec<u8>) { ... }
    fn on_enter<S: StrategyState<Self::Genotype>, C: StrategyConfig>(
        &mut self,
        _genotype: &Self::Genotype,
        _state: &S,
        _config: &C,
    ) { ... }
    fn on_exit<S: StrategyState<Self::Genotype>, C: StrategyConfig>(
        &mut self,
        _genotype: &Self::Genotype,
        _state: &S,
        _config: &C,
    ) { ... }
    fn on_start<S: StrategyState<Self::Genotype>, C: StrategyConfig>(
        &mut self,
        _genotype: &Self::Genotype,
        _state: &S,
        _config: &C,
    ) { ... }
    fn on_finish<S: StrategyState<Self::Genotype>, C: StrategyConfig>(
        &mut self,
        _genotype: &Self::Genotype,
        _state: &S,
        _config: &C,
    ) { ... }
    fn on_new_generation<S: StrategyState<Self::Genotype>, C: StrategyConfig>(
        &mut self,
        _genotype: &Self::Genotype,
        _state: &S,
        _config: &C,
    ) { ... }
    fn on_new_best_chromosome<S: StrategyState<Self::Genotype>, C: StrategyConfig>(
        &mut self,
        _genotype: &Self::Genotype,
        _state: &S,
        _config: &C,
    ) { ... }
    fn on_new_best_chromosome_equal_fitness<S: StrategyState<Self::Genotype>, C: StrategyConfig>(
        &mut self,
        _genotype: &Self::Genotype,
        _state: &S,
        _config: &C,
    ) { ... }
    fn on_extension_event<S: StrategyState<Self::Genotype>, C: StrategyConfig>(
        &mut self,
        _event: ExtensionEvent,
        _genotype: &Self::Genotype,
        _state: &S,
        _config: &C,
    ) { ... }
    fn on_mutate_event<S: StrategyState<Self::Genotype>, C: StrategyConfig>(
        &mut self,
        _event: MutateEvent,
        _genotype: &Self::Genotype,
        _state: &S,
        _config: &C,
    ) { ... }
}
Expand description

Reporter with event hooks for all Strategies. You are encouraged to roll your own implementation, depending on your needs.

Event hooks in lifecycle:

  • on_enter (before setup)
  • on_start (of run loop)
  • in run loop:
    • on_new_generation
    • on_new_best_chromosome
    • on_new_best_chromosome_equal_fitness
    • on_extension_event
    • on_mutate_event
  • on_finish (of run loop)
  • on_exit (after cleanup)

It has an associated type Genotype, just like Fitness, so you can implement reporting with access to your domain’s specific Genotype and Chromosome etc..

For reference, take a look at the provided strategy independent StrategyReporterSimple implementation, or strategy specific EvolveReporterSimple, HillClimbReporterSimple and PermutateReporterSimple implementations.

§Example:

use genetic_algorithm::strategy::evolve::prelude::*;

#[derive(Clone)]
pub struct CustomReporter { pub period: usize }
impl StrategyReporter for CustomReporter {
    type Genotype = BinaryGenotype;

    fn on_new_generation<S: StrategyState<Self::Genotype>, C: StrategyConfig>(
        &mut self,
        _genotype: &Self::Genotype,
        state: &S,
        _config: &C,
    ) {
        if state.current_generation() % self.period == 0 {
            println!(
                "periodic - current_generation: {}, stale_generations: {}, best_generation: {}, scale_index: {:?}",
                state.current_generation(),
                state.stale_generations(),
                state.best_generation(),
                state.current_scale_index(),
            );
        }
    }

    fn on_new_best_chromosome<S: StrategyState<Self::Genotype>, C: StrategyConfig>(
        &mut self,
        _genotype: &Self::Genotype,
        state: &S,
        _config: &C,
    ) {
        println!(
            "new best - generation: {}, fitness_score: {:?}, scale_index: {:?}",
            state.current_generation(),
            state.best_fitness_score(),
            state.current_scale_index(),
        );
    }

    fn on_exit<S: StrategyState<Self::Genotype>, C: StrategyConfig>(
        &mut self,
        _genotype: &Self::Genotype,
        state: &S,
        _config: &C,
    ) {
        println!("exit - iteration: {}", state.current_iteration());
        STRATEGY_ACTIONS.iter().for_each(|action| {
            if let Some(duration) = state.durations().get(action) {
                println!("  {:?}: {:.3?}", action, duration);
            }
        });
        println!(
            "  Total: {:.3?} ({:.0}% fitness)",
            &state.total_duration(),
            state.fitness_duration_rate() * 100.0
        );
    }
}

Required Associated Types§

Provided Methods§

Source

fn flush(&mut self, _output: &mut Vec<u8>)

Source

fn on_enter<S: StrategyState<Self::Genotype>, C: StrategyConfig>( &mut self, _genotype: &Self::Genotype, _state: &S, _config: &C, )

Source

fn on_exit<S: StrategyState<Self::Genotype>, C: StrategyConfig>( &mut self, _genotype: &Self::Genotype, _state: &S, _config: &C, )

Source

fn on_start<S: StrategyState<Self::Genotype>, C: StrategyConfig>( &mut self, _genotype: &Self::Genotype, _state: &S, _config: &C, )

Source

fn on_finish<S: StrategyState<Self::Genotype>, C: StrategyConfig>( &mut self, _genotype: &Self::Genotype, _state: &S, _config: &C, )

Source

fn on_new_generation<S: StrategyState<Self::Genotype>, C: StrategyConfig>( &mut self, _genotype: &Self::Genotype, _state: &S, _config: &C, )

Source

fn on_new_best_chromosome<S: StrategyState<Self::Genotype>, C: StrategyConfig>( &mut self, _genotype: &Self::Genotype, _state: &S, _config: &C, )

Source

fn on_new_best_chromosome_equal_fitness<S: StrategyState<Self::Genotype>, C: StrategyConfig>( &mut self, _genotype: &Self::Genotype, _state: &S, _config: &C, )

Source

fn on_extension_event<S: StrategyState<Self::Genotype>, C: StrategyConfig>( &mut self, _event: ExtensionEvent, _genotype: &Self::Genotype, _state: &S, _config: &C, )

Source

fn on_mutate_event<S: StrategyState<Self::Genotype>, C: StrategyConfig>( &mut self, _event: MutateEvent, _genotype: &Self::Genotype, _state: &S, _config: &C, )

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§

Source§

impl<G: EvolveGenotype> StrategyReporter for genetic_algorithm::strategy::evolve::EvolveReporterSimple<G>

Source§

impl<G: Genotype> StrategyReporter for Duration<G>

Source§

impl<G: Genotype> StrategyReporter for Noop<G>

Source§

impl<G: Genotype> StrategyReporter for genetic_algorithm::strategy::reporter::Simple<G>

Source§

impl<G: HillClimbGenotype> StrategyReporter for genetic_algorithm::strategy::hill_climb::HillClimbReporterSimple<G>

Source§

impl<G: PermutateGenotype> StrategyReporter for genetic_algorithm::strategy::permutate::PermutateReporterSimple<G>