CompositeCodec implements Codec<T, G> { @Override public Factory<Genotype<G>> encoding() { return _encoding; } CompositeCodec(
		final ISeq<? extends Codec<?, G>> codecs,
		final Function<? super Object[], ? extends T> decoder
	); @Override Factory<Genotype<G>> encoding(); @Override Function<Genotype<G>, T> decoder(); @Override String toString();  }
@Test(dataProvider = "scalarCodecCount") public void minimalScalarCodec(final Integer scalars) { final ISeq<Codec<Double,DoubleGene>> seq = Stream .generate(() -> Codecs.ofScalar(DoubleRange.of(0, 1))) .limit(scalars) .collect(ISeq.toISeq()); final Codec<Double, DoubleGene> codec = new CompositeCodec<>( seq, values -> 10.0 ); final Genotype<DoubleGene> gt = codec.encoding().newInstance(); Assert.assertEquals(gt.length(), scalars.intValue()); }
@Test public void minimalScalarVectorCodec() { final Codec<Double, DoubleGene> codec = new CompositeCodec<>( ISeq.of( Codecs.ofScalar(DoubleRange.of(0, 1)), Codecs.ofVector(DoubleRange.of(10, 100), 3), Codecs.ofScalar(DoubleRange.of(2, 3)), Codecs.ofVector(DoubleRange.of(200, 500), DoubleRange.of(200, 500)) ), values -> 10.0 ); final Genotype<DoubleGene> gt = codec.encoding().newInstance(); Assert.assertEquals(gt.length(), 5); }
@Test public void encoding() { final Codec<Double, DoubleGene> codec = new CompositeCodec<>( ISeq.of( Codecs.ofScalar(DoubleRange.of(0, 1)), Codecs.ofVector(DoubleRange.of(10, 100), 3), Codecs.ofScalar(DoubleRange.of(2, 3)), Codecs.ofVector(DoubleRange.of(200, 500), DoubleRange.of(200, 500)) ), values -> { final Double v1 = (Double)values[0]; final double[] v2 = (double[])values[1]; final Double v3 = (Double)values[2]; final double[] v4 = (double[])values[3]; return v1 + DoubleAdder.sum(v2) + v3 + DoubleAdder.sum(v4); } ); final Genotype<DoubleGene> gt = codec.encoding().newInstance(); final double sum = gt.stream() .mapToDouble(c -> c.stream() .mapToDouble(DoubleGene::doubleValue) .sum()) .sum(); Assert.assertEquals(sum, codec.decoder().apply(gt), 0.000001); }