Skip to content

Commit 6800698

Browse files
committed
Use standard update_best_chromosome_and_report in strategy setup for HillClimb and Permutate as well
Use fitness.call_for_state_population in HillClimb SteepestAscent setup
1 parent b12c2fc commit 6800698

File tree

2 files changed

+40
-16
lines changed

2 files changed

+40
-16
lines changed

src/strategy/hill_climb.rs

Lines changed: 34 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -310,24 +310,47 @@ impl<G: HillClimbGenotype, F: Fitness<Genotype = G>, SR: StrategyReporter<Genoty
310310
.add_duration(StrategyAction::SetupAndCleanup, now.elapsed());
311311

312312
match self.config.variant {
313-
HillClimbVariant::Stochastic => self.fitness.call_for_state_chromosome(
314-
&self.genotype,
315-
&mut self.state,
316-
&self.config,
317-
),
313+
HillClimbVariant::Stochastic => {
314+
self.fitness.call_for_state_chromosome(
315+
&self.genotype,
316+
&mut self.state,
317+
&self.config,
318+
);
319+
self.state.update_best_chromosome_from_state_chromosome(
320+
&mut self.genotype,
321+
&self.config,
322+
&mut self.reporter,
323+
);
324+
}
318325
HillClimbVariant::SteepestAscent => {
319-
// skip so calculate_for_chromosome does not have to be implemented on Fitness
326+
// population of one
327+
self.state.population.chromosomes.push(
328+
self.genotype
329+
.chromosome_cloner(self.state.chromosome.as_ref().unwrap()),
330+
);
331+
// calculate_for_chromosome does not have to be implemented on Fitness
332+
self.fitness.call_for_state_population(
333+
&self.genotype,
334+
&mut self.state,
335+
&self.config,
336+
None,
337+
);
338+
self.state.update_best_chromosome_from_state_population(
339+
&mut self.genotype,
340+
&self.config,
341+
&mut self.reporter,
342+
&mut self.rng,
343+
);
344+
// cleanup population
345+
self.genotype
346+
.chromosome_destructor_truncate(&mut self.state.population.chromosomes, 0);
320347
}
321348
}
322349

323-
// best by definition
350+
// in case fitness_score is None, set best by definition anyway
324351
self.state.best_generation = self.state.current_generation;
325-
self.state.best_fitness_score = self.state.chromosome.as_ref().unwrap().fitness_score();
326352
self.genotype
327353
.save_best_genes(self.state.chromosome.as_ref().unwrap());
328-
329-
self.reporter
330-
.on_new_best_chromosome(&self.genotype, &self.state, &self.config);
331354
}
332355
pub fn cleanup(&mut self, fitness_thread_local: Option<&mut ThreadLocal<RefCell<F>>>) {
333356
let now = Instant::now();

src/strategy/permutate.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -177,15 +177,16 @@ impl<G: PermutateGenotype, F: Fitness<Genotype = G>, SR: StrategyReporter<Genoty
177177
.add_duration(StrategyAction::SetupAndCleanup, now.elapsed());
178178
self.fitness
179179
.call_for_state_chromosome(&self.genotype, &mut self.state, &self.config);
180+
self.state.update_best_chromosome_and_report(
181+
&mut self.genotype,
182+
&self.config,
183+
&mut self.reporter,
184+
);
180185

181-
// best by definition
186+
// in case fitness_score is None, set best by definition anyway
182187
self.state.best_generation = self.state.current_generation;
183-
self.state.best_fitness_score = self.state.chromosome.as_ref().unwrap().fitness_score();
184188
self.genotype
185189
.save_best_genes(self.state.chromosome.as_ref().unwrap());
186-
187-
self.reporter
188-
.on_new_best_chromosome(&self.genotype, &self.state, &self.config);
189190
}
190191
pub fn cleanup(&mut self) {
191192
let now = Instant::now();

0 commit comments

Comments
 (0)