Codecs { public static InvertibleCodec<int[], IntegerGene> ofVector( final IntRange domain, final int length ) { requireNonNull(domain); Requires.positive(length); return InvertibleCodec.of( Genotype.of(IntegerChromosome.of(domain, length)), gt -> gt.chromosome().as(IntegerChromosome.class).toArray(), val -> Genotype.of( IntegerChromosome.of( IntStream.of(val) .mapToObj(i -> IntegerGene.of(i, domain)) .collect(ISeq.toISeq()) ) ) ); } private  Codecs(); static InvertibleCodec<Integer, IntegerGene> ofScalar(final IntRange domain); static InvertibleCodec<Long, LongGene> ofScalar(final LongRange domain); static InvertibleCodec<Double, DoubleGene> ofScalar(final DoubleRange domain); static Codec<A, AnyGene<A>> ofScalar(
		final Supplier<? extends A> supplier,
		final Predicate<? super A> validator
	); static Codec<A, AnyGene<A>> ofScalar(
		final Supplier<? extends A> supplier
	); static InvertibleCodec<int[], IntegerGene> ofVector(
		final IntRange domain,
		final int length
	); static InvertibleCodec<long[], LongGene> ofVector(
		final LongRange domain,
		final int length
	); static InvertibleCodec<double[], DoubleGene> ofVector(
		final DoubleRange domain,
		final int length
	); static InvertibleCodec<int[], IntegerGene> ofVector(final IntRange... domains); static InvertibleCodec<long[], LongGene> ofVector(final LongRange... domains); static InvertibleCodec<double[], DoubleGene> ofVector(final DoubleRange... domains); static Codec<ISeq<A>, AnyGene<A>> ofVector(
		final Supplier<? extends A> supplier,
		final Predicate<? super A> alleleValidator,
		final Predicate<? super ISeq<A>> alleleSeqValidator,
		final int length
	); static Codec<ISeq<A>, AnyGene<A>> ofVector(
		final Supplier<? extends A> supplier,
		final Predicate<? super A> validator,
		final int length
	); static Codec<ISeq<A>, AnyGene<A>> ofVector(
		final Supplier<? extends A> supplier,
		final int length
	); static InvertibleCodec<int[], EnumGene<Integer>> ofPermutation(final int length); static InvertibleCodec<ISeq<T>, EnumGene<T>> ofPermutation(final ISeq<? extends T> alleles); static InvertibleCodec<int[][], IntegerGene> ofMatrix(
		final IntRange domain,
		final int rows,
		final int cols
	); static InvertibleCodec<long[][], LongGene> ofMatrix(
		final LongRange domain,
		final int rows,
		final int cols
	); static InvertibleCodec<double[][], DoubleGene> ofMatrix(
		final DoubleRange domain,
		final int rows,
		final int cols
	); static InvertibleCodec<M, EnumGene<Integer>> ofMapping(
		final ISeq<? extends A> source,
		final ISeq<? extends B> target,
		final Supplier<M> mapSupplier
	); static InvertibleCodec<Map<A, B>, EnumGene<Integer>> ofMapping(final ISeq<? extends A> source, final ISeq<? extends B> target); static InvertibleCodec<ISeq<T>, BitGene> ofSubSet(final ISeq<? extends T> basicSet); static InvertibleCodec<ISeq<T>, EnumGene<T>> ofSubSet(
		final ISeq<? extends T> basicSet,
		final int size
	);  }
@Test(dataProvider = "intVectorData") public void ofIntVector(final IntRange domain, final int length) { final Codec<int[], IntegerGene> codec = Codecs.ofVector(domain, length); final Genotype<IntegerGene> gt = codec.encoding().newInstance(); assertEquals(gt.length(), 1); assertEquals(gt.chromosome().length(), length); for (IntegerGene gene : gt.chromosome()) { assertEquals(gene.min().intValue(), domain.min()); assertEquals(gene.max().intValue(), domain.max()); } final Function<Genotype<IntegerGene>, int[]> f = codec.decoder(); final int[] value = f.apply(gt); assertEquals(value.length, length); for (int i = 0; i < length; ++i) { assertEquals(gt.get(0).get(i).intValue(), value[i]); } }
@Test(dataProvider = "longVectorData") public void ofLongVector(final LongRange domain, final int length) { final Codec<long[], LongGene> codec = Codecs.ofVector(domain, length); final Genotype<LongGene> gt = codec.encoding().newInstance(); assertEquals(gt.length(), 1); assertEquals(gt.chromosome().length(), length); for (LongGene gene : gt.chromosome()) { assertEquals(gene.min().longValue(), domain.min()); assertEquals(gene.max().longValue(), domain.max()); } final Function<Genotype<LongGene>, long[]> f = codec.decoder(); final long[] value = f.apply(gt); assertEquals(value.length, length); for (int i = 0; i < length; ++i) { assertEquals(gt.get(0).get(i).longValue(), value[i]); } }
@Test(dataProvider = "doubleVectorData") public void ofDoubleVector(final DoubleRange domain, final int length) { final Codec<double[], DoubleGene> codec = Codecs.ofVector(domain, length); final Genotype<DoubleGene> gt = codec.encoding().newInstance(); assertEquals(gt.length(), 1); assertEquals(gt.chromosome().length(), length); for (DoubleGene gene : gt.chromosome()) { assertEquals(gene.min().doubleValue(), domain.min()); assertEquals(gene.max().doubleValue(), domain.max()); } final Function<Genotype<DoubleGene>, double[]> f = codec.decoder(); final double[] value = f.apply(gt); assertEquals(value.length, length); for (int i = 0; i < length; ++i) { assertEquals(gt.get(0).get(i).doubleValue(), value[i]); } }
@Test(dataProvider = "intVectorDataVector") public void ofIntVectorVector(final IntRange[] domain) { final Codec<int[], IntegerGene> codec = Codecs.ofVector(domain); final Genotype<IntegerGene> gt = codec.encoding().newInstance(); assertEquals(gt.length(), domain.length); for (int i = 0; i < gt.length(); ++i) { final Chromosome<IntegerGene> ch = gt.get(i); assertEquals(ch.length(), 1); final IntegerGene gene = ch.gene(); assertEquals(gene.min().intValue(), domain[i].min()); assertEquals(gene.max().intValue(), domain[i].max()); } final Function<Genotype<IntegerGene>, int[]> f = codec.decoder(); final int[] value = f.apply(gt); assertEquals(value.length, domain.length); for (int i = 0; i < domain.length; ++i) { assertEquals(gt.get(i).get(0).intValue(), value[i]); } }
@Test(dataProvider = "longVectorDataVector") public void ofLongVectorVector(final LongRange[] domain) { final Codec<long[], LongGene> codec = Codecs.ofVector(domain); final Genotype<LongGene> gt = codec.encoding().newInstance(); assertEquals(gt.length(), domain.length); for (int i = 0; i < gt.length(); ++i) { final Chromosome<LongGene> ch = gt.get(i); assertEquals(ch.length(), 1); final LongGene gene = ch.gene(); assertEquals(gene.min().longValue(), domain[i].min()); assertEquals(gene.max().longValue(), domain[i].max()); } final Function<Genotype<LongGene>, long[]> f = codec.decoder(); final long[] value = f.apply(gt); assertEquals(value.length, domain.length); for (int i = 0; i < domain.length; ++i) { assertEquals(gt.get(i).get(0).longValue(), value[i]); } }
@Test(dataProvider = "doubleVectorDataVector") public void ofDoubleVectorVector(final DoubleRange[] domain) { final Codec<double[], DoubleGene> codec = Codecs.ofVector(domain); final Genotype<DoubleGene> gt = codec.encoding().newInstance(); assertEquals(gt.length(), domain.length); for (int i = 0; i < gt.length(); ++i) { final Chromosome<DoubleGene> ch = gt.get(i); assertEquals(ch.length(), 1); final DoubleGene gene = ch.gene(); assertEquals(gene.min().doubleValue(), domain[i].min()); assertEquals(gene.max().doubleValue(), domain[i].max()); } final Function<Genotype<DoubleGene>, double[]> f = codec.decoder(); final double[] value = f.apply(gt); assertEquals(value.length, domain.length); for (int i = 0; i < domain.length; ++i) { assertEquals(gt.get(i).get(0).doubleValue(), value[i]); } }
@Test public void ofAnyVector() { final int length = 23; final Codec<ISeq<Integer>, AnyGene<Integer>> codec = Codecs.ofVector( () -> RandomRegistry.random().nextInt(1000), i -> i < 100, length ); for (int i = 0; i < 100; ++i) { final Chromosome<AnyGene<Integer>> ch = codec.encoding() .newInstance().chromosome(); assertEquals(ch.length(), length); for (AnyGene<Integer> gene : ch) { assertEquals(gene.isValid(), gene.allele() < 100); if (!gene.isValid()) { Assert.assertFalse(ch.isValid()); } assertTrue(gene.allele() < 1000); assertTrue(gene.allele() >= 0); } } }