Interface InvertibleCodec<T,G extends Gene<?,G>>

Type Parameters:
T - the argument type of given problem
G - the Gene type used for encoding the argument type T
All Superinterfaces:
Codec<T,G>

public interface InvertibleCodec<T,G extends Gene<?,G>> extends Codec<T,G>
This interface extends the 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));
The main usage of an invertible codec is to simplify the definition of 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 Details

    • encoder

      Return the encoder function which transforms a value from the native problem domain back to the genotype. This is the inverse of the 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));
      
      Returns:
      value encoder function
      See Also:
    • encode

      default Genotype<G> encode(T value)
      Decodes the given value, which is an element of the native problem domain, into a Genotype.
      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 new 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());
      
      Type Parameters:
      B - the new argument type of the given problem
      Parameters:
      mapper - the mapper function
      inverseMapper - the inverse function of the mapper
      Returns:
      a new InvertibleCodec with the mapped result type
      Throws:
      NullPointerException - if one the mapper is null.
      See Also:
    • of

      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.
      Type Parameters:
      T - the fitness function argument type in the problem domain
      G - the Gene type
      Parameters:
      encoding - 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
      Returns:
      a new InvertibleCodec object with the given parameters.
      Throws:
      NullPointerException - if one of the arguments is null.