@Test public void concat0() { final EvolutionStream<IntegerGene, Integer> stream = ConcatEngine.<IntegerGene, Integer>of().stream(); final int[] array = stream .mapToInt(r -> r.getGenotypes().get(0).getGene().intValue()) .toArray(); Assert.assertEquals(array, new int[]{}); }
@Test public void adapt1() { final EvolutionStream<IntegerGene, Integer> stream = new AdaptiveEngine<IntegerGene, Integer>(r -> streamable(2)) .stream(); final int[] array = stream.limit(12) .mapToInt(r -> r.getGenotypes().get(0).getGene().intValue()) .toArray(); Assert.assertEquals(array, new int[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12}); }
@Test public void cycle0() { final EvolutionStream<IntegerGene, Integer> stream = CyclicEngine.<IntegerGene, Integer>of() .stream(); final int[] array = stream .mapToInt(r -> r.getGenotypes().get(0).getGene().intValue()) .toArray(); Assert.assertEquals(array, new int[]{}); }
@Test public void concat1() { final EvolutionStream<IntegerGene, Integer> stream = ConcatEngine.of(streamable(1)) .stream(); final int[] array = stream .mapToInt(r -> r.getGenotypes().get(0).getGene().intValue()) .toArray(); Assert.assertEquals(array, new int[]{1}); }
@Test public void concat2() { final EvolutionStream<IntegerGene, Integer> stream = ConcatEngine.of(streamable(5)) .stream(); final int[] array = stream .mapToInt(r -> r.getGenotypes().get(0).getGene().intValue()) .toArray(); Assert.assertEquals(array, new int[]{1, 2, 3, 4, 5}); }
@Test public void adapt3() { final EvolutionStream<IntegerGene, Integer> stream = new AdaptiveEngine<IntegerGene, Integer>(r -> streamable(2)) .stream(); final int[] array = stream .limit(Limits.byFixedGeneration(12)) .limit(10) .mapToInt(r -> r.getGenotypes().get(0).getGene().intValue()) .toArray(); Assert.assertEquals(array, new int[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}); }
@Test public void cycle1() { final EvolutionStream<IntegerGene, Integer> stream = CyclicEngine.of( streamable(2), streamable(2), streamable(2) ) .stream(); final int[] array = stream.limit(12) .mapToInt(r -> r.getGenotypes().get(0).getGene().intValue()) .toArray(); Assert.assertEquals(array, new int[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12}); }
@Test public void concat1a() { final EvolutionStream<IntegerGene, Integer> stream = ConcatEngine.of(streamable(1)) .stream(() -> EvolutionStreams.result(5).toEvolutionStart()); final int[] array = stream .mapToInt(r -> r.getGenotypes().get(0).getGene().intValue()) .toArray(); Assert.assertEquals(array, new int[]{6}); }
@Test public void concat3() { final EvolutionStream<IntegerGene, Integer> stream = ConcatEngine.of(streamable(5)) .stream() .limit(Limits.byFixedGeneration(3)); final int[] array = stream .mapToInt(r -> r.getGenotypes().get(0).getGene().intValue()) .toArray(); Assert.assertEquals(array, new int[]{1, 2, 3}); }
@Test public void adapt2() { final EvolutionStream<IntegerGene, Integer> stream = new AdaptiveEngine<IntegerGene, Integer>(r -> streamable(2)) .stream(); final int[] array = stream .limit(Limits.byFixedGeneration(12)) .mapToInt(r -> r.getGenotypes().get(0).getGene().intValue()) .toArray(); Assert.assertEquals(array, new int[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12}); }
@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 cycle3() { final EvolutionStream<IntegerGene, Integer> stream = CyclicEngine.of( streamable(2), streamable(2) ) .stream(); final int[] array = stream .limit(Limits.byFixedGeneration(12)) .limit(10) .mapToInt(r -> r.getGenotypes().get(0).getGene().intValue()) .toArray(); Assert.assertEquals(array, new int[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}); }
@Test public void concat4() { final EvolutionStream<IntegerGene, Integer> stream = ConcatEngine.of( streamable(3), streamable(4), streamable(5) ) .stream(); final int[] array = stream .mapToInt(r -> r.getGenotypes().get(0).getGene().intValue()) .toArray(); Assert.assertEquals(array, new int[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12}); }
@Test public void cycle2() { final EvolutionStream<IntegerGene, Integer> stream = CyclicEngine.of( streamable(2), streamable(2) ) .stream(); final int[] array = stream .limit(Limits.byFixedGeneration(12)) .mapToInt(r -> r.getGenotypes().get(0).getGene().intValue()) .toArray(); Assert.assertEquals(array, new int[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12}); }
@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 concat5() { final EvolutionStream<IntegerGene, Integer> stream = ConcatEngine.of( streamable(3), streamable(4), streamable(5), streamable(15), streamable(15) ) .stream() .limit(Limits.byFixedGeneration(15)); final int[] array = stream .mapToInt(r -> r.getGenotypes().get(0).getGene().intValue()) .toArray(); Assert.assertEquals(array, new int[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}); }
public static void main(final String[] args) throws Exception { final Problem<BitChromosome, BitGene, Integer> count = Problem.of( BitChromosome::bitCount, Codec.of( Genotype.of(BitChromosome.of(10)), gt -> gt.getChromosome().as(BitChromosome.class) ) ); final Engine<BitGene, Integer> engine = Engine.builder(count).build(); final EvolutionResult<BitGene, Integer> result = engine.stream() .limit(10) .collect(EvolutionResult.toBestEvolutionResult()); final ISeq<Genotype<BitGene>> genotypes = result.getGenotypes(); Writers.write(System.out, genotypes.asList(), Writers.BitChromosome.writer()); Readers.read(System.in, Readers.BitChromosome.reader()); engine.stream(genotypes); }