BinaryTournamentSelectionOperator extends AbstractSelectionOperator { public Population[] select(Population[] populations) { checkConsistency(populations); Individual[] inputIndividuals = this.concatenateIndividualsFromPopulations(populations, this.inputArity); int resultSize = this.individualsPerPopulation * this.outputArity; List<Individual> resultingIndividuals = new ArrayList<Individual>(resultSize); Individual individual1; Individual individual2; for (int i = 0; i < resultSize; i++){ individual1 = inputIndividuals[this.randomGenerator.nextInt(inputIndividuals.length)]; individual2 = inputIndividuals[this.randomGenerator.nextInt(inputIndividuals.length)]; resultingIndividuals.add(individual1.getFitness() > individual2.getFitness() ? individual1 : individual2); } Population[] outputPopulations = this.createOutputPopulations(); Individual[][] outputIndividuals = this.createOutputIndividuals(); this.assignIndividualsToPopulations(outputIndividuals, outputPopulations); this.mapByColumns(resultingIndividuals, outputIndividuals); return outputPopulations; }  Population[] select(Population[] populations); @Override void checkConsistency(Population[] inputPopulations); void setRandomGenerator(RandomGenerator randomGenerator); void resetGenerationCount(); void nextGeneration(); void setGeneration(int currentGeneration);  }
@Test public void testSelect1() throws Exception { Individual ind1 = new SimpleIndividual(0, 0.0, new SimplePhenotypeRepresentation()); ind1.setFitness(10.0); Individual ind2 = new SimpleIndividual(0, 0.0, new SimplePhenotypeRepresentation()); ind1.setFitness(1.0); Population popIn1 = new SimplePopulation(new Individual[]{ind2, ind1}); this.operator.setInputArity(1); this.operator.setIndividualsPerPopulation(100); this.operator.setOutputArity(1); Population[] popsOut = this.operator.select(new Population[]{popIn1}); assertTrue(popsOut != null); assertTrue(popsOut.length == 1); Population outPop = popsOut[0]; assertTrue(outPop.getIndividuals().length == 100); int fittestCount = 0; for (Individual ind : outPop.getIndividuals()){ if (ind == ind1){ fittestCount++; } } assertTrue(((double)fittestCount/100) > 0.65); }