geppy.tools package

Submodules

geppy.tools.crossover module

The module crossover provides crossover (mating, recombination) related genetic modifications in GEP, including one-point and two-point crossover, and gene crossover between multigenic chromosomes. Please refer to Chapter 3 of [FC2006] for more details.

Note

All the recombination operators including crossover_one_point(), crossover_two_point(), and crossover_gene() can be applied to both Gene and GeneDc.

[FC2006](1, 2, 3, 4) Ferreira, Cândida. Gene expression programming: mathematical modeling by an artificial intelligence. Vol. 21. Springer, 2006.
geppy.tools.crossover.crossover_gene(ind1, ind2)[source]

Entire genes are exchanged between two parent chromosomes. The two individuals are modified in place, and the two children are returned.

Parameters:
  • ind1 – The first individual (chromosome) participating in the crossover.
  • ind2 – The second individual (chromosome) participating in the crossover.
Returns:

a tuple of two children individuals

This operation has no effect if the chromosome has only one gene. Typically, a gene recombination rate around 0.2 is used.

geppy.tools.crossover.crossover_one_point(ind1, ind2)[source]

Execute one-point recombination of two individuals. The two individuals are modified in place, and the two children are returned.

Parameters:
  • ind1 – The first individual (chromosome) participating in the crossover.
  • ind2 – The second individual (chromosome) participating in the crossover.
Returns:

A tuple of two children individuals.

Note the crossover can happen at any point across the whole chromosome and thus entire genes may be also exchanged between the two parents if they are multigenic chromosomes.

geppy.tools.crossover.crossover_two_point(ind1, ind2)[source]

Execute two-point recombination of two individuals. The two individuals are modified in place, and the two children are returned. The materials between two randomly chosen points are swapped to generate two children.

Parameters:
  • ind1 – The first individual (chromosome) participating in the crossover.
  • ind2 – The second individual (chromosome) participating in the crossover.
Returns:

A tuple of two individuals.

Note the crossover can happen at any point across the whole chromosome and thus entire genes may be also exchanged between the two parents if they are multigenic chromosomes.

geppy.tools.generator module

This module generator provides functionality to generate a genome for a gene. That is, choose functions and terminals randomly from a given primitive set to form a linear form gene expression.

geppy.tools.generator.generate_dc(rnc_array_length, dc_length)[source]

Generate a Dc domain for a RNC array of size rnc_array_length.

Parameters:
  • rnc_array_length – length of the RNC array
  • dc_length – length of the Dc domain
Returns:

a list of integers of length dc_length, each element in range [0, rnc_array_length - 1]

Refer to Chapter 5 of [FC2006] for more details.

geppy.tools.generator.generate_genome(pset, head_length)[source]

Generate a genome with the given primitive set pset and the specified head domain length head_length.

Parameters:
  • pset – a primitive set
  • head_length – length of the head domain
Returns:

a list of symbols representing a genome

Supposing the maximum arity of functions in pset is max_arity, then the tail length is automatically determined to be tail_length = head_length * (max_arity - 1) + 1.

geppy.tools.mutation module

The module mutation provides mutation related genetic modification operators in GEP, including point-mutation, transposition and inversion. Please refer to Chapter 3 of [FC2006] for more details.

Note

  1. Operators that can be applied to both Gene and GeneDc: mutate_uniform(), invert(), is_transpose(), ris_transpose(), gene_transpose()
  2. Operators that end with ‘_dc’ are specially designed to handle the Dc domain in GeneDc.
geppy.tools.mutation.gene_transpose(individual)[source]

Perform gene transposition in place

Parameters:individualChromosome, a chromosome
Returns:a tuple of one individual

An entire gene is selected randomly and exchanged with the first gene in the chromosome. Obviously, this operation only makes sense if the chromosome is a multigenic one. Typically, the gene transposition rate is set around 0.1.

geppy.tools.mutation.invert(individual)[source]

A gene is randomly chosen, and afterwards a subsequence within this gene’s head domain is randomly selected and inverted.

Parameters:individualChromosome, a chromosome
Returns:a tuple of one individual

Typically, a small inversion rate of 0.1 is used.

geppy.tools.mutation.invert_dc(individual)[source]

Dc-specific inversion. A gene is randomly chosen, and afterwards a subsequence within this gene’s Dc domain is randomly selected and inverted.

Parameters:individualChromosome, a chromosome, which contains genes of type GeneDc
Returns:a tuple of one individual

Typically, a small inversion rate of 0.1 is used.

geppy.tools.mutation.is_transpose(individual)[source]

Perform IS transposition in place

Parameters:individualChromosome, a chromosome
Returns:a tuple of one individual

An Insertion Sequence (IS) is a random chosen segment across the chromosome, and then the IS is copied to be inserted at another position in the head of gene, except the start position. Note that an IS from one gene may be copied to a different gene. Typically, the IS transposition rate is set around 0.1.

geppy.tools.mutation.mutate_rnc_array_dc(individual, rnc_gen, ind_pb='1p')[source]

Direct mutation of RNCs, which changes the values in a gene’s RNC array rnc_array() randomly.

Parameters:
  • individualChromosome, a chromosome, which contains genes of type GeneDc.
  • rnc_gen – callable, which returns a random numerical constant by calling rnc_gen().
  • ind_pb – float or str, default ‘1p’. Use a float number to specify the probability of each RNC in the array being mutated. Alternatively, if a str ending with ‘p’ is given in the form like ‘xp’, where ‘x’ is a numerical number, then it specifies how many point mutations are expected for the RNC arrays in individual. For example, if there are d RNCs inside the arrays in total in individual, then ‘1.5p’ is approximately equal to ind_pb = 1.5 / d.
Returns:

a tuple of one individual

The genetic operators mutate_uniform_dc(), transpose_dc() and invert_dc() actually only move the random numerical constants around without generating new numerical values. This method mutate_rnc_array_dc() can replace the value of a particular numerical constant by another in the rnc_array().

Refer to section 5.4.4 of [FC2006] for more details.

geppy.tools.mutation.mutate_uniform(individual, pset, ind_pb='2p')[source]

Uniform point mutation. For each symbol (primitive) in individual, change it to another randomly chosen symbol from pset with the probability indpb. A symbol may be a function or a terminal.

Parameters:
  • individualChromosome, the chromosome to be mutated.
  • psetPrimitiveSet, a primitive set
  • ind_pb – float or str, default ‘2p’. Probability of mutating each symbol. If ind_pb is given as a string ending with ‘p’, then it indicates the expected number of point mutations among all the symbols in individual. For example, if the total length of each gene of individual is l and there are m genes in total, then by passing ind_pb=’1.5p’ we specify approximately ind_pb=1.5/(l*m).
Returns:

A tuple of one chromosome

It is typical to set a mutation rate indpb equivalent to two one-point mutations per chromosome. That is, indpb = 2 / len(chromosome) * len(gene).

geppy.tools.mutation.mutate_uniform_dc(individual, ind_pb='1p')[source]

Dc-specific mutation. This operator changes one index stored in the Dc domain to another index in place. The indices in the Dc domain can later be used to retrieve numerical constants from the gene associated rnc_array().

Parameters:
  • individualChromosome, a chromosome, which contains genes of type GeneDc
  • ind_pb – float or str, default ‘1p’. Probability of mutating each index/position in the Dc domain. If given in str like ‘xp’, then x point mutations for expected for the Dc domains in total of individual. For instance, if there are d Dc elements in the whole chromosome, then 1.5p is equal to ind_pb = 1.5 / d.
Returns:

a tuple of one chromosome

It is typical to set a mutation rate indpb equivalent to two one-point mutations per chromosome. That is, indpb = 2 / len(chromosome) * len(gene).

geppy.tools.mutation.mutate_uniform_ephemeral(individual, ind_pb='1p')[source]

This operator is specially designed for ephemeral numerical constant (ENC) mutation. It attempts to change the value of the ephemeral constants in the individual if any by calling e.update_value(), where e is an ephemeral inside individual. See update_value() for details.

Parameters:
  • individualChromosome, a chromosome
  • ind_pb – float or str, default ‘1p’. If a float is given, then ind_pb specifies the probability of each ephemeral in the individual being mutated. If a str ending with ‘p’ is given in the form like ‘xp’, where ‘x’ is a numerical number, then it specifies how many point mutations are expected for the ephemeral constants. For example, if there are d ephemeral constants present in total in individual, then ‘1.5p’ is approximately equal to ind_pb = 1.5 / d.
Returns:

a tuple of one individual

geppy.tools.mutation.ris_transpose(individual)[source]

Perform RIS transposition in place

Parameters:individualChromosome, a chromosome
Returns:a tuple of one individual

A Root Insertion Sequence (RIS) is a segment of consecutive elements that starts with a function. Once an RIS is randomly chosen, it is copied and inserted at the root (first position) of a gene. Note that an RIS from one gene may be copied to a different gene. Typically, the RIS transposition rate is set around 0.1.

geppy.tools.mutation.transpose_dc(individual)[source]

This operator randomly chooses the chromosome, the gene with its respective Dc to be subjected to transposition, the start and termination points of the transposon, and the target site; then it inserts a copy of the transposon from the place of origin to the target site, while the old content at the target site are shifted to the right and the length of the Dc domain is maintained.

Parameters:individualChromosome, a chromosome, which contains genes of type GeneDc
Returns:a tuple of one individual

Typically, a small transposition rate of 0.1 is used. Note that a transposon from one gene’s Dc may be copied and inserted into another gene’s Dc, which means the genes in a multigenic chromosome individual must have an identical structure.

geppy.tools.parser module

This module parser provides functionality for compiling an individual (a chromosome) in GEP into an executable lambda function in Python for subsequent fitness evaluation.

Todo

Parse an individual into codes of other languages, such as C++/Java, for deployment in an industrial environment.

geppy.tools.parser.compile_(individual, pset)[source]

Compile the individual into a Python lambda expression.

Parameters:
  • individualChromosome, a chromosome
  • psetPrimitiveSet, a primitive set
Returns:

a function if the primitive set pset has any inputs (arguments), which can later be called with specific parameter values; otherwise, a numerical result obtained from evaluation.

geppy.tools.toolbox module

The module toolbox mainly provides a class Toolbox on the basis of deap.base.Toolbox for GEP operator registration, which is then extensively used in the builtin GEP algorithms. For user-defined custom algorithms, they should also accept a toolbox instance and retrieve the operators and their associated probability from the toolbox instance.

class geppy.tools.toolbox.Toolbox[source]

Bases: sphinx.ext.autodoc.importer._MockObject

A toolbox for evolution that contains the evolutionary operators. Initially, the toolbox contains a clone() method that duplicates any element it is passed as argument, this method defaults to the copy.deepcopy() function. and a map() method that applies the function given as first argument to every items of the iterables given as next arguments, this method defaults to the map() function. You may populate the toolbox with any other function by using the register() method.

As an extension of deap.base.Toolbox, this class adds a pbs property to specify the probabilities for the registered operators. For example, pbs[‘mut_uniform’] gives the probability for an operator with the alias ‘mut_uniform’. Besides, Toolbox.pbs is an OrderedDict instance which can remember the order of entry insertion. Thus, if ‘mut_A’ is inserted into pbs before ‘mut_B’, then the operator with the alias ‘mut_A’ is applied earlier than the one corresponding to ‘mut_B’.

A short way to combine operator registration and probability specification together is to pass a keyword-only argument pb into the register() method, which will insert the probability automatically into pbs.

__init__()[source]

Initialize an empty toolbox with only the default clone() and map() methods.

pbs

Get the probability dictionary of type OrderedDict, which specifies the probabilities for the registered operators.

register(alias, function, *args, **kargs)[source]

Register a function in the toolbox under the name alias. You may provide default arguments that will be passed automatically when calling the registered function. Fixed arguments can then be overriden at function call time.

Parameters:
  • alias – The name the operator will take in the toolbox. If the alias already exists it will overwrite the the operator already present.
  • function – The function to which the alias refers.
  • args – one or more positional arguments to pass to the registered function, optional
  • kargs – one or more keyword arguments to pass to the registered function, optional

Hint

Under the hood lies the partial function binding. Check functools.partial() for details.

Note

If an operator needs its probability specified, like mutation and crossover operators, it can be done by inserting the probability into the pbs dictionary with the same alias. Alternatively, it can be given with a the special keyword argument pb in this method

tb = Toolbox()
tb.register('mut_uniform', mutate_uniform, ind_pb=0.02)
tb.pbs['mut_uniform'] = 0.1

or equivalently

tb = Toolbox()
tb.register('mut_uniform', mutate_uniform, ind_pb=0.02, pb=0.1)

As a result, the special keyword argument pb is always excluded from binding into function.

Module contents