@ Override public ISeq < Phenotype < G , C > > select ( final Seq < Phenotype < G , C > > population , final int count , final Optimize opt ) { requireNonNull ( population , "Population" ) ; requireNonNull ( opt , "Optimization" ) ; if ( count < 0 ) { throw new IllegalArgumentException ( format ( "Selection count must be greater or equal then zero, but was %s" , count ) ) ; } final MSeq < Phenotype < G , C > > selection = MSeq . ofLength ( population . isEmpty ( ) ? 0 : count ) ; if ( count > 0 && ! population . isEmpty ( ) ) { final MSeq < Phenotype < G , C > > copy = population . asISeq ( ) . copy ( ) ; copy . sort ( ( a , b ) -> opt . < C > descending ( ) . compare ( a . getFitness ( ) , b . getFitness ( ) ) ) ; int size = count ; do { final int length = min ( min ( copy . size ( ) , size ) , _n ) ; for ( int i = 0 ; i < length ; ++ i ) { selection . set ( ( count - size ) + i , copy . get ( i ) ) ; } size -= length ; } while ( size > 0 ) ; } return selection . toISeq ( ) ; }
This method sorts the population in descending order while calculating the selection probabilities . If the selection size is greater the the population size the whole population is duplicated until the desired sample size is reached .