private ISeq<Phenotype<G, C>> selectSurvivors(final ISeq<Phenotype<G, C>> population) { return _survivorsCount > 0 ?_survivorsSelector.select(population, _survivorsCount, _optimize) : ISeq.empty(); }
private ISeq<Phenotype<G, C>> selectOffspring(final ISeq<Phenotype<G, C>> population) { return _offspringCount > 0 ? _offspringSelector.select(population, _offspringCount, _optimize) : ISeq.empty(); }
@Override public ISeq<Phenotype<G, C>> select( final Seq<Phenotype<G, C>> population, final int count, final Optimize opt ) { if (count < 0) { throw new IllegalArgumentException(format( "Selection count must be greater or equal then zero, but was %s.", count )); } ISeq<Phenotype<G, C>> result; if (population.isEmpty() || count <= 0) { result = ISeq.empty(); } else { final int ec = min(count, _eliteCount); result = ELITE_SELECTOR.select(population, ec, opt); result = result.append( _nonEliteSelector.select(population, max(0, count - ec), opt) ); } return result; }
@Test(expectedExceptions = NullPointerException.class) public void selectNullPopulationArgument() { selector().select(null, 23, Optimize.MAXIMUM); }
@Test public void selectMin() { final Selector<DoubleGene, Vec<double[]>> selector = NSGA2Selector.ofVec(); final ISeq<Phenotype<DoubleGene, Vec<double[]>>> population = Stream.generate(this::phenotype) .limit(2000) .collect(ISeq.toISeq()); final ISeq<Phenotype<DoubleGene, Vec<double[]>>> selected = selector.select(population, 100, Optimize.MINIMUM); final double mean = selected.stream() .map(Phenotype::getFitness) .mapToDouble(NSGA2SelectorTest::dist) .sum()/selected.size(); Assert.assertTrue(mean < -0.8, format("Expect mean < -0.8: %s", mean)); Assert.assertEquals( selected.stream() .map(Phenotype::getFitness) .collect(Collectors.toSet()) .size(), selected.size() ); }
@Test(retryAnalyzer = Retry.class) public void selectMin() { final Selector<DoubleGene, Vec<double[]>> selector = UFTournamentSelector.ofVec(); final ISeq<Phenotype<DoubleGene, Vec<double[]>>> population = Stream.generate(this::phenotype) .limit(2000) .collect(ISeq.toISeq()); final ISeq<Phenotype<DoubleGene, Vec<double[]>>> selected = selector.select(population, 100, Optimize.MINIMUM); final double mean = selected.stream() .map(Phenotype::getFitness) .mapToDouble(NSGA2SelectorTest::dist) .sum()/selected.size(); Assert.assertTrue(mean < -0.15, format("Expect mean < -0.15: %s", mean)); Assert.assertEquals( selected.stream() .map(Phenotype::getFitness) .collect(Collectors.toSet()) .size(), selected.size() ); }
@Test public void selectMax() { final Selector<DoubleGene, Vec<double[]>> selector = NSGA2Selector.ofVec(); final ISeq<Phenotype<DoubleGene, Vec<double[]>>> population = Stream.generate(this::phenotype) .limit(2000) .collect(ISeq.toISeq()); final ISeq<Phenotype<DoubleGene, Vec<double[]>>> selected = selector.select(population, 100, Optimize.MAXIMUM); /* selected.stream().limit(100) .map(Phenotype::getFitness) .forEach(f -> System.out.println(f.data()[0] + " " + f.data()[1])); */ final double mean = selected.stream() .map(Phenotype::getFitness) .mapToDouble(NSGA2SelectorTest::dist) .sum()/selected.size(); Assert.assertTrue(mean > 0.8, format("Expect mean > 0.8: %s", mean)); Assert.assertEquals( selected.stream() .map(Phenotype::getFitness) .collect(Collectors.toSet()) .size(), selected.size() ); }
@Test(retryAnalyzer = Retry.class) public void selectMax() { final Selector<DoubleGene, Vec<double[]>> selector = UFTournamentSelector.ofVec(); final ISeq<Phenotype<DoubleGene, Vec<double[]>>> population = Stream.generate(this::phenotype) .limit(2000) .collect(ISeq.toISeq()); final ISeq<Phenotype<DoubleGene, Vec<double[]>>> selected = selector.select(population, 100, Optimize.MAXIMUM); /* selected.stream().limit(100) .map(Phenotype::getFitness) .forEach(f -> System.out.println(f.data()[0] + " " + f.data()[1])); */ final double mean = selected.stream() .map(Phenotype::getFitness) .mapToDouble(NSGA2SelectorTest::dist) .sum()/selected.size(); Assert.assertTrue(mean > 0.15, format("Expect mean > 0.15: %s", mean)); Assert.assertEquals( selected.stream() .map(Phenotype::getFitness) .collect(Collectors.toSet()) .size(), selected.size() ); }
.select(population, count, opt).stream() .mapToDouble(Phenotype::getFitness) .sum();
@Test(expectedExceptions = IllegalArgumentException.class) public void selectNegativeCountArgument() { final Factory<Genotype<DoubleGene>> gtf = Genotype.of(DoubleChromosome.of(0.0, 1.0)); final MSeq<Phenotype<DoubleGene, Double>> population = MSeq.ofLength(2); for (int i = 0, n = 2; i < n; ++i) { population.set(i, Phenotype.of(gtf.newInstance(), 12, TestUtils.FF)); } selector().select(population.toISeq(), -1, Optimize.MAXIMUM); }
@Test(expectedExceptions = NullPointerException.class) public void selectNullOptimizeArgument() { final Factory<Genotype<DoubleGene>> gtf = Genotype.of(DoubleChromosome.of(0.0, 1.0)); final MSeq<Phenotype<DoubleGene, Double>> population = MSeq.ofLength(2); for (int i = 0, n = 2; i < n; ++i) { population.set(i, Phenotype.of(gtf.newInstance(), 12, TestUtils.FF)); } selector().select(population.toISeq(), 1, null); }
selector.select(population, selectionCount, opt).stream() .map(pt -> pt.getGenotype().getGene().getAllele()) .forEach(hist::accept);