Skip to content

Commit 9836ca8

Browse files
committed
Add on_crossover_complete, on_mutation_complete and on_fitness_complete to StrategyReporter as well
Hook on_crossover_complete and on_mutation_complete to Evolve strategy Hook on_fitness_complete to all strategies
1 parent d88402a commit 9836ca8

File tree

4 files changed

+46
-8
lines changed

4 files changed

+46
-8
lines changed

src/strategy.rs

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -269,8 +269,11 @@ pub trait StrategyState<G: Genotype>: Display {
269269
/// * `on_enter` (before setup)
270270
/// * `on_start` (of run loop)
271271
/// * *in run loop:*
272-
/// * `on_generation_complete`
273272
/// * `on_selection_complete` (for Evolve only, as it is a more interesting point in the loop)
273+
/// * `on_crossover_complete` (for Evolve only, for completeness)
274+
/// * `on_mutation_complete` (for Evolve only, for completeness)
275+
/// * `on_fitness_complete` (for completeness)
276+
/// * `on_generation_complete`
274277
/// * `on_new_best_chromosome`
275278
/// * `on_new_best_chromosome_equal_fitness`
276279
/// * `on_select_event`
@@ -381,14 +384,35 @@ pub trait StrategyReporter: Clone + Send + Sync {
381384
_config: &C,
382385
) {
383386
}
384-
fn on_generation_complete<S: StrategyState<Self::Genotype>, C: StrategyConfig>(
387+
fn on_selection_complete<S: StrategyState<Self::Genotype>, C: StrategyConfig>(
385388
&mut self,
386389
_genotype: &Self::Genotype,
387390
_state: &S,
388391
_config: &C,
389392
) {
390393
}
391-
fn on_selection_complete<S: StrategyState<Self::Genotype>, C: StrategyConfig>(
394+
fn on_crossover_complete<S: StrategyState<Self::Genotype>, C: StrategyConfig>(
395+
&mut self,
396+
_genotype: &Self::Genotype,
397+
_state: &S,
398+
_config: &C,
399+
) {
400+
}
401+
fn on_mutation_complete<S: StrategyState<Self::Genotype>, C: StrategyConfig>(
402+
&mut self,
403+
_genotype: &Self::Genotype,
404+
_state: &S,
405+
_config: &C,
406+
) {
407+
}
408+
fn on_fitness_complete<S: StrategyState<Self::Genotype>, C: StrategyConfig>(
409+
&mut self,
410+
_genotype: &Self::Genotype,
411+
_state: &S,
412+
_config: &C,
413+
) {
414+
}
415+
fn on_generation_complete<S: StrategyState<Self::Genotype>, C: StrategyConfig>(
392416
&mut self,
393417
_genotype: &Self::Genotype,
394418
_state: &S,

src/strategy/evolve.rs

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -124,11 +124,11 @@ pub enum EvolveVariant {
124124
/// [StrategyReporter] (e.g. [EvolveReporterDuration], [EvolveReporterSimple]). But you are encouraged to
125125
/// roll your own, see [StrategyReporter].
126126
///
127-
/// For [Evolve] the reporting has an additional `on_selection_complete` hook, because
128-
/// that is a more interesting point in the loop, as the population and cardinality are refreshed
129-
/// after selection.
130-
/// The `on_generation_complete` contains all the new mutations, most of which will be immediately
131-
/// selected out. This gives the false impression of good cardinality while there is actually little.
127+
/// For [Evolve] the reporting has an additional `on_selection_complete` hook, because that is a
128+
/// more interesting point in the loop, as the population and cardinality are refreshed after
129+
/// selection. As the `on_generation_complete` contains all the new mutations, most of which will
130+
/// be immediately selected out. This gives the false impression of good cardinality while there is
131+
/// actually little.
132132
///
133133
/// From the [EvolveBuilder] level, there are several calling mechanisms:
134134
/// * [call](EvolveBuilder::call): this runs a single evolve strategy
@@ -306,6 +306,8 @@ impl<
306306
&mut self.reporter,
307307
&mut self.rng,
308308
);
309+
self.reporter
310+
.on_crossover_complete(&self.genotype, &self.state, &self.config);
309311
self.plugins.extension.after_crossover_complete(
310312
&mut self.genotype,
311313
&mut self.state,
@@ -321,6 +323,8 @@ impl<
321323
&mut self.reporter,
322324
&mut self.rng,
323325
);
326+
self.reporter
327+
.on_mutation_complete(&self.genotype, &self.state, &self.config);
324328
self.plugins.extension.after_mutation_complete(
325329
&mut self.genotype,
326330
&mut self.state,
@@ -335,6 +339,8 @@ impl<
335339
&self.config,
336340
fitness_thread_local.as_ref(),
337341
);
342+
self.reporter
343+
.on_fitness_complete(&self.genotype, &self.state, &self.config);
338344
self.plugins.extension.after_fitness_complete(
339345
&mut self.genotype,
340346
&mut self.state,

src/strategy/hill_climb.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,8 @@ impl<G: HillClimbGenotype, F: Fitness<Genotype = G>, SR: StrategyReporter<Genoty
228228
&mut self.state,
229229
&self.config,
230230
);
231+
self.reporter
232+
.on_fitness_complete(&self.genotype, &self.state, &self.config);
231233
self.state.update_best_chromosome_from_state_chromosome(
232234
&self.genotype,
233235
&self.config,
@@ -250,6 +252,8 @@ impl<G: HillClimbGenotype, F: Fitness<Genotype = G>, SR: StrategyReporter<Genoty
250252
&self.config,
251253
fitness_thread_local.as_ref(),
252254
);
255+
self.reporter
256+
.on_fitness_complete(&self.genotype, &self.state, &self.config);
253257
self.state.update_best_chromosome_from_state_population(
254258
&self.genotype,
255259
&self.config,

src/strategy/permutate.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,8 @@ impl<G: PermutateGenotype, F: Fitness<Genotype = G>, SR: StrategyReporter<Genoty
235235
&mut self.state,
236236
&self.config,
237237
);
238+
self.reporter
239+
.on_fitness_complete(&self.genotype, &self.state, &self.config);
238240
self.state.update_best_chromosome_and_report(
239241
&self.genotype,
240242
&self.config,
@@ -268,6 +270,8 @@ impl<G: PermutateGenotype, F: Fitness<Genotype = G>, SR: StrategyReporter<Genoty
268270
});
269271

270272
receiver.iter().for_each(|(chromosome, fitness_duration)| {
273+
self.reporter
274+
.on_fitness_complete(&self.genotype, &self.state, &self.config);
271275
self.state.increment_generation();
272276
self.state.chromosome.replace(chromosome);
273277
self.state.update_best_chromosome_and_report(

0 commit comments

Comments
 (0)