@Test public void ofSubSet() { final Codec<ISeq<String>, EnumGene<String>> codec = Codecs.ofSubSet( ISeq.of("1", "2", "3", "4", "5"), 3 ); for (int i = 0; i < 100; ++i) { final Genotype<EnumGene<String>> gt = codec.encoding().newInstance(); final Chromosome<EnumGene<String>> ch = gt.chromosome(); assertEquals(ch.length(), 3); assertTrue(ch.isValid()); final ISeq<String> permutation = codec.decoder().apply(gt); assertEquals(permutation.length(), 3); } }
public static <T> InvertibleCodec<ISeq<T>, BitGene> ofSubSet(final ISeq<? extends T> basicSet) { requireNonNull(basicSet); Requires.positive(basicSet.length()); return InvertibleCodec.of( Genotype.of(BitChromosome.of(basicSet.length())), gt -> gt.chromosome() .as(BitChromosome.class).ones() .<T>mapToObj(basicSet) .collect(ISeq.toISeq()), values -> { final byte[] bits = Bits.newArray(basicSet.size()); int i = 0; for (T v : values) { while (i < basicSet.size() && !Objects.equals(basicSet.get(i), v)) { ++i; } Bits.set(bits, i); } return Genotype.of(new BitChromosome(bits, 0, basicSet.size())); } ); }
Codecs { public static <T> InvertibleCodec<ISeq<T>, BitGene> ofSubSet(final ISeq<? extends T> basicSet) { requireNonNull(basicSet); Requires.positive(basicSet.length()); return InvertibleCodec.of( Genotype.of(BitChromosome.of(basicSet.length())), gt -> gt.chromosome() .as(BitChromosome.class).ones() .<T>mapToObj(basicSet) .collect(ISeq.toISeq()), values -> { final byte[] bits = Bits.newArray(basicSet.size()); int i = 0; for (T v : values) { while (i < basicSet.size() && !Objects.equals(basicSet.get(i), v)) { ++i; } Bits.set(bits, i); } return Genotype.of(new BitChromosome(bits, 0, basicSet.size())); } ); } }
Codecs { public static <T> InvertibleCodec<ISeq<T>, BitGene> ofSubSet(final ISeq<? extends T> basicSet) { requireNonNull(basicSet); Requires.positive(basicSet.length()); return InvertibleCodec.of( Genotype.of(BitChromosome.of(basicSet.length())), gt -> gt.chromosome() .as(BitChromosome.class).ones() .<T>mapToObj(basicSet) .collect(ISeq.toISeq()), values -> { final byte[] bits = Bits.newArray(basicSet.size()); int i = 0; for (T v : values) { while (i < basicSet.size() && !Objects.equals(basicSet.get(i), v)) { ++i; } Bits.set(bits, i); } return Genotype.of(new BitChromosome(bits, 0, basicSet.size())); } ); } private  Codecs(); }
Codecs { public static <T> InvertibleCodec<ISeq<T>, BitGene> ofSubSet(final ISeq<? extends T> basicSet) { requireNonNull(basicSet); Requires.positive(basicSet.length()); return InvertibleCodec.of( Genotype.of(BitChromosome.of(basicSet.length())), gt -> gt.chromosome() .as(BitChromosome.class).ones() .<T>mapToObj(basicSet) .collect(ISeq.toISeq()), values -> { final byte[] bits = Bits.newArray(basicSet.size()); int i = 0; for (T v : values) { while (i < basicSet.size() && !Objects.equals(basicSet.get(i), v)) { ++i; } Bits.set(bits, i); } return Genotype.of(new BitChromosome(bits, 0, basicSet.size())); } ); } 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
	); }
Codecs { public static <T> InvertibleCodec<ISeq<T>, BitGene> ofSubSet(final ISeq<? extends T> basicSet) { requireNonNull(basicSet); Requires.positive(basicSet.length()); return InvertibleCodec.of( Genotype.of(BitChromosome.of(basicSet.length())), gt -> gt.chromosome() .as(BitChromosome.class).ones() .<T>mapToObj(basicSet) .collect(ISeq.toISeq()), values -> { final byte[] bits = Bits.newArray(basicSet.size()); int i = 0; for (T v : values) { while (i < basicSet.size() && !Objects.equals(basicSet.get(i), v)) { ++i; } Bits.set(bits, i); } return Genotype.of(new BitChromosome(bits, 0, basicSet.size())); } ); } 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
	);  }