- Type Parameters:
T
- the argument type of given problemG
- theGene
type used for encoding the argument typeT
- All Superinterfaces:
Codec<T,
G>
This interface extends the
The main usage of an invertible codec is to simplify the definition of
Codec
and allows to encode an object from
the problem space to a corresponding Genotype
, which is the
inverse functionality of the codec. The following example shows the
relation between encoder and decoder function must fulfill.
final InvertibleCodec<int[], IntegerGene> codec =
Codecs.ofVector(IntRange.of(0, 100), 6);
final int[] value = new int[]{3, 4, 6, 7, 8, 3};
final Genotype<IntegerGene> gt = codec.encode(value);
assert Arrays.equals(value, codec.decode(gt));
Constraint
objects. Instead of working with the GA classes
(Phenotype
or Genotype
), it is possible to work
in the native problem domain T
.- Since:
- 5.2
- Version:
- 5.2
- See Also:
-
Method Summary
Modifier and TypeMethodDescriptionDecodes the givenvalue
, which is an element of the native problem domain, into aGenotype
.encoder()
Return the encoder function which transforms a value from the native problem domain back to the genotype.default <B> InvertibleCodec
<B, G> Create a newInvertibleCodec
with the mapped result type.static <T,
G extends Gene<?, G>>
InvertibleCodec<T, G> of
(Factory<Genotype<G>> encoding, Function<? super Genotype<G>, ? extends T> decoder, Function<? super T, Genotype<G>> encoder) Create a new invertible codec from the given parameters.
-
Method Details
-
encoder
Return the encoder function which transforms a value from the native problem domain back to the genotype. This is the inverse of theCodec.decoder()
function. The following code snippet shows how a given value in the native problem domain can be converted into aGenotype
and transformed back.final InvertibleCodec<int[], IntegerGene> codec = Codecs.ofVector(IntRange.of(0, 100), 6); final int[] value = new int[]{3, 4, 6, 7, 8, 3}; final Genotype<IntegerGene> gt = codec.encode(value); assert Arrays.equals(value, codec.decode(gt));
- Returns:
- value encoder function
- See Also:
-
encode
Decodes the givenvalue
, which is an element of the native problem domain, into aGenotype
.- Parameters:
value
- the value of the native problem domain- Returns:
- the genotype, which represents the given
value
-
map
default <B> InvertibleCodec<B,G> map(Function<? super T, ? extends B> mapper, Function<? super B, ? extends T> inverseMapper) Create a newInvertibleCodec
with the mapped result type. This method can also be used for creating non-trivial codes like split ranges, as shown in the following example, where only values between [0, 2) and [8, 10) are valid.+--+--+--+--+--+--+--+--+--+--+ | | | | | | | | | | | 0 1 2 3 4 5 6 7 8 9 10 |-----|xxxxxxxxxxxxxxxxx|-----| ^ |llllllll|rrrrrrrr| ^ | | | | +-------+ +------+
final InvertibleCodec<Double, DoubleGene> codec = Codecs .ofScalar(DoubleRange.of(0, 10)) .map(v -> { if (v >= 2 && v < 8) { return v < 5 ? ((v - 2)/3)*2 : ((8 - v)/3)*2 + 8; } return v; }, Function.identity());
- Type Parameters:
B
- the new argument type of the given problem- Parameters:
mapper
- the mapper functioninverseMapper
- the inverse function of themapper
- Returns:
- a new
InvertibleCodec
with the mapped result type - Throws:
NullPointerException
- if one the mapper isnull
.- See Also:
-
of
static <T,G extends Gene<?, InvertibleCodec<T,G>> G> of(Factory<Genotype<G>> encoding, Function<? super Genotype<G>, ? extends T> decoder, Function<? super T, Genotype<G>> encoder) Create a new invertible codec from the given parameters.- Type Parameters:
T
- the fitness function argument type in the problem domainG
- theGene
type- Parameters:
encoding
- the genotype factory used for creating newGenotypes
decoder
- decoder function, which converts aGenotype
to a value in the problem domain.encoder
- encoder function, which converts a value of the problem domain into aGenotype
- Returns:
- a new
InvertibleCodec
object with the given parameters. - Throws:
NullPointerException
- if one of the arguments isnull
.
-