@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); }
/** * Create a new builder, with the current configuration. * * @since 3.1 * * @return a new builder, with the current configuration */ @Override public Builder<G, C> copy() { return new Builder<G, C>(_genotypeFactory, _fitnessFunction) .alterers(_alterer) .clock(_clock) .executor(_executor) .evaluator(_evaluator) .fitnessScaler(_fitnessScaler) .maximalPhenotypeAge(_maximalPhenotypeAge) .offspringFraction(_offspringFraction) .offspringSelector(_offspringSelector) .phenotypeValidator(_validator) .optimize(_optimize) .populationSize(_populationSize) .survivorsSelector(_survivorsSelector) .individualCreationRetries(_individualCreationRetries) .mapping(_mapper); }
/** * Create a new evolution {@code Engine.Builder} initialized with the values * of the current evolution {@code Engine}. With this method, the evolution * engine can serve as a template for a new one. * * @return a new engine builder */ public Builder<G, C> builder() { return new Builder<G, C>(_genotypeFactory, _fitnessFunction) .alterers(_alterer) .clock(_clock) .evaluator(_evaluator) .executor(_executor.get()) .fitnessScaler(_fitnessScaler) .maximalPhenotypeAge(_maximalPhenotypeAge) .offspringFraction((double)_offspringCount/(double)getPopulationSize()) .offspringSelector(_offspringSelector) .optimize(_optimize) .phenotypeValidator(_validator) .populationSize(getPopulationSize()) .survivorsSelector(_survivorsSelector) .individualCreationRetries(_individualCreationRetries) .mapping(_mapper); }