11import { CrossoverBase , IChromosome } from '@technote-space/genetic-algorithms-js' ;
2+ import { Genotype } from '../Genotype' ;
23
34export class Mgg extends CrossoverBase {
5+ private _pool : Array < IChromosome > = [ ] ;
6+
47 public constructor ( probability : number , private readonly mixProbability : number , private readonly crossoverTime : number ) {
58 super ( 2 , crossoverTime * 2 , probability ) ;
69
@@ -9,15 +12,22 @@ export class Mgg extends CrossoverBase {
912 this . mixProbability = 1 - mixProbability ;
1013 }
1114 this . mixProbability = Math . min ( Math . max ( this . mixProbability , 0 ) , 0.5 ) ;
15+
16+ [ ...Array ( this . childrenNumber ) ] . forEach ( ( ) => {
17+ this . _pool . push ( new Genotype ( ) ) ;
18+ } ) ;
1219 }
1320
1421 protected performCross ( parents : Array < IChromosome > , probability : number ) : Array < IChromosome > {
1522 const parent1 = parents [ 0 ] ;
1623 const parent2 = parents [ 1 ] ;
1724
18- return [ ...Array ( this . crossoverTime ) ] . flatMap ( ( ) => {
19- const child1 = parent1 . clone ( ) ;
20- const child2 = parent2 . clone ( ) ;
25+ let index = 0 ;
26+ [ ...Array ( this . crossoverTime ) ] . forEach ( ( ) => {
27+ const child1 = this . _pool [ index ++ ] ;
28+ const child2 = this . _pool [ index ++ ] ;
29+ child1 . copyFrom ( parent1 ) ;
30+ child2 . copyFrom ( parent2 ) ;
2131
2232 if ( probability > 0 && Math . random ( ) < probability ) {
2333 const len = Math . min ( parent1 . length , parent2 . length ) ;
@@ -30,8 +40,8 @@ export class Mgg extends CrossoverBase {
3040 }
3141 }
3242 }
33-
34- return [ child1 , child2 ] ;
3543 } ) ;
44+
45+ return this . _pool ;
3646 }
3747}
0 commit comments