alogos.systems.pige.mapping

Forward and reverse mapping functions for piGE.

Functions

forward(grammar, genotype, parameters=None, raise_errors=True, return_derivation_tree=False, verbose=False)

Map a piGE genotype to a string phenotype.

reverse(grammar, phenotype_or_derivation_tree, parameters=None, return_derivation_tree=False)

Map a string phenotype (or derivation tree) to a piGE genotype.


Detailed object descriptions

alogos.systems.pige.mapping.forward(grammar, genotype, parameters=None, raise_errors=True, return_derivation_tree=False, verbose=False)[source]

Map a piGE genotype to a string phenotype.

Beginning with the start symbol, each nonterminal symbol is expanded by applying a production rule. These rule applications are done until the sequence of symbols contains only terminals. Then the symbols form a sentence of the language defined by the grammar and hence are a valid phenotype.

Choices to be made in the mapping process:

  1. Which nonterminal is expanded if several are present in the current sequence of symbols?

    While standard GE always uses the leftmost nonterminal, here the next nonterminal is chosen by the next codon in the data (aka “order codon”).

  2. Which production rule is used to expand a selected nonterminal?

    In case there is more than one production rule available for the nonterminal, both GE and piGE use the next codon in the data to select one (aka “content codon”).

    The formula used is: rule id = codon value % number of rules for the NT.

    The data is read from left to right. If the end is reached, a so-called wrap is done to restart again from the left, i.e. codons can be used more than once. If a maximum number of wraps is reached, the mapping process is stopped and the individual has no valid phenotype and should receive the worst possible fitness during fitness evaluation.

Parameters:
  • grammar (Grammar)

  • genotype (Genotype or data that can be converted to it)

  • parameters (dict or ParameterCollection, optional) –

    • max_expansions (int): Maximum number of nonterminal expansions allowed in the derivation created by the mapping process.

    • max_wraps (int): Maximum number of times the genotype is allowed to be wrapped in order to have more codons available for making decisions in the mapping process.

    • stack_mode (str): Mode by which newly discovered symbols are added to the stack of all currently present symbols in a derivation.

      Possible values:

      • "start": Insert new nodes at the start of the stack.

      • "end": Insert new nodes at the end of the stack.

      • "inplace": Insert new nodes at the same place as the expanded nonterminal in the stack.

  • raise_errors (bool, optional) – Possible values:

    • True: A mapping error will be raised if a derivation is not finished within a limit provided in the parameters.

    • False: A partial derivation is allowed. In this case, the returned string will contain unexpanded nonterminal symbols. Therefore it is not a valid phenotype, i.e. not a string of the grammar’s language but a so-called sentential form.

  • return_derivation_tree (bool, optional) – If True, not only the phenotype is returned but additionally also the derivation tree.

  • verbose (bool, optional) – If True, output about steps of the mapping process is printed.

Returns:

  • phenotype (str) – If return_derivation_tree is False, which is the default.

  • (phenotype, derivation_tree) (tuple with two elements of type str and DerivationTree) – If return_derivation_tree is True.

Raises:

MappingError – If raise_errors is True and the mapping process can not generate a full derivation before reaching a limit provided in the parameters.

alogos.systems.pige.mapping.reverse(grammar, phenotype_or_derivation_tree, parameters=None, return_derivation_tree=False)[source]

Map a string phenotype (or derivation tree) to a piGE genotype.

This is a reversal of the mapping procedure of position-independent Grammatical Evolution (piGE). Note that many different piGE genotypes can encode the same derivation tree and phenotype. It is possible to return a deterministic piGE genotype that uses the lowest possible integer value for each choice of expansion, or a random piGE genotype that uses a random integer value within the codon size limit.

Parameters:
  • grammar (Grammar)

  • phenotype_or_derivation_tree (str or DerivationTree)

  • parameters (dict or ParameterCollection, optional) – Following keyword-value pairs are considered by this function:

    • codon_size (int): Codon size in bits. The number of different integer values a codon can assume is therefore determined by 2**codon_size.

    • codon_randomization (bool): If True, the reverse mapping will use the so-called “unmod” operation and therefore generate random integers that encode the required rule choice. If False, the lowest possible integer will be used and the reverse mapping becomes deterministic.

    • derivation_order (str): Choice of next nonterminal to expand, which in piGE is chosen by a codon in the genotype.

      Possible values:

      • "leftmost": Always choose the leftmost unexpanded nonterminal in the partial derivation.

      • "rightmost": Always choose the rightmost unexpanded nonterminal in the partial derivation.

      • "random": Always choose a random unexpanded nonterminal in the partial derivation. This makes the reverse mapping stochastic.

    • stack_mode (str): Mode by which newly discovered symbols are added to the stack of all currently present symbols in a derivation.

      Possible values:

      • "start": Insert new nodes at the start of the stack.

      • "end": Insert new nodes at the end of the stack.

      • "inplace": Insert new nodes at the same place as the expanded nonterminal in the stack.

  • return_derivation_tree (bool, optional) – If True, not only the genotype is returned but additionally also the derivation tree.

Returns:

Raises:

MappingError – If the reverse mapping fails because the string does not belong to the grammar’s language or the derivation tree does not represent a valid derivation.