public List < T > apply ( List < T > selectedCandidates , Random rng ) { // Shuffle the collection before applying each operation so that the // evolution is not influenced by any ordering artifacts from previous // operations. List < T > selectionClone = new ArrayList < T > ( selectedCandidates ) ; Collections . shuffle ( selectionClone , rng ) ; List < T > result = new ArrayList < T > ( selectedCandidates . size ( ) ) ; Iterator < T > iterator = selectionClone . iterator ( ) ; while ( iterator . hasNext ( ) ) { T parent1 = iterator . next ( ) ; if ( iterator . hasNext ( ) ) { T parent2 = iterator . next ( ) ; // Randomly decide (according to the current cross-over probability) // whether to perform cross-over for these 2 parents. int crossoverPoints = crossoverProbabilityVariable . nextValue ( ) . nextEvent ( rng ) ? crossoverPointsVariable . nextValue ( ) : 0 ; if ( crossoverPoints > 0 ) { result . addAll ( mate ( parent1 , parent2 , crossoverPoints , rng ) ) ; } else { // If there is no cross-over to perform, just add the parents to the // results unaltered. result . add ( parent1 ) ; result . add ( parent2 ) ; } } else { // If we have an odd number of selected candidates, we can't pair up // the last one so just leave it unmodified. result . add ( parent1 ) ; } } return result ; }
Applies the cross - over operation to the selected candidates . Pairs of candidates are chosen randomly and subjected to cross - over to produce a pair of offspring candidates .