UniformStochasticSelectionOperator extends AbstractSelectionOperator { public Population[] select(Population[] populations) { checkConsistency(populations); Individual[] inputIndividuals = this.concatenateIndividualsFromPopulations(populations, this.inputArity); int inputSize = inputIndividuals.length; int resultSize = this.getOutputArity() * this.getIndividualsPerPopulation(); List<Individual> selectedIndividuals = new ArrayList<Individual>(resultSize); for (int i = 0; i < resultSize; i++){ selectedIndividuals.add(inputIndividuals[this.randomGenerator.nextInt(inputSize)]); } Population[] outputPopulations = this.createOutputPopulations(); Individual[][] outputIndividuals = this.createOutputIndividuals(); this.mapByColumns(selectedIndividuals, outputIndividuals); this.assignIndividualsToPopulations(outputIndividuals, outputPopulations); 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 { SimplePopulation popIn1 = new SimplePopulation(); Individual[] individuals1 = new SimpleIndividual[2]; for (int i = 0; i < individuals1.length; i++){ individuals1[i] = new SimpleIndividual(0, 0.0, new SimplePhenotypeRepresentation()); individuals1[i].setFitness((i-1)*(i-1)); } popIn1.setIndividuals(individuals1); this.operator.setInputArity(1); this.operator.setOutputArity(1); this.operator.setIndividualsPerPopulation(4); Population[] popsOut = this.operator.select(new SimplePopulation[]{popIn1}); assertTrue(popsOut.length == 1); assertTrue(popsOut[0].getIndividuals().length == 4); for (Individual ind : popsOut[0].getIndividuals()){ assertTrue(ind != null); } }
@Test public void testSelect2() throws Exception { SimplePopulation popIn1 = new SimplePopulation(); SimplePopulation popIn2 = new SimplePopulation(); Individual[] individuals1 = new SimpleIndividual[2]; for (int i = 0; i < individuals1.length; i++){ individuals1[i] = new SimpleIndividual(0, 0.0, new SimplePhenotypeRepresentation()); individuals1[i].setFitness((i-1)*(i-1)); } Individual[] individuals2 = new SimpleIndividual[2]; for (int i = 0; i < individuals2.length; i++){ individuals2[i] = new SimpleIndividual(0, 0.0, new SimplePhenotypeRepresentation()); individuals2[i].setFitness((i-1)*(i-1)); } popIn1.setIndividuals(individuals1); popIn2.setIndividuals(individuals2); this.operator.setInputArity(2); this.operator.setOutputArity(1); this.operator.setIndividualsPerPopulation(2); Population[] popsOut = this.operator.select(new SimplePopulation[]{popIn1, popIn2}); assertTrue(popsOut.length == 1); assertTrue(popsOut[0].getIndividuals().length == 2); for (Individual ind : popsOut[0].getIndividuals()){ assertTrue(ind != null); } }