T
- the argument type of a given problemG
- the Gene
type used for encoding the argument type T
public interface InvertibleCodec<T,G extends Gene<?,G>> extends Codec<T,G>
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
.Constraint.of(InvertibleCodec, Predicate, Function)
,
Constraint.of(InvertibleCodec, Predicate, Function)
,
RetryConstraint.of(InvertibleCodec, Predicate, int)
Modifier and Type | Method and Description |
---|---|
default Genotype<G> |
encode(T value)
Decodes the given
value , which is an element of the native
problem domain, into a Genotype . |
Function<T,Genotype<G>> |
encoder()
Return the encoder function which transforms a value from the
native problem domain back to the genotype.
|
default <B> InvertibleCodec<B,G> |
map(Function<? super T,? extends B> mapper,
Function<? super B,? extends T> inverseMapper)
Create a new
InvertibleCodec with the mapped result type. |
static <T,G extends Gene<?,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.
|
Function<T,Genotype<G>> encoder()
Codec.decoder()
function. The following code
snippet shows how a given value in the native problem domain
can be converted into a Genotype
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));
Codec.decoder()
,
encode(Object)
default Genotype<G> encode(T value)
value
, which is an element of the native
problem domain, into a Genotype
.value
- the value of the native problem domainvalue
default <B> InvertibleCodec<B,G> map(Function<? super T,? extends B> mapper, Function<? super B,? extends T> inverseMapper)
InvertibleCodec
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());
B
- the new argument type of the given problemmapper
- the mapper functioninverseMapper
- the inverse function of the mapper
InvertibleCodec
with the mapped result typeNullPointerException
- if one the mapper is null
.Codec.map(Function)
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)
G
- the Gene
typeT
- the fitness function argument type in the problem domainencoding
- the genotype factory used for creating new
Genotypes
decoder
- decoder function, which converts a Genotype
to a
value in the problem domain.encoder
- encoder function, which converts a value of the problem
domain into a Genotype
InvertibleCodec
object with the given parameters.NullPointerException
- if one of the arguments is null
.© 2007-2020 Franz Wilhelmstötter (2020-02-18 20:01)