Codecs { public static InvertibleCodec<Integer, IntegerGene> ofScalar(final IntRange domain) { requireNonNull(domain); return InvertibleCodec.of( Genotype.of(IntegerChromosome.of(domain)), gt -> gt.chromosome().gene().allele(), val -> Genotype.of(IntegerChromosome.of(IntegerGene.of(val, domain))) ); } 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 = "intScalarData") public void ofIntScalar(final IntRange domain) { final Codec<Integer, IntegerGene> codec = Codecs.ofScalar(domain); final Genotype<IntegerGene> gt = codec.encoding().newInstance(); assertEquals(gt.length(), 1); assertEquals(gt.chromosome().length(), 1); assertEquals(gt.gene().min().intValue(), domain.min()); assertEquals(gt.gene().max().intValue(), domain.max()); final Function<Genotype<IntegerGene>, Integer> f = codec.decoder(); assertEquals(f.apply(gt).intValue(), gt.gene().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(); assertEquals(gt.length(), 1); assertEquals(gt.chromosome().length(), 1); assertEquals(gt.gene().min().longValue(), domain.min()); assertEquals(gt.gene().max().longValue(), domain.max()); final Function<Genotype<LongGene>, Long> f = codec.decoder(); assertEquals(f.apply(gt).longValue(), gt.gene().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(); assertEquals(gt.length(), 1); assertEquals(gt.chromosome().length(), 1); assertEquals(gt.gene().min().doubleValue(), domain.min()); assertEquals(gt.gene().max().doubleValue(), domain.max()); final Function<Genotype<DoubleGene>, Double> f = codec.decoder(); assertEquals(f.apply(gt).doubleValue(), gt.gene().doubleValue()); }
@Test public void ofAnyScalar() { final Codec<Integer, AnyGene<Integer>> codec = Codecs.ofScalar( () -> RandomRegistry.random().nextInt(1000), i -> i < 100 ); for (int i = 0; i < 1000; ++i) { final AnyGene<Integer> gene = codec.encoding() .newInstance().gene(); assertEquals(gene.isValid(), gene.allele() < 100); assertTrue(gene.allele() < 1000); assertTrue(gene.allele() >= 0); } }
@Test public void ofAnyScalar2() { final Codec<Integer, AnyGene<Integer>> codec = Codecs.ofScalar( () -> RandomRegistry.random().nextInt(1000) ); for (int i = 0; i < 1000; ++i) { final AnyGene<Integer> gene = codec.encoding() .newInstance().gene(); assertTrue(gene.isValid()); assertTrue(gene.allele() < 1000); assertTrue(gene.allele() >= 0); } }