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.
-
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
-
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.constraints module¶
-
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
¶
-
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_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
¶
-
terminals
¶ Return terminals that occur in 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
¶
-
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: - pset –
deap.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: - pset –
deap.gp.PrimitiveSet
- name – name of the expression tree class
- kwargs – additional attributes
Returns: expression tree class
- pset –
- pset –
-
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
-
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.
-
add_sc
(pset, func)[source]¶ Adds a structural constant to a given primitive set.
Parameters: - func –
callable(x, y) -> float
where x and y are the expressions of the left and right subtree - pset (
deap.gp.PrimitiveSet
) – You may want to usesympy_primitive_set
ornumpy_primitive_set
without symbolic constants.
- func –
-
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: 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