glyph.gp package

Submodules

glyph.gp.algorithms module

class NSGA2(mate_func, mutate_func)[source]

Bases: glyph.gp.algorithms.MOGP

Implementation of the NSGA-II algorithm as described in Essentials of Metaheuristics

class SPEA2(mate_func, mutate_func)[source]

Bases: glyph.gp.algorithms.MOGP

Implementation of the SPEA2 algorithm as described in Essentials of Metaheuristics

class DeapEaSimple(mate_func, mutate_func)[source]

Bases: object

Basically a copy of deap.algorithms.eaSimple algorithm.

evolve(population)[source]
class AgeFitness(mate_func, mutate_func, select, create_func)[source]

Bases: glyph.gp.algorithms.MOGP

AgeFitness algorithm as described in Schmidt & Lipson. DOI: 10.1007/978-1-4419-7747-2_8

evolve(population)[source]
class UNSGA2(mate_func, mutate_func)

Bases: glyph.gp.algorithms.NSGA2

evlolve(population)
class USPEA2(mate_func, mutate_func)

Bases: glyph.gp.algorithms.SPEA2

evlolve(population)
class UDeapEaSimple(mate_func, mutate_func)

Bases: glyph.gp.algorithms.DeapEaSimple

evlolve(population)
class UAgeFitness(mate_func, mutate_func, select, create_func)

Bases: glyph.gp.algorithms.AgeFitness

evlolve(population)

glyph.gp.breeding module

mutuniform(pset, **kwargs)[source]

Factory for mutuniform

mutnodereplacement(pset, **kwargs)[source]

Factory for mutnodereplacement

mutshrink(pset, **kwargs)[source]

Factory for mutshrink

mutshrink(pset, **kwargs)[source]

Factory for mutshrink

cxonepoint(**kwargs)[source]

Factory for cxonepoint

cxonepointleafbiased(**kwargs)[source]

Factory for cxonepointleafbiased

glyph.gp.constraints module

class Constraint(spaces)[source]

Bases: object

class NonFiniteExpression(zero=True, infty=True, constant=False)[source]

Bases: glyph.gp.constraints.Constraint

Use sympy to check for finite expressions.

Parameters:
  • zero – flag to check for zero expressions
  • infty – flag to check for infinite expressions
  • constant – flag to check for constant expressions
class PreTest(fn, fun='chi')[source]

Bases: glyph.gp.constraints.Constraint

Apply pre-testing to check for constraint violation.

The python script needs to provide a callable fun(ind).

Parameters:
  • fn – filename of the python script.
  • fun – name of the function in fn.
class PreTestService(assessment_runner)[source]

Bases: glyph.gp.constraints.Constraint

com
make_str
constrain(funcs, constraint, n_trials=30, timeout=60)[source]

Decorate a list of genetic operators with constraints.

Parameters:
  • funcs – list of operators (mate, mutate, create)
  • constraint – instance of Nullspace
Returns:

constrained operators

reject_constrain_violation(constraint, n_trials=30, timeout=60)[source]

Create constraints decorators based on rules.

Parameters:
  • constraint
  • n_trials – Number of tries. Give up afterwards (return input).
Returns:

list of constraint decorators

glyph.gp.individual module

Provide Individual class for gp.

class AExpressionTree(content)[source]

Bases: deap.gp.PrimitiveTree

Abstract base class for the genotype.

Derived classes need to specify a primitive set from which the expression tree can be build, as well as a phenotype method.

const_opt
classmethod create(gen_method=<function genHalfAndHalf>, min=1, max=4)[source]
classmethod create_population(size, gen_method=<function genHalfAndHalf>, min=1, max=4)[source]

Create a list of individuals of class Individual.

classmethod from_string(string)[source]

Try to convert a string expression into a PrimitiveTree given a PrimitiveSet pset. The primitive set needs to contain every primitive present in the expression.

Parameters:
  • string – String representation of a Python expression.
  • pset – Primitive set from which primitives are selected.
Returns:

PrimitiveTree populated with the deserialized primitives.

hasher

alias of builtins.str

pset
resolve_sc()[source]

Evaluate StructConst in individual top to bottom.

terminals

Return terminals that occur in the expression tree.

to_polish(for_sympy=False, replace_struct=True)[source]

Symbolic representation of the expression tree.

class ANDimTree(trees)[source]

Bases: list

A naive tree class representing a vector-valued expression.

Each dimension is encoded as a expression tree.

base
classmethod create_individual(ndim)[source]
classmethod create_population(size, ndim)[source]
classmethod from_string(strs)[source]
height
pset
terminals

Return terminals that occur in the expression tree.

class Individual(pset, name='MyIndividual', **kwargs)[source]

Bases: type

Construct a new expression tree type.

Parameters:
  • psetdeap.gp.PrimitiveSet
  • name – name of the expression tree class
  • kwargs – additional attributes

Returns: expression tree class

static __new__(mcs, pset, name='MyIndividual', **kwargs)[source]

Construct a new expression tree type.

Parameters:
  • psetdeap.gp.PrimitiveSet
  • name – name of the expression tree class
  • kwargs – additional attributes

Returns: expression tree class

class Measure(values=())[source]

Bases: deap.base.Fitness

This is basically a wrapper around deap.base.Fitness.

It provides the following enhancements over the base class: - more adequate naming - copy constructable - no weight attribute

del_values()[source]
get_values()[source]
set_values(values)[source]
values
weights = repeat(-1)
class NDIndividual(base, name='MyNDIndividual', **kwargs)[source]

Bases: type

Construct a new n-dimensional expression tree type.

Parameters:
  • base (Individual) – one dimensional base class
  • name – name of the n-dimensional expression tree class
  • **kwargs – addtional attributes

Returns: n-dimensional expression tree class

class StructConst(func, arity=2)[source]

Bases: deap.gp.Primitive

Parameters:func – evaluate left and right subtree and assign a constant.
as_terminal(*args)[source]
static get_len(expr, tokens='(, ')[source]
add_sc(pset, func)[source]

Adds a structural constant to a given primitive set.

Parameters:
child_trees(ind)[source]

Yield all child tree which are used as arguments for the head node of ind.

convert_inverse_prim(prim, args)[source]

Convert inverse prims according to: [Dd]iv(a,b) -> Mul[a, 1/b] [Ss]ub(a,b) -> Add[a, -b]

We achieve this by overwriting the corresponding format method of the sub and div prim.

nd_phenotype(nd_tree, backend=<function sympy_phenotype>)[source]
Parameters:
  • nd_tree (ANDimTree) –
  • backend – sympy_phenotype or numpy_phenotype
Returns:

lambda function

numpy_phenotype(individual)[source]

Lambdify the individual

Parameters:individual (glyph.gp.individual.AExpressionTree) –
Returns:lambda function
Note:

In constrast to sympy_phenotype the callable will have a variable number of keyword arguments depending on the number of symbolic constants in the individual.

Example:
>>> pset = numpy_primitive_set(1)
>>> MyIndividual = Individual(pset=pset)
>>> ind = MyIndividual.from_string("Add(x_0, Symc)")
>>> f = numpy_phenotype(ind)
>>> f(1, 1)
2
numpy_primitive_set(arity, categories=('algebraic', 'trigonometric', 'exponential', 'symc'))[source]

Create a primitive set based on numpys vectorized functions.

Parameters:
  • arity – Number of variables in the primitive set
  • categories
Returns:

deap.gp.PrimitiveSet

Note

All functions will be closed, that is non-defined values will be mapped to 1. 1/0 = 1!

pretty_print(expr, constants, consts_values, count=0)[source]

Replace symbolic constants in the str representation of an individual by their numeric values.

This checks for
  • c followed by “)” or “,”
  • c followed by infix operators
  • c
sc_mmqout(x, y, cmin=-1, cmax=1)[source]

SC is the minimum-maximum quotient of the number of nodes of both child-trees x and y mapped into the constant interval [cmin, cmax]

sc_qout(x, y)[source]

SC is the quotient of the number of nodes of its left and right child-trees x and y

sympy_phenotype(individual)[source]

Compile python function from individual.

Uses sympy’s lambdify function. Terminals from the primitive set will be used as parameters to the constructed lambda function; primitives (like sympy.exp) will be converted into numpy expressions (eg. numpy.exp).

Parameters:individual (glyph.gp.individual.AExpressionTree) –
Returns:lambda function
sympy_primitive_set(categories=('algebraic', 'trigonometric', 'exponential'), arguments=['y_0'], constants=[])[source]

Create a primitive set with sympy primitves.

Parameters:
  • arguments – variables to use in primitive set
  • constants – symbolic constants to use in primitive set
  • categories
    an optional list of function categories for the primitive set. The following are available
    ’algebraic’, ‘neg’, ‘trigonometric’, ‘exponential’, ‘exponential’, ‘logarithm’, ‘sqrt’.
    return:deap.gp.PrimitiveSet

Module contents