Skip to content

Commit 29f342c

Browse files
committed
Add CustomReporter for examples/evolve_scrabble to showcase genes_key_cardinality v fitness_score_cardinality #7
1 parent 806f28d commit 29f342c

File tree

1 file changed

+65
-5
lines changed

1 file changed

+65
-5
lines changed

examples/evolve_scrabble.rs

Lines changed: 65 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,14 @@ use std::collections::{HashMap, HashSet};
44
type Row = usize;
55
type Column = usize;
66

7-
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
8-
enum Orientation {
7+
#[derive(Clone, Copy, Debug, Eq, PartialEq, Hash)]
8+
pub enum Orientation {
99
Horizontal,
1010
Vertical,
1111
}
1212

13-
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
14-
struct WordPosition(pub Row, pub Column, pub Orientation);
13+
#[derive(Clone, Copy, Debug, Eq, PartialEq, Hash)]
14+
pub struct WordPosition(pub Row, pub Column, pub Orientation);
1515
impl Allele for WordPosition {}
1616

1717
#[derive(Clone, Debug)]
@@ -223,6 +223,65 @@ impl ScrabbleFitness {
223223
}
224224
}
225225

226+
use cardinality_estimator::CardinalityEstimator;
227+
228+
#[derive(Clone)]
229+
pub struct CustomReporter(usize);
230+
impl StrategyReporter for CustomReporter {
231+
type Genotype = MultiListGenotype<WordPosition>;
232+
233+
fn on_enter<S: StrategyState<Self::Genotype>, C: StrategyConfig>(
234+
&mut self,
235+
genotype: &Self::Genotype,
236+
state: &S,
237+
config: &C,
238+
) {
239+
let number_of_seed_genes = genotype.seed_genes_list().len();
240+
if number_of_seed_genes > 0 {
241+
println!(
242+
"enter - {}, iteration: {}, number of seed genes: {}",
243+
config.variant(),
244+
state.current_iteration(),
245+
number_of_seed_genes
246+
);
247+
} else {
248+
println!(
249+
"enter - {}, iteration: {}",
250+
config.variant(),
251+
state.current_iteration()
252+
);
253+
}
254+
}
255+
256+
fn on_new_generation<S: StrategyState<Self::Genotype>, C: StrategyConfig>(
257+
&mut self,
258+
_genotype: &Self::Genotype,
259+
state: &S,
260+
_config: &C,
261+
) {
262+
if state.current_generation() % self.0 == 0 {
263+
let mut estimator = CardinalityEstimator::<u64>::new();
264+
state
265+
.population_as_ref()
266+
.chromosomes
267+
.iter()
268+
.map(|c| c.genes_key())
269+
.for_each(|key| estimator.insert(&key));
270+
let genes_key_cardinality = estimator.estimate();
271+
let fitness_score_cardinality = state.population_as_ref().fitness_score_cardinality();
272+
273+
println!(
274+
"current_generation: {}, stale_generations: {}, best_generation: {}, fitness_score_cardinality: {}, genes_key_cardinality: {}",
275+
state.current_generation(),
276+
state.stale_generations(),
277+
state.best_generation(),
278+
fitness_score_cardinality,
279+
genes_key_cardinality,
280+
);
281+
}
282+
}
283+
}
284+
226285
fn main() {
227286
env_logger::init();
228287

@@ -283,7 +342,8 @@ fn main() {
283342
// .with_reporter(EvolveReporterSimple::new_with_flags(
284343
// 100, false, false, false, true,
285344
// ))
286-
.with_reporter(EvolveReporterSimple::new(100))
345+
// .with_reporter(EvolveReporterSimple::new(100))
346+
.with_reporter(CustomReporter(50))
287347
.with_par_fitness(true)
288348
.with_fitness(ScrabbleFitness::new(
289349
words.clone(),

0 commit comments

Comments
 (0)