TruncationSelectionOperator extends AbstractSelectionOperator { public Population[] select(Population[] populations) { checkConsistency(populations); List<Individual> allIndividuals = Arrays.asList(this.concatenateIndividualsFromPopulations(populations, this.inputArity)); Collections.sort(allIndividuals, Collections.reverseOrder()); int resultSize = this.individualsPerPopulation * this.outputArity; List<Individual> resultingIndividuals = allIndividuals.subList(0, resultSize); 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[] populations); void setRandomGenerator(RandomGenerator randomGenerator); void resetGenerationCount(); void nextGeneration(); void setGeneration(int currentGeneration);  }
@Test public void testSelect1() throws Exception{ SimplePopulation popIn = new SimplePopulation(); Individual[] individuals = new SimpleIndividual[5]; for (int i = 0; i < individuals.length; i++){ individuals[i] = new SimpleIndividual(0, 0.0, new SimplePhenotypeRepresentation()); individuals[i].setFitness((i-1)*(i-1)); } popIn.setIndividuals(individuals); this.operator.setInputArity(1); this.operator.setOutputArity(1); this.operator.setIndividualsPerPopulation(5); Population[] popsOut = this.operator.select(new SimplePopulation[]{popIn}); Individual ind = popsOut[0].getIndividuals()[0]; assertTrue(ind.getFitness() == 9.0); ind = popsOut[0].getIndividuals()[1]; assertTrue(ind.getFitness() == 4.0); ind = popsOut[0].getIndividuals()[2]; assertTrue(ind.getFitness() == 1.0); ind = popsOut[0].getIndividuals()[3]; assertTrue(ind.getFitness() == 1.0); ind = popsOut[0].getIndividuals()[4]; assertTrue(ind.getFitness() == 0.0); }
@Test public void testSelect2() throws Exception{ SimplePopulation popIn1 = new SimplePopulation(); SimplePopulation popIn2 = new SimplePopulation(); Individual[] individuals = new SimpleIndividual[5]; for (int i = 0; i < individuals.length; i++){ individuals[i] = new SimpleIndividual(0, 0.0, new SimplePhenotypeRepresentation()); individuals[i].setFitness((i-1)*(i-1)); } Individual[] ind1 = new Individual[]{individuals[0], individuals[2], individuals[4]}; Individual[] ind2 = new Individual[]{individuals[1], individuals[3]}; popIn1.setIndividuals(ind1); popIn2.setIndividuals(ind2); this.operator.setInputArity(2); this.operator.setOutputArity(2); this.operator.setIndividualsPerPopulation(2); Population[] popsOut = this.operator.select(new SimplePopulation[]{popIn1, popIn2}); assertTrue(popsOut.length == 2); assertTrue(popsOut[0].getIndividuals().length == 2); assertTrue(popsOut[1].getIndividuals().length == 2); Individual indOut = popsOut[0].getIndividuals()[0]; assertTrue(indOut.getFitness() == 9.0); indOut = popsOut[1].getIndividuals()[0]; assertTrue(indOut.getFitness() == 4.0); indOut = popsOut[0].getIndividuals()[1]; assertTrue(indOut.getFitness() == 1.0); indOut = popsOut[1].getIndividuals()[1]; assertTrue(indOut.getFitness() == 1.0); }