UnaryOperator<EvolutionResult<G, C>> toUniquePopulation(final Factory<Genotype<G>> factory) { return toUniquePopulation(factory, 100);
toUniquePopulation(factory, maxRetries);
@Test public void toUniquePopulation() { final ISeq<Genotype<IntegerGene>> genotypes = Genotype.of(IntegerChromosome.of(0, Integer.MAX_VALUE)).instances() .limit(10) .collect(ISeq.toISeq()); final EvolutionResult<IntegerGene, Integer> result = result(genotypes); Assert.assertSame( EvolutionResult.<IntegerGene, Integer>toUniquePopulation().apply(result), result ); }
@Test public void toUniquePopulation3() { final ISeq<Genotype<IntegerGene>> genotypes = Genotype.of(IntegerChromosome.of(0, 10)).instances() .limit(100) .collect(ISeq.toISeq()); final UnaryOperator<EvolutionResult<IntegerGene, Integer>> unifier = EvolutionResult.toUniquePopulation( Genotype.of(IntegerChromosome.of(0, 10))); final EvolutionResult<IntegerGene, Integer> result = result(genotypes); final EvolutionResult<IntegerGene, Integer> unified = unifier.apply(result); Assert.assertNotEquals(unified, result); Assert.assertTrue( unified.getGenotypes().stream().collect(Collectors.toSet()).size() < unified.getPopulation().size() ); Assert.assertEquals( result.getPopulation().size(), unified.getPopulation().size() ); }
@Test public void toUniquePopulation2() { final ISeq<Genotype<IntegerGene>> genotypes = Genotype.of(IntegerChromosome.of(0, 10)).instances() .limit(100) .collect(ISeq.toISeq()); final UnaryOperator<EvolutionResult<IntegerGene, Integer>> unifier = EvolutionResult.toUniquePopulation( Genotype.of(IntegerChromosome.of(0, Integer.MAX_VALUE))); final EvolutionResult<IntegerGene, Integer> result = result(genotypes); final EvolutionResult<IntegerGene, Integer> unified = unifier.apply(result); Assert.assertNotEquals(unified, result); Assert.assertEquals( unified.getGenotypes().stream().collect(Collectors.toSet()).size(), unified.getPopulation().size() ); Assert.assertEquals( result.getPopulation().size(), unified.getPopulation().size() ); }
@Test public void toUniquePopulation() { final int populationSize = 100; final Engine<IntegerGene, Integer> engine = Engine .builder(a -> a.getGene().getAllele(), IntegerChromosome.of(0, 10)) .populationSize(populationSize) .mapping(EvolutionResult.toUniquePopulation( Genotype.of(IntegerChromosome.of(0, Integer.MAX_VALUE)))) .build(); final EvolutionResult<IntegerGene, Integer> result = engine.stream() .limit(10) .peek(r -> { if (r.getGenotypes().stream().collect(Collectors.toSet()).size() != populationSize) { throw new AssertionError(format( "Expected unique population size %d, but got %d.", populationSize, r.getGenotypes().stream().collect(Collectors.toSet()).size() )); } }) .collect(EvolutionResult.toBestEvolutionResult()); Assert.assertEquals(result.getPopulation().size(), populationSize); }