@Test public void testSelection() { SelectionStrategy<String, String> strategy = new FitnessProportionateSelection<>(4); List<EvaluatedIndividual<String>> population = new ArrayList<EvaluatedIndividual<String>>(4); population.add(new EvaluatedIndividual<>("a", 10.0)); population.add(new EvaluatedIndividual<>("b", 9.5)); population.add(new EvaluatedIndividual<>("c", 9.8)); population.add(new EvaluatedIndividual<>("d", 1.6)); population.add(new EvaluatedIndividual<>("e", 5.6)); population.add(new EvaluatedIndividual<>("f", 2.1)); ThreadedContext threadedRandom = new ThreadedContext(123); assertEquals(of("a", "b", "a", "f"), strategy.select(population, threadedRandom.getRandomForThread())); }
@Override public List<T> select(List<EvaluatedIndividual<T>> individuals, RandomDataGenerator random) { final List<Double> cumulativeFitnesses = accumulateFitness(individuals); final List<T> selection = new ArrayList<T>(selectionSize); for (int i = 0; i < selectionSize; i++) { double randomFitness = random.nextUniform(0, 1) * cumulativeFitnesses.get(cumulativeFitnesses.size() - 1); int index = binarySearch(cumulativeFitnesses, randomFitness); if (index < 0) { index = Math.abs(index + 1); } selection.add(individuals.get(index).getIndividual()); } return selection; }
FitnessProportionateSelection implements SelectionStrategy<T, T> { @Override public List<T> select(List<EvaluatedIndividual<T>> individuals, RandomDataGenerator random) { final List<Double> cumulativeFitnesses = accumulateFitness(individuals); final List<T> selection = new ArrayList<T>(selectionSize); for (int i = 0; i < selectionSize; i++) { double randomFitness = random.nextUniform(0, 1) * cumulativeFitnesses.get(cumulativeFitnesses.size() - 1); int index = binarySearch(cumulativeFitnesses, randomFitness); if (index < 0) { index = Math.abs(index + 1); } selection.add(individuals.get(index).getIndividual()); } return selection; } }
FitnessProportionateSelection implements SelectionStrategy<T, T> { @Override public List<T> select(List<EvaluatedIndividual<T>> individuals, RandomDataGenerator random) { final List<Double> cumulativeFitnesses = accumulateFitness(individuals); final List<T> selection = new ArrayList<T>(selectionSize); for (int i = 0; i < selectionSize; i++) { double randomFitness = random.nextUniform(0, 1) * cumulativeFitnesses.get(cumulativeFitnesses.size() - 1); int index = binarySearch(cumulativeFitnesses, randomFitness); if (index < 0) { index = Math.abs(index + 1); } selection.add(individuals.get(index).getIndividual()); } return selection; } @AccessedViaReflection  FitnessProportionateSelection(int selectionSize); }
FitnessProportionateSelection implements SelectionStrategy<T, T> { @Override public List<T> select(List<EvaluatedIndividual<T>> individuals, RandomDataGenerator random) { final List<Double> cumulativeFitnesses = accumulateFitness(individuals); final List<T> selection = new ArrayList<T>(selectionSize); for (int i = 0; i < selectionSize; i++) { double randomFitness = random.nextUniform(0, 1) * cumulativeFitnesses.get(cumulativeFitnesses.size() - 1); int index = binarySearch(cumulativeFitnesses, randomFitness); if (index < 0) { index = Math.abs(index + 1); } selection.add(individuals.get(index).getIndividual()); } return selection; } @AccessedViaReflection  FitnessProportionateSelection(int selectionSize); @Override List<T> select(List<EvaluatedIndividual<T>> individuals, RandomDataGenerator random); }
FitnessProportionateSelection implements SelectionStrategy<T, T> { @Override public List<T> select(List<EvaluatedIndividual<T>> individuals, RandomDataGenerator random) { final List<Double> cumulativeFitnesses = accumulateFitness(individuals); final List<T> selection = new ArrayList<T>(selectionSize); for (int i = 0; i < selectionSize; i++) { double randomFitness = random.nextUniform(0, 1) * cumulativeFitnesses.get(cumulativeFitnesses.size() - 1); int index = binarySearch(cumulativeFitnesses, randomFitness); if (index < 0) { index = Math.abs(index + 1); } selection.add(individuals.get(index).getIndividual()); } return selection; } @AccessedViaReflection  FitnessProportionateSelection(int selectionSize); @Override List<T> select(List<EvaluatedIndividual<T>> individuals, RandomDataGenerator random);  }