private static double f(final Genotype<DoubleGene> gt) { return IntStream.range(0, 50000) .mapToDouble(Math::sinh) .sum()*gt.getGene().getAllele(); }
@Test(dataProvider = "intScalarData") public void ofIntScalar(final IntRange domain) { final Codec<Integer, IntegerGene> codec = Codecs.ofScalar(domain); final Genotype<IntegerGene> gt = codec.encoding().newInstance(); Assert.assertEquals(gt.length(), 1); Assert.assertEquals(gt.getChromosome().length(), 1); Assert.assertEquals(gt.getGene().getMin().intValue(), domain.getMin()); Assert.assertEquals(gt.getGene().getMax().intValue(), domain.getMax()); final Function<Genotype<IntegerGene>, Integer> f = codec.decoder(); Assert.assertEquals(f.apply(gt).intValue(), gt.getGene().intValue()); }
@Test(dataProvider = "longScalarData") public void ofLongScalar(final LongRange domain) { final Codec<Long, LongGene> codec = Codecs.ofScalar(domain); final Genotype<LongGene> gt = codec.encoding().newInstance(); Assert.assertEquals(gt.length(), 1); Assert.assertEquals(gt.getChromosome().length(), 1); Assert.assertEquals(gt.getGene().getMin().longValue(), domain.getMin()); Assert.assertEquals(gt.getGene().getMax().longValue(), domain.getMax()); final Function<Genotype<LongGene>, Long> f = codec.decoder(); Assert.assertEquals(f.apply(gt).longValue(), gt.getGene().longValue()); }
@Test(dataProvider = "doubleScalarData") public void ofDoubleScalar(final DoubleRange domain) { final Codec<Double, DoubleGene> codec = Codecs.ofScalar(domain); final Genotype<DoubleGene> gt = codec.encoding().newInstance(); Assert.assertEquals(gt.length(), 1); Assert.assertEquals(gt.getChromosome().length(), 1); Assert.assertEquals(gt.getGene().getMin(), domain.getMin()); Assert.assertEquals(gt.getGene().getMax(), domain.getMax()); final Function<Genotype<DoubleGene>, Double> f = codec.decoder(); Assert.assertEquals(f.apply(gt), gt.getGene().doubleValue()); }
@Test public void ofAnyScalar2() { final Codec<Integer, AnyGene<Integer>> codec = Codecs.ofScalar( () -> RandomRegistry.getRandom().nextInt(1000) ); for (int i = 0; i < 1000; ++i) { final AnyGene<Integer> gene = codec.encoding() .newInstance().getGene(); Assert.assertTrue(gene.isValid()); Assert.assertTrue(gene.getAllele() < 1000); Assert.assertTrue(gene.getAllele() >= 0); } }
@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 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(dataProvider = "generations") public void generationCount(final Long generations) { final Engine<DoubleGene, Double> engine = Engine .builder(a -> a.getGene().getAllele(), DoubleChromosome.of(0, 1)) .build(); final EvolutionResult<DoubleGene, Double> result = engine.stream() .limit(generations) .collect(EvolutionResult.toBestEvolutionResult()); Assert.assertEquals(generations.longValue(), result.getTotalGenerations()); }
@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 emptyStreamCollectGenotype() { final Engine<DoubleGene, Double> engine = Engine .builder(a -> a.getGene().getAllele(), DoubleChromosome.of(0, 1)) .build(); final Genotype<DoubleGene> result = engine.stream() .limit(0) .collect(EvolutionResult.toBestGenotype()); Assert.assertNull(result); }
@Test public void emptyStreamCollectPhenotype() { final Engine<DoubleGene, Double> engine = Engine .builder(a -> a.getGene().getAllele(), DoubleChromosome.of(0, 1)) .build(); final Phenotype<DoubleGene, Double> result = engine.stream() .limit(0) .collect(EvolutionResult.toBestPhenotype()); Assert.assertNull(result); }
@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 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(dataProvider = "generations") public void generationLimit(final Long generations) { final Engine<DoubleGene, Double> engine = Engine .builder(a -> a.getGene().getAllele(), DoubleChromosome.of(0, 1)) .build(); final EvolutionResult<DoubleGene, Double> result = engine.stream() .limit(Limits.byFixedGeneration(generations)) .collect(EvolutionResult.toBestEvolutionResult()); Assert.assertEquals(generations.longValue(), result.getTotalGenerations()); }
@Test public void phenotypeValidator() { final int populationSize = 100; final Engine<DoubleGene, Double> engine = Engine .builder(a -> a.getGene().getAllele(), DoubleChromosome.of(0, 1)) .phenotypeValidator(pt -> false) .populationSize(populationSize) .build(); final EvolutionResult<DoubleGene, Double> result = engine.stream() .limit(10) .collect(EvolutionResult.toBestEvolutionResult()); Assert.assertEquals(result.getInvalidCount(), populationSize); }
private static Phenotype<DoubleGene, Double> pt(double value) { return Phenotype.of( Genotype.of(DoubleChromosome.of(DoubleGene.of(value, 0, 10))), 0, gt -> gt.getGene().getAllele() ); }
private static Phenotype<DoubleGene, Double> phenotype(final double value) { return Phenotype.of( Genotype.of(DoubleChromosome.of( DoubleGene.of(value, 0.0, 1000.0), DoubleGene.of(value, 0.0, 1000.0) )), 1, a -> a.getGene().getAllele() ); }