StochasticUniversalSamplingSelectionOperator extends FitnessProportionateSelectionOperator { @Override protected List<Individual> selectWinners(Individual[] inputIndividuals, double[] accumulatedProbabilities) { int resultSize = this.outputArity * this.individualsPerPopulation; List<Individual> resultingIndividuals = new ArrayList<Individual>(resultSize); double stepSize = 1.0 / resultSize; double fraction = this.randomGenerator.nextDouble(0.0, stepSize); int probabilitiesIndex = 0; for (int currentStep = 0; currentStep < resultSize; currentStep++){ double currentProbability = stepSize * currentStep + fraction; while (true){ if (accumulatedProbabilities[probabilitiesIndex] >= currentProbability){ resultingIndividuals.add(inputIndividuals[probabilitiesIndex]); break; } else { probabilitiesIndex++; } } } return resultingIndividuals; }    }
@Test public void testSelectWinners1() throws Exception { this.operator.setIndividualsPerPopulation(3); double[] accumulatedProbabilities = this.operator.makeAccumulatedProbabilities(this.inputIndividuals); List<Individual> winners = this.operator.selectWinners(this.inputIndividuals, accumulatedProbabilities); assertEquals(3, winners.size()); assertSame(this.inputIndividuals[0], winners.get(0)); assertSame(this.inputIndividuals[2], winners.get(1)); assertSame(this.inputIndividuals[3], winners.get(2)); }
@Test public void testSelectWinners2() throws Exception { this.operator.setIndividualsPerPopulation(5); double[] accumulatedProbabilities = this.operator.makeAccumulatedProbabilities(this.inputIndividuals); List<Individual> winners = this.operator.selectWinners(this.inputIndividuals, accumulatedProbabilities); assertEquals(5, winners.size()); assertSame(this.inputIndividuals[0], winners.get(0)); assertSame(this.inputIndividuals[1], winners.get(1)); assertSame(this.inputIndividuals[3], winners.get(2)); assertSame(this.inputIndividuals[3], winners.get(3)); assertSame(this.inputIndividuals[4], winners.get(4)); }