FitnessProportionateSelectionOperator extends AbstractProportionateSelectionOperator { protected double[] makeAccumulatedProbabilities(Individual[] inputIndividuals){ double[] accumulatedFitnessValues = new double[inputIndividuals.length]; double[] accumulatedProbabilities = new double[inputIndividuals.length]; double totalFitness = 0.0; for (int i = 0; i < inputIndividuals.length; i++){ if (inputIndividuals[i].getFitness() >= 0.0){ accumulatedFitnessValues[i] = totalFitness + inputIndividuals[i].getFitness(); totalFitness = accumulatedFitnessValues[i]; } else { throw new OptimizationException(this.getClass().getSimpleName() + ": individual fitness value cannot be negative but found value of " + inputIndividuals[i].getFitness()); } } for (int i = 0; i < inputIndividuals.length; i++){ accumulatedProbabilities[i] = accumulatedFitnessValues[i] / totalFitness; } accumulatedProbabilities[inputIndividuals.length-1] = 1.0; return accumulatedProbabilities; }    }
@Test public void testMakeAccumulatedProbabilities() throws Exception{ Individual[] inputIndividuals = new Individual[5]; double[] fitness = new double[]{2.0, 4.0, 1.0, 5.0, 3.0}; for (int i = 0; i < 5; i++){ inputIndividuals[i] = new SimpleIndividual(1, 1.0, new SimplePhenotypeRepresentation()); inputIndividuals[i].setFitness(fitness[i]); } double[] accumulatedProbabilities = this.operator.makeAccumulatedProbabilities(inputIndividuals); assertTrue(accumulatedProbabilities.length == 5); assertTrue(accumulatedProbabilities[0] == (2.0/15)); assertTrue(accumulatedProbabilities[1] == (6.0/15)); assertTrue(accumulatedProbabilities[2] == (7.0/15)); assertTrue(accumulatedProbabilities[3] == (12.0/15)); assertTrue(accumulatedProbabilities[4] == 1.0); }