@@ -6,7 +6,9 @@ use crate::strategy::{StrategyAction, StrategyReporter, StrategyState};
6
6
use rand:: Rng ;
7
7
use std:: time:: Instant ;
8
8
9
- /// Rejuvenate parents to children in place, no copying of chromosomes
9
+ /// Drop non-selected parents, then clone top parents to repopulate, then rejuvenate selected
10
+ /// parents to children in place. No copying of chromosomes for creating the offspring itself, only
11
+ /// for repopulating the dropped non-selected parents (smaller fraction)
10
12
/// Allowed for unique genotypes.
11
13
#[ derive( Clone , Debug ) ]
12
14
pub struct Rejuvenate {
@@ -15,15 +17,25 @@ pub struct Rejuvenate {
15
17
impl Crossover for Rejuvenate {
16
18
fn call < G : EvolveGenotype , R : Rng , SR : StrategyReporter < Genotype = G > > (
17
19
& mut self ,
18
- _genotype : & mut G ,
20
+ genotype : & mut G ,
19
21
state : & mut EvolveState < G > ,
20
22
_config : & EvolveConfig ,
21
23
_reporter : & mut SR ,
22
24
_rng : & mut R ,
23
25
) {
24
26
let now = Instant :: now ( ) ;
27
+ let existing_population_size = state. population . chromosomes . len ( ) ;
25
28
let selected_population_size =
26
- ( state. population . size ( ) as f32 * self . selection_rate ) . ceil ( ) as usize ;
29
+ ( existing_population_size as f32 * self . selection_rate ) . ceil ( ) as usize ;
30
+ let dropped_population_size = ( existing_population_size - selected_population_size) . max ( 0 ) ;
31
+
32
+ genotype. chromosome_destructor_truncate (
33
+ & mut state. population . chromosomes ,
34
+ selected_population_size,
35
+ ) ;
36
+ genotype
37
+ . chromosome_cloner_expand ( & mut state. population . chromosomes , dropped_population_size) ;
38
+
27
39
state
28
40
. population
29
41
. chromosomes
0 commit comments