/** * Create a new <b>infinite</b> evolution iterator with the given initial * individuals. If an empty {@code Iterable} is given, the engines genotype * factory is used for creating the population. * * @deprecated Marked for removal in the next major version. * @param genotypes the initial individuals used for the evolution iterator. * Missing individuals are created and individuals not needed are * skipped. * @param generation the generation the stream starts from; must be greater * than zero. * @return a new <b>infinite</b> evolution iterator * @throws java.lang.NullPointerException if the given {@code genotypes} is * {@code null}. * @throws IllegalArgumentException if the given {@code generation} is * smaller then one */ @Deprecated public default Iterator<EvolutionResult<G, C>> iterator( final Iterable<Genotype<G>> genotypes, final long generation ) { return iterator(EvolutionInit.of( ISeq.of(genotypes), generation )); }
/** * Create a new evolution start object with the given population and for the * given generation. * * @param <G> the gene type * @param population the start population. * @param generation the start generation of the population * @return a new evolution start object * @throws java.lang.NullPointerException if the given {@code population} is * {@code null}. * @throws IllegalArgumentException if the given {@code generation} is * smaller then one */ public static <G extends Gene<?, G>> EvolutionInit<G> of( final ISeq<Genotype<G>> population, final long generation ) { return new EvolutionInit<>(population, generation); }
private Supplier<EvolutionStart<G, C>> evolutionStart(final EvolutionInit<G> init) { return evolutionStart(() -> EvolutionStart.of( init.getPopulation() .map(gt -> Phenotype.of( gt, init.getGeneration(), _fitnessFunction, _fitnessScaler) ), init.getGeneration()) ); }
/** * Create a new, possibly <em>infinite</em>, evolution stream with the given * initial individuals. If an empty {@code Iterable} is given, the engines * genotype factory is used for creating the population. * * @param genotypes the initial individuals used for the evolution stream. * Missing individuals are created and individuals not needed are * skipped. * @param generation the generation the stream starts from; must be greater * than zero. * @return a new evolution stream. * @throws java.lang.NullPointerException if the given {@code genotypes} is * {@code null}. * @throws IllegalArgumentException if the given {@code generation} is * smaller then one */ public default EvolutionStream<G, C> stream( final Iterable<Genotype<G>> genotypes, final long generation ) { return stream(EvolutionInit.of(ISeq.of(genotypes), generation)); }
@Test public void concat1b() { EvolutionInit<IntegerGene> init = EvolutionInit.of( EvolutionStreams.result(5) .toEvolutionStart() .getPopulation().stream() .map(Phenotype::getGenotype) .collect(ISeq.toISeq()), 1 ); final EvolutionStream<IntegerGene, Integer> stream = ConcatEngine.of(streamable(1)) .stream(init); final int[] array = stream .mapToInt(r -> r.getGenotypes().get(0).getGene().intValue()) .toArray(); Assert.assertEquals(array, new int[]{6}); }
@Test public void cycleInit() { final Chromosome<IntegerGene> ch = IntegerChromosome.of(IntegerGene.of(5, 0, 1000)); final Genotype<IntegerGene> gt = Genotype.of(ch); final EvolutionInit<IntegerGene> init = EvolutionInit.of( ISeq.<Genotype<IntegerGene>>of(gt), 1L ); final EvolutionStream<IntegerGene, Integer> stream = CyclicEngine.of( streamable(2), streamable(2) ) .stream(init); final int[] array = stream .limit(10) .mapToInt(r -> r.getGenotypes().get(0).getGene().intValue()) .toArray(); Assert.assertEquals(array, new int[]{6, 7, 8, 9, 10, 11, 12, 13, 14, 15}); }
@Test public void adaptInit() { final Chromosome<IntegerGene> ch = IntegerChromosome.of(IntegerGene.of(5, 0, 1000)); final Genotype<IntegerGene> gt = Genotype.of(ch); final EvolutionInit<IntegerGene> init = EvolutionInit.of( ISeq.<Genotype<IntegerGene>>of(gt), 1L ); final EvolutionStream<IntegerGene, Integer> stream = new AdaptiveEngine<IntegerGene, Integer>(r -> streamable(2)) .stream(init); final int[] array = stream .limit(Limits.byFixedGeneration(12)) .limit(10) .mapToInt(r -> r.getGenotypes().get(0).getGene().intValue()) .toArray(); Assert.assertEquals(array, new int[]{6, 7, 8, 9, 10, 11, 12, 13, 14, 15}); }
@Test public void concatInit() { final Chromosome<IntegerGene> ch = IntegerChromosome.of(IntegerGene.of(5, 0, 1000)); final Genotype<IntegerGene> gt = Genotype.of(ch); final EvolutionInit<IntegerGene> init = EvolutionInit.of( ISeq.<Genotype<IntegerGene>>of(gt), 1L ); final EvolutionStream<IntegerGene, Integer> stream = ConcatEngine.of( streamable(2), streamable(2), streamable(2), streamable(32) ) .stream(init); final int[] array = stream .limit(12) .mapToInt(r -> r.getGenotypes().get(0).getGene().intValue()) .toArray(); Assert.assertEquals(array, new int[]{6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17}); }