@@ -4,14 +4,14 @@ use std::collections::{HashMap, HashSet};
44type Row = usize ;
55type 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 ) ;
1515impl 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+
226285fn 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