java.lang.Object
io.jenetics.ext.grammar.Mappers
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.
This codec creates a mapping for the given grammar
final Cfg<String> cfg = ...;
final Codec<List<Terminal<String>>, BitGene> codec = singleBitChromosomeMapper(
cfg,
1000,
index -> new SentenceGenerator<>(index, 1000)
);
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 Summary
Modifier and TypeMethodDescriptionstatic <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.singleBitChromosomeMapper
(Cfg<? extends T> cfg, int length, Function<? super SymbolIndex, ? extends Generator<T, R>> generator) Return a classic mapping codec.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 insingleBitChromosomeMapper(Cfg, int, Function)
.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 insingleBitChromosomeMapper(Cfg, int, Function)
.
-
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 grammarR
- the result type of the mapper- Parameters:
cfg
- the encoding grammarlength
- the length of the bit-chromosomegenerator
- sentence generator function from a givenSymbolIndex
- 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 insingleBitChromosomeMapper(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 grammarR
- the result type of the mapper- Parameters:
cfg
- the encoding grammarrange
- the value range of the integer geneslength
- the length range of the integer-chromosomegenerator
- sentence generator function from a givenSymbolIndex
- 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 insingleBitChromosomeMapper(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 grammarR
- the result type of the mapper- Parameters:
cfg
- the encoding grammarrange
- the value range of the integer geneslength
- the length range of the integer-chromosomegenerator
- sentence generator function from a givenSymbolIndex
- 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 aIntegerChromosome
, for every rule. The length of the chromosome is defined as a function of the encoded rules. This means that the following CFG,will be represented by the following(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
Genotype
TheGenotype.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))) )
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 grammarR
- the result type of the mapper- Parameters:
cfg
- the encoding grammarlength
- 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 givenSymbolIndex
- Returns:
- a new mapping codec for the given
cfg
-