Trait genetic_algorithm::strategy::evolve::EvolveReporter
source · pub trait EvolveReporter:
Clone
+ Send
+ Sync {
type Genotype: Genotype;
// Provided methods
fn on_init(
&mut self,
_genotype: &Self::Genotype,
_state: &EvolveState<Self::Genotype>,
_config: &EvolveConfig,
) { ... }
fn on_start(
&mut self,
_state: &EvolveState<Self::Genotype>,
_config: &EvolveConfig,
) { ... }
fn on_finish(
&mut self,
_state: &EvolveState<Self::Genotype>,
_config: &EvolveConfig,
) { ... }
fn on_new_generation(
&mut self,
_state: &EvolveState<Self::Genotype>,
_config: &EvolveConfig,
) { ... }
fn on_new_best_chromosome(
&mut self,
_state: &EvolveState<Self::Genotype>,
_config: &EvolveConfig,
) { ... }
fn on_new_best_chromosome_equal_fitness(
&mut self,
_state: &EvolveState<Self::Genotype>,
_config: &EvolveConfig,
) { ... }
fn on_extension_event(
&mut self,
_event: ExtensionEvent,
_state: &EvolveState<Self::Genotype>,
_config: &EvolveConfig,
) { ... }
fn on_mutate_event(
&mut self,
_event: MutateEvent,
_state: &EvolveState<Self::Genotype>,
_config: &EvolveConfig,
) { ... }
}
Expand description
Reporter with event hooks in the Evolve process.
§Example:
You are encouraged to take a look at the EvolveReporterSimple implementation, and then roll your own like below:
use genetic_algorithm::strategy::evolve::prelude::*;
#[derive(Clone)]
pub struct CustomReporter { pub period: usize }
impl EvolveReporter for CustomReporter {
type Genotype = BinaryGenotype;
fn on_new_generation(&mut self, state: &EvolveState<Self::Genotype>, _config: &EvolveConfig) {
if state.current_generation() % self.period == 0 {
println!(
"periodic - current_generation: {}, stale_generations: {}, best_generation: {}, current_scale_index: {:?}, fitness_score_cardinality: {}, current_population_size: {}",
state.current_generation(),
state.stale_generations(),
state.best_generation(),
state.current_scale_index.as_ref(),
state.population.fitness_score_cardinality(),
state.population.size(),
);
}
}
fn on_new_best_chromosome(&mut self, state: &EvolveState<Self::Genotype>, _config: &EvolveConfig) {
println!(
"new best - generation: {}, fitness_score: {:?}, genes: {:?}, scale_index: {:?}, population_size: {}",
state.current_generation(),
state.best_fitness_score(),
state.best_chromosome_as_ref().genes,
state.current_scale_index.as_ref(),
state.population.size(),
);
}
fn on_finish(&mut self, state: &EvolveState<Self::Genotype>, _config: &EvolveConfig) {
println!("finish - iteration: {}", state.current_iteration());
STRATEGY_ACTIONS.iter().for_each(|action| {
if let Some(duration) = state.durations.get(action) {
println!(" {:?}: {:?}", action, duration,);
}
});
println!(" Total: {:?}", &state.total_duration());
}
}
Required Associated Types§
Provided Methods§
fn on_init( &mut self, _genotype: &Self::Genotype, _state: &EvolveState<Self::Genotype>, _config: &EvolveConfig, )
fn on_start( &mut self, _state: &EvolveState<Self::Genotype>, _config: &EvolveConfig, )
fn on_finish( &mut self, _state: &EvolveState<Self::Genotype>, _config: &EvolveConfig, )
fn on_new_generation( &mut self, _state: &EvolveState<Self::Genotype>, _config: &EvolveConfig, )
fn on_new_best_chromosome( &mut self, _state: &EvolveState<Self::Genotype>, _config: &EvolveConfig, )
fn on_new_best_chromosome_equal_fitness( &mut self, _state: &EvolveState<Self::Genotype>, _config: &EvolveConfig, )
fn on_extension_event( &mut self, _event: ExtensionEvent, _state: &EvolveState<Self::Genotype>, _config: &EvolveConfig, )
fn on_mutate_event( &mut self, _event: MutateEvent, _state: &EvolveState<Self::Genotype>, _config: &EvolveConfig, )
Object Safety§
This trait is not object safe.