public final class Codecs extends Object
Modifier and Type | Method and Description |
---|---|
static <A,B> Codec<Map<A,B>,EnumGene<Integer>> |
ofMapping(ISeq<? extends A> source,
ISeq<? extends B> target)
Create a codec, which creates a a mapping from the elements given in the
source sequence to the elements given in the target
sequence. |
static <A,B,M extends Map<A,B>> |
ofMapping(ISeq<? extends A> source,
ISeq<? extends B> target,
Supplier<M> mapSupplier)
Create a codec, which creates a a mapping from the elements given in the
source sequence to the elements given in the target
sequence. |
static Codec<double[][],DoubleGene> |
ofMatrix(DoubleRange domain,
int rows,
int cols)
Return a 2-dimensional matrix
Codec for the given range. |
static Codec<int[][],IntegerGene> |
ofMatrix(IntRange domain,
int rows,
int cols)
Return a 2-dimensional matrix
Codec for the given range. |
static Codec<long[][],LongGene> |
ofMatrix(LongRange domain,
int rows,
int cols)
Return a 2-dimensional matrix
Codec for the given range. |
static Codec<int[],EnumGene<Integer>> |
ofPermutation(int length)
Create a permutation
Codec of integer in the range
[0, length) . |
static <T> Codec<ISeq<T>,EnumGene<T>> |
ofPermutation(ISeq<? extends 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<ISeq<A>,AnyGene<A>> |
ofVector(Supplier<? extends A> supplier,
int length)
|
static <A> Codec<ISeq<A>,AnyGene<A>> |
ofVector(Supplier<? extends A> supplier,
Predicate<? super A> validator,
int length)
|
static <A> Codec<ISeq<A>,AnyGene<A>> |
ofVector(Supplier<? extends A> supplier,
Predicate<? super A> alleleValidator,
Predicate<? super ISeq<A>> alleleSeqValidator,
int length)
|
public static Codec<Integer,IntegerGene> ofScalar(IntRange domain)
Codec
for the given range.domain
- the domain of the returned Codec
Codec
with the given domain.NullPointerException
- if the given domain
is null
public static Codec<Long,LongGene> ofScalar(LongRange domain)
Codec
for the given range.domain
- the domain of the returned Codec
Codec
with the given domain.NullPointerException
- if the given domain
is null
public static Codec<Double,DoubleGene> ofScalar(DoubleRange domain)
Codec
for the given range.domain
- the domain of the returned Codec
Codec
with the given domain.NullPointerException
- if the given domain
is null
public 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 null
AnyGene.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 null
ofScalar(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 lengthCodec
NullPointerException
- if the given domain
is null
IllegalArgumentException
- 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 lengthCodec
NullPointerException
- if the given domain
is null
IllegalArgumentException
- 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 lengthCodec
NullPointerException
- if the given domain
is null
IllegalArgumentException
- 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 rangesCodec
NullPointerException
- if one of the arguments is null
IllegalArgumentException
- 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 rangesCodec
NullPointerException
- if one of the arguments is null
IllegalArgumentException
- 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 rangesCodec
NullPointerException
- if one of the arguments is null
IllegalArgumentException
- if the domains
array is emptypublic static <A> Codec<ISeq<A>,AnyGene<A>> ofVector(Supplier<? extends A> supplier, Predicate<? super A> alleleValidator, Predicate<? super ISeq<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);
},
// 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 allelesalleleValidator
- 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 null
IllegalArgumentException
- if the length of the vector is smaller
than one.AnyChromosome.of(Supplier, Predicate, Predicate, int)
public static <A> Codec<ISeq<A>,AnyGene<A>> ofVector(Supplier<? extends A> supplier, 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 allelesvalidator
- 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 null
IllegalArgumentException
- if the length of the vector is smaller
than one.public static <A> Codec<ISeq<A>,AnyGene<A>> ofVector(Supplier<? extends A> supplier, 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 alleleslength
- the vector lengthCodec
with the given parametersNullPointerException
- if one of the parameters is null
IllegalArgumentException
- 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.public static Codec<int[][],IntegerGene> ofMatrix(IntRange domain, int rows, int cols)
Codec
for the given range. All
matrix values are restricted by the same domain. The dimension of the
returned matrix is int[rows][cols]
.domain
- the domain of the matrix valuesrows
- the number of rows of the matrixcols
- the number of columns of the matrixCodec
NullPointerException
- if the given domain
is null
IllegalArgumentException
- if the rows
or cols
are
smaller than one.public static Codec<long[][],LongGene> ofMatrix(LongRange domain, int rows, int cols)
Codec
for the given range. All
matrix values are restricted by the same domain. The dimension of the
returned matrix is long[rows][cols]
.domain
- the domain of the matrix valuesrows
- the number of rows of the matrixcols
- the number of columns of the matrixCodec
NullPointerException
- if the given domain
is null
IllegalArgumentException
- if the rows
or cols
are
smaller than one.public static Codec<double[][],DoubleGene> ofMatrix(DoubleRange domain, int rows, int cols)
Codec
for the given range. All
matrix values are restricted by the same domain. The dimension of the
returned matrix is double[rows][cols]
.domain
- the domain of the matrix valuesrows
- the number of rows of the matrixcols
- the number of columns of the matrixCodec
NullPointerException
- if the given domain
is null
IllegalArgumentException
- if the rows
or cols
are
smaller than one.public static <T> Codec<ISeq<T>,EnumGene<T>> ofPermutation(ISeq<? extends T> alleles)
Codec
with the given alleles.T
- the allele typealleles
- the alleles of the permutationCodec
IllegalArgumentException
- if the given allele array is emptyNullPointerException
- if one of the alleles is null
public static <A,B,M extends Map<A,B>> Codec<M,EnumGene<Integer>> ofMapping(ISeq<? extends A> source, ISeq<? extends B> target, Supplier<M> mapSupplier)
source
sequence to the elements given in the target
sequence. The returned mapping can be seen as a function which maps every
element of the target
set to an element of the source
set.
final ISeq<Integer> numbers = ISeq.of(1, 2, 3, 4, 5);
final ISeq<String> strings = ISeq.of("1", "2", "3");
final Codec<Map<Integer, String>, EnumGene<Integer>> codec =
Codecs.ofMapping(numbers, strings, HashMap::new);
source.size() > target.size()
, the created mapping is
surjective,
if source.size() < target.size()
, the mapping is
injective
and if both sets have the same size, the returned mapping is
bijective.A
- the type of the source elementsB
- the type of the target elementsM
- the type of the encoded Mapsource
- the source elements. Will be the keys of the
encoded Map
.target
- the target elements. Will be the values of the
encoded Map
.mapSupplier
- a function which returns a new, empty Map into which
the mapping will be insertedIllegalArgumentException
- if the target
sequences are emptyNullPointerException
- if one of the argument is null
public static <A,B> Codec<Map<A,B>,EnumGene<Integer>> ofMapping(ISeq<? extends A> source, ISeq<? extends B> target)
source
sequence to the elements given in the target
sequence. The returned mapping can be seen as a function which maps every
element of the target
set to an element of the source
set.
final ISeq<Integer> numbers = ISeq.of(1, 2, 3, 4, 5);
final ISeq<String> strings = ISeq.of("1", "2", "3");
final Codec<Map<Integer, String>, EnumGene<Integer>> codec =
Codecs.ofMapping(numbers, strings);
source.size() > target.size()
, the created mapping is
surjective,
if source.size() < target.size()
, the mapping is
injective
and if both sets have the same size, the returned mapping is
bijective.A
- the type of the source elementsB
- the type of the target elementssource
- the source elements. Will be the keys of the
encoded Map
.target
- the target elements. Will be the values of the
encoded Map
.IllegalArgumentException
- if the target
sequences are emptyNullPointerException
- if one of the argument is null
public 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-2019 Franz Wilhelmstötter (2019-06-23 15:39)