java.lang.Object
io.jenetics.engine.Codecs
This class contains factory methods for creating common problem encodings.
- Since:
- 3.2
- Version:
- 5.2
-
Method Summary
Modifier and TypeMethodDescriptionstatic <A,B> InvertibleCodec<Map<A, B>, EnumGene<Integer>> Create a codec, which creates a mapping from the elements given in thesourcesequence to the elements given in thetargetsequence.static <A,B, M extends Map<A, B>>
InvertibleCodec<M,EnumGene<Integer>> Create a codec, which creates a mapping from the elements given in thesourcesequence to the elements given in thetargetsequence.static InvertibleCodec<double[][],DoubleGene> ofMatrix(DoubleRange domain, int rows, int cols) Return a 2-dimensional matrixInvertibleCodecfor the given range.static InvertibleCodec<int[][],IntegerGene> Return a 2-dimensional matrixInvertibleCodecfor the given range.static InvertibleCodec<long[][],LongGene> Return a 2-dimensional matrixInvertibleCodecfor the given range.static InvertibleCodec<int[],EnumGene<Integer>> ofPermutation(int length) Create a permutationInvertibleCodecof integer in the range[0, length).static <T> InvertibleCodec<ISeq<T>,EnumGene<T>> ofPermutation(ISeq<? extends T> alleles) Create a permutationInvertibleCodecwith the given alleles.static InvertibleCodec<Double,DoubleGene> ofScalar(DoubleRange domain) Return a scalarInvertibleCodecfor the given range.static InvertibleCodec<Integer,IntegerGene> Return a scalarInvertibleCodecfor the given range.static InvertibleCodec<Long,LongGene> Return a scalarInvertibleCodecfor the given range.static <T> InvertibleCodec<ISeq<T>,BitGene> The subsetInvertibleCodeccan be used for problems where it is required to find the best variable-sized subset from a given basic set.static <T> InvertibleCodec<ISeq<T>,EnumGene<T>> The subsetInvertibleCodeccan be used for problems where it is required to find the best fixed-size subset from a given basic set.static InvertibleCodec<double[],DoubleGene> ofVector(DoubleRange... domains) Create a vectorInvertibleCodecfor the given ranges.static InvertibleCodec<double[],DoubleGene> ofVector(DoubleRange domain, int length) Return a vectorInvertibleCodecfor the given range.static InvertibleCodec<double[],DoubleGene> ofVector(DoubleRange domain, IntRange length) Return a vectorInvertibleCodecfor the given range.static InvertibleCodec<int[],IntegerGene> Create a vectorInvertibleCodecfor the given ranges.static InvertibleCodec<int[],IntegerGene> Return a vectorInvertibleCodecfor the given range.static InvertibleCodec<int[],IntegerGene> Return a vectorInvertibleCodecfor the given range.static InvertibleCodec<long[],LongGene> Create a vectorInvertibleCodecfor the given ranges.static InvertibleCodec<long[],LongGene> Return a vectorInvertibleCodecfor the given range.static InvertibleCodec<long[],LongGene> Return a vectorInvertibleCodecfor the given range.
-
Method Details
-
ofScalar
Return a scalarInvertibleCodecfor the given range.- Parameters:
domain- the domain of the returnedCodec- Returns:
- a new scalar
Codecwith the given domain. - Throws:
NullPointerException- if the givendomainisnull
-
ofScalar
Return a scalarInvertibleCodecfor the given range.- Parameters:
domain- the domain of the returnedCodec- Returns:
- a new scalar
Codecwith the given domain. - Throws:
NullPointerException- if the givendomainisnull
-
ofScalar
Return a scalarInvertibleCodecfor the given range.- Parameters:
domain- the domain of the returnedCodec- Returns:
- a new scalar
Codecwith the given domain. - Throws:
NullPointerException- if the givendomainisnull
-
ofScalar
public static <A> Codec<A,AnyGene<A>> ofScalar(Supplier<? extends A> supplier, Predicate<? super A> validator) Return a scalaCodecwith the given alleleSupplierand allelevalidator. Thesupplieris responsible for creating new random alleles, and thevalidatorcan verify it.The following example shows a codec which creates and verifies
BigIntegerobjects.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) );- Type Parameters:
A- the allele type- Parameters:
supplier- the allele-supplier which is used for creating new, random allelesvalidator- the validator used for validating the created gene. This predicate is used in theAnyGene.isValid()method.- Returns:
- a new
Codecwith the given parameters - Throws:
NullPointerException- if one of the parameters isnull- See Also:
-
ofScalar
Return a scalaCodecwith the given alleleSupplierand allelevalidator. Thesupplieris responsible for creating new random alleles.- Type Parameters:
A- the allele type- Parameters:
supplier- the allele-supplier which is used for creating new, random alleles- Returns:
- a new
Codecwith the given parameters - Throws:
NullPointerException- if the parameter isnull- See Also:
-
ofVector
Return a vectorInvertibleCodecfor the given range. All vector values are restricted by the same domain.- Parameters:
domain- the domain of the vector valueslength- the vector length- Returns:
- a new vector
Codec - Throws:
NullPointerException- if the givendomainisnullIllegalArgumentException- if thelengthis smaller than one.
-
ofVector
Return a vectorInvertibleCodecfor the given range. All vector values are restricted by the same domain.- Parameters:
domain- the domain of the vector valueslength- the vector length range- Returns:
- a new vector
Codec - Throws:
NullPointerException- if the givendomainisnullIllegalArgumentException- if thelengthis smaller than one.- Since:
- 7.0
-
ofVector
Return a vectorInvertibleCodecfor the given range. All vector values are restricted by the same domain.- Parameters:
domain- the domain of the vector valueslength- the vector length- Returns:
- a new vector
Codec - Throws:
NullPointerException- if the givendomainisnullIllegalArgumentException- if thelengthis smaller than one.
-
ofVector
Return a vectorInvertibleCodecfor the given range. All vector values are restricted by the same domain.- Parameters:
domain- the domain of the vector valueslength- the vector length range- Returns:
- a new vector
Codec - Throws:
NullPointerException- if the givendomainisnullIllegalArgumentException- if thelengthis smaller than one.- Since:
- 7.0
-
ofVector
Return a vectorInvertibleCodecfor the given range. All vector values are restricted by the same domain.- Parameters:
domain- the domain of the vector valueslength- the vector length- Returns:
- a new vector
Codec - Throws:
NullPointerException- if the givendomainisnullIllegalArgumentException- if thelengthis smaller than one.
-
ofVector
Return a vectorInvertibleCodecfor the given range. All vector values are restricted by the same domain.- Parameters:
domain- the domain of the vector valueslength- the vector length range- Returns:
- a new vector
Codec - Throws:
NullPointerException- if the givendomainisnullIllegalArgumentException- if thelengthis smaller than one.- Since:
- 7.0
-
ofVector
Create a vectorInvertibleCodecfor the given ranges. Each vector element might have a different domain. The vector length is equal to the number of domains.- Parameters:
domains- the domain ranges- Returns:
- a new vector
Codec - Throws:
NullPointerException- if one of the arguments isnullIllegalArgumentException- if thedomainsarray is empty
-
ofVector
Create a vectorInvertibleCodecfor the given ranges. Each vector element might have a different domain. The vector length is equal to the number of domains.- Parameters:
domains- the domain ranges- Returns:
- a new vector
Codec - Throws:
NullPointerException- if one of the arguments isnullIllegalArgumentException- if thedomainsarray is empty
-
ofVector
Create a vectorInvertibleCodecfor the given ranges. Each vector element might have a different domain. The vector length is equal to the number of domains.- Parameters:
domains- the domain ranges- Returns:
- a new vector
Codec - Throws:
NullPointerException- if one of the arguments isnullIllegalArgumentException- if thedomainsarray is empty
-
ofVector
public static <A> Codec<ISeq<A>,AnyGene<A>> ofVector(Supplier<? extends A> supplier, Predicate<? super A> alleleValidator, Predicate<? super ISeq<A>> alleleSeqValidator, int length) Return a scalaCodecwith the given alleleSupplier, allelevalidatorandChromosomelength. Thesupplieris responsible for creating new random alleles, and thevalidatorcan verify it.The following example shows a codec which creates and verifies
BigIntegerobject 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 );- Type Parameters:
A- the allele type- Parameters:
supplier- the allele-supplier which is used for creating new, random allelesalleleValidator- the validator used for validating the created gene. This predicate is used in theAnyGene.isValid()method.alleleSeqValidator- the validator used for validating the created chromosome. This predicate is used in theAnyChromosome.isValid()method.length- the vector length- Returns:
- a new
Codecwith the given parameters - Throws:
NullPointerException- if one of the parameters isnullIllegalArgumentException- if the length of the vector is smaller than one.- See Also:
-
ofVector
public static <A> Codec<ISeq<A>,AnyGene<A>> ofVector(Supplier<? extends A> supplier, Predicate<? super A> validator, int length) Return a scalaCodecwith the given alleleSupplier, allelevalidatorandChromosomelength. Thesupplieris responsible for creating new random alleles, and thevalidatorcan verify it.- Type Parameters:
A- the allele type- Parameters:
supplier- the allele-supplier which is used for creating new, random allelesvalidator- the validator used for validating the created gene. This predicate is used in theAnyGene.isValid()method.length- the vector length- Returns:
- a new
Codecwith the given parameters - Throws:
NullPointerException- if one of the parameters isnullIllegalArgumentException- if the length of the vector is smaller than one.
-
ofVector
Return a scalaCodecwith the given alleleSupplierandChromosomelength. Thesupplieris responsible for creating new random alleles.- Type Parameters:
A- the allele type- Parameters:
supplier- the allele-supplier which is used for creating new, random alleleslength- the vector length- Returns:
- a new
Codecwith the given parameters - Throws:
NullPointerException- if one of the parameters isnullIllegalArgumentException- if the length of the vector is smaller than one.
-
ofPermutation
Create a permutationInvertibleCodecof integer in the range[0, length).- Parameters:
length- the number of permutation elements- Returns:
- a permutation
Codecof integers - Throws:
IllegalArgumentException- if thelengthis smaller than one.
-
ofPermutation
Create a permutationInvertibleCodecwith the given alleles.- Type Parameters:
T- the allele type- Parameters:
alleles- the alleles of the permutation- Returns:
- a new permutation
Codec - Throws:
IllegalArgumentException- if the given allele array is emptyNullPointerException- if one of the alleles isnull
-
ofMatrix
Return a 2-dimensional matrixInvertibleCodecfor the given range. All matrix values are restricted by the same domain. The dimension of the returned matrix isint[rows][cols].- Parameters:
domain- the domain of the matrix valuesrows- the number of rows of the matrixcols- the number of columns of the matrix- Returns:
- a new matrix
Codec - Throws:
NullPointerException- if the givendomainisnullIllegalArgumentException- if therowsorcolsare smaller than one.- Since:
- 4.4
-
ofMatrix
Return a 2-dimensional matrixInvertibleCodecfor the given range. All matrix values are restricted by the same domain. The dimension of the returned matrix islong[rows][cols].- Parameters:
domain- the domain of the matrix valuesrows- the number of rows of the matrixcols- the number of columns of the matrix- Returns:
- a new matrix
Codec - Throws:
NullPointerException- if the givendomainisnullIllegalArgumentException- if therowsorcolsare smaller than one.- Since:
- 4.4
-
ofMatrix
public static InvertibleCodec<double[][],DoubleGene> ofMatrix(DoubleRange domain, int rows, int cols) Return a 2-dimensional matrixInvertibleCodecfor the given range. All matrix values are restricted by the same domain. The dimension of the returned matrix isdouble[rows][cols].- Parameters:
domain- the domain of the matrix valuesrows- the number of rows of the matrixcols- the number of columns of the matrix- Returns:
- a new matrix
Codec - Throws:
NullPointerException- if the givendomainisnullIllegalArgumentException- if therowsorcolsare smaller than one.- Since:
- 4.4
-
ofMapping
public static <A,B, InvertibleCodec<M,M extends Map<A, B>> EnumGene<Integer>> ofMapping(ISeq<? extends A> source, ISeq<? extends B> target, Supplier<M> mapSupplier) Create a codec, which creates a mapping from the elements given in thesourcesequence to the elements given in thetargetsequence. The returned mapping can be seen as a function which maps every element of thetargetset to an element of thesourceset.Iffinal 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, ifsource.size() < target.size(), the mapping is injective and if both sets have the same size, the returned mapping is bijective.- Type Parameters:
A- the type of the source elementsB- the type of the target elementsM- the type of the encoded Map- Parameters:
source- the source elements. Will be the keys of the encodedMap.target- the target elements. Will be the values of the encodedMap.mapSupplier- a function which returns a new, empty Map into which the mapping will be inserted- Returns:
- a new mapping codec
- Throws:
IllegalArgumentException- if thetargetsequences are emptyNullPointerException- if one of the arguments isnull- Since:
- 4.3
-
ofMapping
public static <A,B> InvertibleCodec<Map<A,B>, ofMappingEnumGene<Integer>> (ISeq<? extends A> source, ISeq<? extends B> target) Create a codec, which creates a mapping from the elements given in thesourcesequence to the elements given in thetargetsequence. The returned mapping can be seen as a function which maps every element of thetargetset to an element of thesourceset.Iffinal 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, ifsource.size() < target.size(), the mapping is injective and if both sets have the same size, the returned mapping is bijective.- Type Parameters:
A- the type of the source elementsB- the type of the target elements- Parameters:
source- the source elements. Will be the keys of the encodedMap.target- the target elements. Will be the values of the encodedMap.- Returns:
- a new mapping codec
- Throws:
IllegalArgumentException- if thetargetsequences are emptyNullPointerException- if one of the arguments isnull- Since:
- 4.3
-
ofSubSet
The subsetInvertibleCodeccan be used for problems where it is required to find the best variable-sized subset from a given basic set. A typical usage example of the returnedCodecis 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(); ... } }- Type Parameters:
T- the element type of the basic set- Parameters:
basicSet- the basic set, from where to choose the optimal subset.- Returns:
- a new codec which can be used for modelling subset problems.
- Throws:
NullPointerException- if the givenbasicSetisnull;nullelements are allowed.IllegalArgumentException- if thebasicSetsize is smaller than one.
-
ofSubSet
public static <T> InvertibleCodec<ISeq<T>,EnumGene<T>> ofSubSet(ISeq<? extends T> basicSet, int size) The subsetInvertibleCodeccan be used for problems where it is required to find the best fixed-size subset from a given basic set.- Type Parameters:
T- the element type of the basic set- Parameters:
basicSet- the basic set, from where to choose the optimal subset.size- the length of the desired subsets- Returns:
- a new codec which can be used for modelling subset problems.
- Throws:
NullPointerException- if the givenbasicSetisnull;nullelements are allowed.IllegalArgumentException- ifbasicSet.size() < size,size <= 0orbasicSet.size()*sizewill cause an integer overflow.- Since:
- 3.4
- See Also:
-