pub trait HillClimbReporter:
Clone
+ Send
+ Sync {
type Genotype: IncrementalGenotype;
// Provided methods
fn on_init(
&mut self,
_genotype: &Self::Genotype,
_state: &HillClimbState<Self::Genotype>,
_config: &HillClimbConfig,
) { ... }
fn on_start(
&mut self,
_state: &HillClimbState<Self::Genotype>,
_config: &HillClimbConfig,
) { ... }
fn on_finish(
&mut self,
_state: &HillClimbState<Self::Genotype>,
_config: &HillClimbConfig,
) { ... }
fn on_new_generation(
&mut self,
_state: &HillClimbState<Self::Genotype>,
_config: &HillClimbConfig,
) { ... }
fn on_new_best_chromosome(
&mut self,
_state: &HillClimbState<Self::Genotype>,
_config: &HillClimbConfig,
) { ... }
fn on_new_best_chromosome_equal_fitness(
&mut self,
_state: &HillClimbState<Self::Genotype>,
_config: &HillClimbConfig,
) { ... }
}Expand description
Reporter with event hooks in the HillClimb process. Since the HillClimb process sets a new best chromosome, even if the fitness is equal, there is an extra event hook for this situation.
§Example:
You are encouraged to take a look at the HillClimbReporterSimple implementation, and then roll your own like below:
use genetic_algorithm::strategy::hill_climb::prelude::*;
#[derive(Clone)]
pub struct CustomReporter { pub period: usize }
impl HillClimbReporter for CustomReporter {
type Genotype = BinaryGenotype;
fn on_new_generation(&mut self, state: &HillClimbState<Self::Genotype>, _config: &HillClimbConfig) {
if state.current_generation() % self.period == 0 {
println!(
"periodic - current_generation: {}, stale_generations: {}, best_generation: {}, current_scale_index: {:?}",
state.current_generation(),
state.stale_generations(),
state.best_generation(),
state.current_scale_index.as_ref(),
);
}
}
fn on_new_best_chromosome(&mut self, state: &HillClimbState<Self::Genotype>, _config: &HillClimbConfig) {
println!(
"new best - generation: {}, fitness_score: {:?}, genes: {:?}, scale_index: {:?}",
state.current_generation(),
state.best_fitness_score(),
state.best_chromosome_as_ref().genes,
state.current_scale_index.as_ref(),
);
}
fn on_finish(&mut self, state: &HillClimbState<Self::Genotype>, _config: &HillClimbConfig) {
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: &HillClimbState<Self::Genotype>, _config: &HillClimbConfig, )
fn on_start( &mut self, _state: &HillClimbState<Self::Genotype>, _config: &HillClimbConfig, )
fn on_finish( &mut self, _state: &HillClimbState<Self::Genotype>, _config: &HillClimbConfig, )
fn on_new_generation( &mut self, _state: &HillClimbState<Self::Genotype>, _config: &HillClimbConfig, )
sourcefn on_new_best_chromosome(
&mut self,
_state: &HillClimbState<Self::Genotype>,
_config: &HillClimbConfig,
)
fn on_new_best_chromosome( &mut self, _state: &HillClimbState<Self::Genotype>, _config: &HillClimbConfig, )
used to report on true improvement (new best chromosome with improved fitness)
sourcefn on_new_best_chromosome_equal_fitness(
&mut self,
_state: &HillClimbState<Self::Genotype>,
_config: &HillClimbConfig,
)
fn on_new_best_chromosome_equal_fitness( &mut self, _state: &HillClimbState<Self::Genotype>, _config: &HillClimbConfig, )
used to report on sideways move (new best chromosome with equal fitness)
Object Safety§
This trait is not object safe.