public final class codecs extends Object
| Modifier and Type | Method and Description |
|---|---|
static Codec<int[],EnumGene<Integer>> |
ofPermutation(int length)
Create a permutation
Codec of integer in the range
[0, length). |
static <T> Codec<T[],EnumGene<T>> |
ofPermutation(T... alleles)
Create a permutation
Codec with the given alleles. |
static Codec<Double,DoubleGene> |
ofScalar(DoubleRange domain)
Return a scalar
Codec for the given range. |
static Codec<Integer,IntegerGene> |
ofScalar(IntRange domain)
Return a scalar
Codec for the given range. |
static Codec<Long,LongGene> |
ofScalar(LongRange domain)
Return a scalar
Codec for the given range. |
static <A> Codec<A,AnyGene<A>> |
ofScalar(Supplier<? extends A> supplier)
|
static <A> Codec<A,AnyGene<A>> |
ofScalar(Supplier<? extends A> supplier,
Predicate<? super A> validator)
|
static <T> Codec<ISeq<T>,BitGene> |
ofSubSet(ISeq<? extends T> basicSet)
The subset
Codec can be used for problems where it is required to
find the best variable-sized subset from given basic set. |
static <T> Codec<ISeq<T>,EnumGene<T>> |
ofSubSet(ISeq<? extends T> basicSet,
int size)
The subset
Codec can be used for problems where it is required to
find the best fixed-size subset from given basic set. |
static Codec<double[],DoubleGene> |
ofVector(DoubleRange... domains)
Create a vector
Codec for the given ranges. |
static Codec<double[],DoubleGene> |
ofVector(DoubleRange domain,
int length)
Return a vector
Codec for the given range. |
static Codec<int[],IntegerGene> |
ofVector(IntRange... domains)
Create a vector
Codec for the given ranges. |
static Codec<int[],IntegerGene> |
ofVector(IntRange domain,
int length)
Return a vector
Codec for the given range. |
static Codec<long[],LongGene> |
ofVector(LongRange... domains)
Create a vector
Codec for the given ranges. |
static Codec<long[],LongGene> |
ofVector(LongRange domain,
int length)
Return a vector
Codec for the given range. |
static <A> Codec<A[],AnyGene<A>> |
ofVector(Supplier<? extends A> supplier,
IntFunction<A[]> generator,
int length)
|
static <A> Codec<A[],AnyGene<A>> |
ofVector(Supplier<? extends A> supplier,
IntFunction<A[]> generator,
Predicate<? super A> validator,
int length)
|
static <A> Codec<A[],AnyGene<A>> |
ofVector(Supplier<? extends A> supplier,
IntFunction<A[]> generator,
Predicate<? super A> alleleValidator,
Predicate<? super ISeq<? super A>> alleleSeqValidator,
int length)
|
public static Codec<Integer,IntegerGene> ofScalar(IntRange domain)
Codec for the given range.domain - the domain of the returned CodecCodec with the given domain.NullPointerException - if the given domain is nullpublic static Codec<Long,LongGene> ofScalar(LongRange domain)
Codec for the given range.domain - the domain of the returned CodecCodec with the given domain.NullPointerException - if the given domain is nullpublic static Codec<Double,DoubleGene> ofScalar(DoubleRange domain)
Codec for the given range.domain - the domain of the returned CodecCodec with the given domain.NullPointerException - if the given domain is nullpublic static <A> Codec<A,AnyGene<A>> ofScalar(Supplier<? extends A> supplier, Predicate<? super A> validator)
Codec with the given allele Supplier and
allele validator. The supplier is responsible for
creating new random alleles, and the validator can verify it.
The following example shows a codec which creates and verifies
BigInteger objects.
final Codec<BigInteger, AnyGene<BigInteger>> codec = codecs.of(
// Create new random 'BigInteger' object.
() -> {
final byte[] data = new byte[100];
RandomRegistry.getRandom().nextBytes(data);
return new BigInteger(data);
},
// Verify that bit 7 is set. (For illustration purpose.)
bi -> bi.testBit(7)
);A - the allele typesupplier - the allele-supplier which is used for creating new,
random allelesvalidator - the validator used for validating the created gene. This
predicate is used in the AnyGene.isValid() method.Codec with the given parametersNullPointerException - if one of the parameters is nullAnyGene.of(Supplier, Predicate),
AnyChromosome.of(Supplier, Predicate)public static <A> Codec<A,AnyGene<A>> ofScalar(Supplier<? extends A> supplier)
Codec with the given allele Supplier and
allele validator. The supplier is responsible for
creating new random alleles.A - the allele typesupplier - the allele-supplier which is used for creating new,
random allelesCodec with the given parametersNullPointerException - if the parameter is nullofScalar(Supplier, Predicate),
AnyGene.of(Supplier),
AnyChromosome.of(Supplier)public static Codec<int[],IntegerGene> ofVector(IntRange domain, int length)
Codec for the given range. All vector values
are restricted by the same domain.domain - the domain of the vector valueslength - the vector lengthCodecNullPointerException - if the given domain is nullIllegalArgumentException - if the length is smaller than
one.public static Codec<long[],LongGene> ofVector(LongRange domain, int length)
Codec for the given range. All vector values
are restricted by the same domain.domain - the domain of the vector valueslength - the vector lengthCodecNullPointerException - if the given domain is nullIllegalArgumentException - if the length is smaller than
one.public static Codec<double[],DoubleGene> ofVector(DoubleRange domain, int length)
Codec for the given range. All vector values
are restricted by the same domain.domain - the domain of the vector valueslength - the vector lengthCodecNullPointerException - if the given domain is nullIllegalArgumentException - if the length is smaller than
one.public static Codec<int[],IntegerGene> ofVector(IntRange... domains)
Codec for the given ranges. Each vector element
might have a different domain. The vector length is equal to the number
of domains.domains - the domain rangesCodecNullPointerException - if one of the arguments is nullIllegalArgumentException - if the domains array is emptypublic static Codec<long[],LongGene> ofVector(LongRange... domains)
Codec for the given ranges. Each vector element
might have a different domain. The vector length is equal to the number
of domains.domains - the domain rangesCodecNullPointerException - if one of the arguments is nullIllegalArgumentException - if the domains array is emptypublic static Codec<double[],DoubleGene> ofVector(DoubleRange... domains)
Codec for the given ranges. Each vector element
might have a different domain. The vector length is equal to the number
of domains.domains - the domain rangesCodecNullPointerException - if one of the arguments is nullIllegalArgumentException - if the domains array is emptypublic static <A> Codec<A[],AnyGene<A>> ofVector(Supplier<? extends A> supplier, IntFunction<A[]> generator, Predicate<? super A> alleleValidator, Predicate<? super ISeq<? super A>> alleleSeqValidator, int length)
Codec with the given allele Supplier,
allele validator and Chromosome length. The
supplier is responsible for creating new random alleles, and the
validator can verify it.
The following example shows a codec which creates and verifies
BigInteger object arrays.
final Codec<BigInteger[], AnyGene<BigInteger>> codec = codecs.of(
// Create new random 'BigInteger' object.
() -> {
final byte[] data = new byte[100];
RandomRegistry.getRandom().nextBytes(data);
return new BigInteger(data);
},
// The array generator.
BigInteger[]::new,
// Verify that bit 7 is set. (For illustration purpose.)
bi -> bi.testBit(7),
// The 'Chromosome' length.
123
);A - the allele typesupplier - the allele-supplier which is used for creating new,
random allelesgenerator - the array generator used for generating arrays of type
AalleleValidator - the validator used for validating the created gene.
This predicate is used in the AnyGene.isValid() method.alleleSeqValidator - the validator used for validating the created
chromosome. This predicate is used in the
AnyChromosome.isValid() method.length - the vector lengthCodec with the given parametersNullPointerException - if one of the parameters is nullIllegalArgumentException - if the length of the vector is smaller
than one.AnyChromosome.of(Supplier, Predicate, Predicate, int)public static <A> Codec<A[],AnyGene<A>> ofVector(Supplier<? extends A> supplier, IntFunction<A[]> generator, Predicate<? super A> validator, int length)
Codec with the given allele Supplier,
allele validator and Chromosome length. The
supplier is responsible for creating new random alleles, and the
validator can verify it.A - the allele typesupplier - the allele-supplier which is used for creating new,
random allelesgenerator - the array generator used for generating arrays of type
Avalidator - the validator used for validating the created gene. This
predicate is used in the AnyGene.isValid() method.length - the vector lengthCodec with the given parametersNullPointerException - if one of the parameters is nullIllegalArgumentException - if the length of the vector is smaller
than one.public static <A> Codec<A[],AnyGene<A>> ofVector(Supplier<? extends A> supplier, IntFunction<A[]> generator, int length)
Codec with the given allele Supplier and
Chromosome length. The supplier is responsible for
creating new random alleles.A - the allele typesupplier - the allele-supplier which is used for creating new,
random allelesgenerator - the array generator used for generating arrays of type
Alength - the vector lengthCodec with the given parametersNullPointerException - if one of the parameters is nullIllegalArgumentException - if the length of the vector is smaller
than one.public static Codec<int[],EnumGene<Integer>> ofPermutation(int length)
Codec of integer in the range
[0, length).length - the number of permutation elementsCodec of integersIllegalArgumentException - if the length is smaller than
one.@SafeVarargs public static <T> Codec<T[],EnumGene<T>> ofPermutation(T... alleles)
Codec with the given alleles.T - the allele typealleles - the alleles of the permutationCodecIllegalArgumentException - if the given allele array is emptyNullPointerException - if one of the alleles is nullpublic static <T> Codec<ISeq<T>,BitGene> ofSubSet(ISeq<? extends T> basicSet)
Codec can be used for problems where it is required to
find the best variable-sized subset from given basic set. A typical
usage example of the returned Codec is the Knapsack problem.
The following code snippet shows a simplified variation of the Knapsack problem.
public final class Main {
// The basic set from where to choose an 'optimal' subset.
private final static ISeq<Integer> SET =
ISeq.of(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
// Fitness function directly takes an 'int' value.
private static int fitness(final ISeq<Integer> subset) {
assert(subset.size() <= SET.size());
final int size = subset.stream()
.collect(Collectors.summingInt(Integer::intValue));
return size <= 20 ? size : 0;
}
public static void main(final String[] args) {
final Engine<BitGene, Double> engine = Engine
.builder(Main::fitness, codec.ofSubSet(SET))
.build();
...
}
}T - the element type of the basic setbasicSet - the basic set, from where to choose the optimal
subset.NullPointerException - if the given basicSet is
null; null elements are allowed.IllegalArgumentException - if the basicSet size is smaller
than one.public static <T> Codec<ISeq<T>,EnumGene<T>> ofSubSet(ISeq<? extends T> basicSet, int size)
Codec can be used for problems where it is required to
find the best fixed-size subset from given basic set.T - the element type of the basic setbasicSet - the basic set, from where to choose the optimal
subset.size - the length of the desired subsetsNullPointerException - if the given basicSet is
null; null elements are allowed.IllegalArgumentException - if basicSet.size() < size,
size <= 0 or basicSet.size()*size will cause an
integer overflow.PermutationChromosome,
PermutationChromosome.of(ISeq, int)© 2007-2016 Franz Wilhelmstötter (2016-04-24 10:25)