Class Mappers

java.lang.Object
io.jenetics.ext.grammar.Mappers

public final class Mappers extends Object
This class defines factories for different CFG ↔ Chromosome mappings (encodings). The classical mapping codec, with a bit-chromosome can be created in the following way.
final Cfg<String> cfg = ...; final Codec<List<Terminal<String>>, BitGene> codec = singleBitChromosomeMapper( cfg, 1000, index -> new SentenceGenerator<>(index, 1000) );
This codec creates a mapping for the given grammar cfg and uses bit-chromosomes with length 1000. The result of the mapping will be a list of terminal symbols which has been created by the given SentenceGenerator. The sentence generator creates sentences with a maximal length of 1000. If no sentence could be created within this limit, an empty list of terminal symbols is returned.
Since:
7.1
Version:
7.1
See Also:
  • Method Details

    • singleBitChromosomeMapper

      public static <T, R> Codec<R,BitGene> singleBitChromosomeMapper(Cfg<? extends T> cfg, int length, Function<? super SymbolIndex,? extends Generator<T,R>> generator)
      Return a classic mapping codec. It uses a bit-chromosome for creating the grammar results. The codons are created by dividing the chromosome in 8-bit junks, as described in Grammatical Evolution by Michael O’Neill and Conor Ryan.
      final Cfg<String> cfg = ...; final Codec<List<Terminal<String>>, BitGene> codec = singleBitChromosomeMapper( cfg, 1000, index -> new SentenceGenerator<>(index, 1000) );
      Type Parameters:
      T - the terminal token type of the grammar
      R - the result type of the mapper
      Parameters:
      cfg - the encoding grammar
      length - the length of the bit-chromosome
      generator - sentence generator function from a given SymbolIndex
      Returns:
      a new mapping codec for the given cfg
      See Also:
    • singleIntegerChromosomeMapper

      public static <T, R> Codec<R,IntegerGene> singleIntegerChromosomeMapper(Cfg<? extends T> cfg, IntRange range, IntRange length, Function<? super SymbolIndex,? extends Generator<T,R>> generator)
      Create a mapping codec, similar as in singleBitChromosomeMapper(Cfg, int, Function). The only difference is that the codons are encoded directly, via an integer-chromosome, so that no gene split is necessary.
      final Cfg<String> cfg = ...; final Codec<List<Terminal<String>>, IntegerGene> codec = singleIntegerChromosomeMapper( cfg, IntRange.of(0, 256), // Value range of chromosomes. IntRange.of(100), // Length (range) ot the chromosome. index -> new SentenceGenerator<>(index, 1000) );
      Type Parameters:
      T - the terminal token type of the grammar
      R - the result type of the mapper
      Parameters:
      cfg - the encoding grammar
      range - the value range of the integer genes
      length - the length range of the integer-chromosome
      generator - sentence generator function from a given SymbolIndex
      Returns:
      a new mapping codec for the given cfg
    • singleIntegerChromosomeMapper

      public static <T, R> Codec<R,IntegerGene> singleIntegerChromosomeMapper(Cfg<? extends T> cfg, IntRange range, int length, Function<? super SymbolIndex,? extends Generator<T,R>> generator)
      Create a mapping codec, similar as in singleBitChromosomeMapper(Cfg, int, Function). The only difference is that the codons are encoded directly, via an integer-chromosome, so that no gene split is necessary.
      final Cfg<String> cfg = ...; final Codec<List<Terminal<String>>, IntegerGene> codec = singleIntegerChromosomeMapper( cfg, IntRange.of(0, 256), // Value range of chromosomes. 100, // Length (range) ot the chromosome. index -> new SentenceGenerator<>(index, 1000) );
      Type Parameters:
      T - the terminal token type of the grammar
      R - the result type of the mapper
      Parameters:
      cfg - the encoding grammar
      range - the value range of the integer genes
      length - the length range of the integer-chromosome
      generator - sentence generator function from a given SymbolIndex
      Returns:
      a new mapping codec for the given cfg
    • multiIntegerChromosomeMapper

      public static <T, R> Codec<R,IntegerGene> multiIntegerChromosomeMapper(Cfg<? extends T> cfg, Function<? super Cfg.Rule<?>,IntRange> length, Function<? super SymbolIndex,? extends Generator<T,R>> generator)
      Codec for creating results from a given grammar. The creation of the grammar result is controlled by a given genotype. This encoding uses separate codons, backed up by a IntegerChromosome, for every rule. The length of the chromosome is defined as a function of the encoded rules. This means that the following CFG,
      (0) (1) (0) <expr> ::= (<expr><op><expr>) | <var> (0) (1) (2) (3) (1) <op> ::= + | - | * | / (0) (1) (2) (3) (4) (2) <var> ::= x | 1 | 2 | 3 | 4
      will be represented by the following Genotype
      Genotype.of( IntegerChromosome.of(IntRange.of(0, 2), length.apply(cfg.rules().get(0))), IntegerChromosome.of(IntRange.of(0, 4), length.apply(cfg.rules().get(1))), IntegerChromosome.of(IntRange.of(0, 5), length.apply(cfg.rules().get(2))) )
      The length function lets you defining the number of codons as function of the rule the chromosome is encoding.
      final Cfg<String> cfg = Bnf.parse(...); final Codec<List<Terminal<String>>, IntegerGene> codec = multiIntegerChromosomeMapper( cfg, // The chromosome length is 25 times the // number of rule alternatives. rule -> IntRange.of(rule.alternatives().size()*25), // Using the standard sentence generator // with a maximal sentence length of 500. index -> new SentenceGenerator<>(index, 500) );
      Type Parameters:
      T - the terminal token type of the grammar
      R - the result type of the mapper
      Parameters:
      cfg - the encoding grammar
      length - the length of the chromosome which is used for selecting rules and symbols. The input parameter for this function is the actual rule. This way it is possible to define the chromosome length dependent on the selectable alternatives.
      generator - sentence generator function from a given SymbolIndex
      Returns:
      a new mapping codec for the given cfg