API

Application

This module provides convenience classes and functions that allow you to quickly build gp apps.

Convenience classes and functions that allow you to quickly build gp apps.

class glyph.application.GPRunner(IndividualClass, algorithm_factory, assessment_runner, callbacks=(<function update_halloffame>, <function update_logbook_record>))[source]

Runner for gp problem sets.

Takes care of propper initialization, execution, and accounting of a gp run (i.e. population creation, random state, generation count, hall of fame, and logbook). The method init() has to be called once before stepping through the evolution process with method step(); init() and step() invoke the assessment runner.

__init__(IndividualClass, algorithm_factory, assessment_runner, callbacks=(<function update_halloffame>, <function update_logbook_record>))[source]

Init GPRunner.

Parameters:
  • IndividualClass – Class inherited from gp.AExpressionTree.
  • algorithm_factory – callable() -> gp algorithm, as defined in gp.algorithms.
  • assessment_runner – callable(population) -> None, updates fitness values of each invalid individual in population.
init(pop_size)[source]

Initialize the gp run.

step()[source]

Step through the evolution process.

glyph.application.default_gprunner(Individual, assessment_runner, callbacks=(), **kwargs)[source]

Create a default GPRunner instance.

For config options see MateFactory, MutateFactory, AlgorithmFactory.

class glyph.application.Application(config, gp_runner, checkpoint_file=None, callbacks=(<function make_checkpoint>, <function log>))[source]

An application based on GPRunner.

Controls execution of the runner and adds checkpointing and logging functionality; also defines a set of available command line options and their default values.

To create a full console application one can use the factory function create_console_app().

__init__(config, gp_runner, checkpoint_file=None, callbacks=(<function make_checkpoint>, <function log>))[source]
Parameters:
  • config (dict or argparse.Namespace) – Container holding all configs
  • gp_runner – Instance of GPRunner
  • checkpoint_file – Path to checkpoint_file
  • callbacks
run(break_condition=None)[source]

Run gp app.

Parameters:break_condition (callable(application)) – is called after every evolutionary step.
Returns:number of iterations executed during run.
checkpoint()[source]

Checkpoint current state of evolution.

classmethod from_checkpoint(file_name)[source]

Create application from checkpoint file.

static add_options(parser)[source]

Add available parser options.

glyph.application.default_console_app(IndividualClass, AssessmentRunnerClass, parser=ArgumentParser(prog='sphinx-build', usage=None, description=None, formatter_class=<class 'argparse.HelpFormatter'>, conflict_handler='error', add_help=True), callbacks=())[source]

Factory function for a console application.

class glyph.application.AlgorithmFactory[source]

Factory class for gp algorithms.

classmethod _create(args, mate_func, mutate_func, select, create_func)[source]

Setup gp algorithm.

static add_options(parser)[source]

Add available parser options.

class glyph.application.MateFactory[source]

Factory class for gp mating functions.

static _create(args, IndividualClass)[source]

Setup mating function.

static add_options(parser)[source]

Add available parser options.

class glyph.application.MutateFactory[source]

Factory class for gp mutation functions.

static _create(args, IndividualClass)[source]

Setup mutation function.

static add_options(parser)[source]

Add available parser options.

class glyph.application.SelectFactory[source]

Factory class for selection

static add_options(parser)[source]

Add available parser options.

class glyph.application.CreateFactory[source]

Factory class for creation

class glyph.application.ParallelizationFactory[source]

Factory class for parallel execution schemes.

static add_options(parser)[source]

Add available parser options.

glyph.application.safe(file_name, **kwargs)[source]

Dump kwargs to file.

glyph.application.load(file_name)[source]

Load data saved with safe().

glyph.application.create_tmp_dir(prefix='run-')[source]

Create directory with current time as signature.

glyph.application.create_stats(n)[source]

Create deap.tools.MultiStatistics object for n fitness values.

glyph.application.to_argparse_namespace(d)[source]

Return argparse.Namespace object created from dictionary d.

Assessment

Some usefull classes/functions for the fitness assessment part in gp problems.

class glyph.assessment.AAssessmentRunner(parallel_factory=<glyph.assessment.SingleProcessFactoy object>)[source]

Abstract runner for the (parallel) assessment of individuals in a population.

Child classes have to at least override the measure() method, which might be executed in a different process or even on a remote machine depending on the parallelization scheme. Child classes may override the setup() method, which is executed once on object instantiation. Child classes may override the assign_fitness() method, which is executed in the main process. This can be usefull if you want to locally post-process the results of measure(), when collected from remote processes.

__init__(parallel_factory=<glyph.assessment.SingleProcessFactoy object>)[source]

Store an instance of parallel_factory.

Parameters:parallel_factory – callable() -> obj, obj has to implement some kind of (parallel) map() method.
setup()[source]

Default implementation.

assign_fitness(individual, fitness)[source]

Assign a fitness value (as returned by self.measure()) to idividual.

Default implementation.

measure(individual)[source]

Return a fitness value for individual.

glyph.assessment.measure(*funcs, pre=<function identity>, post=<function identity>)[source]

Combine several measurement functions into one.

Optionaly do pre- and/or post-processing.

Parameters:
  • funcs – a sequence of measure functions as returned by measure() (eg. callable(*a, **kw) -> tuple), and/or single valued functions (eg. callable(*a, **kw) -> numerical value).
  • pre – some pre-processing function that is to be apllied on input once before passing the result to each function in funcs.
  • post – some post-processing function that is to be apllied on the tuple of measure values as returned by the combined funcs.
Returns:

callable(input) -> tuple of measure values, where input is usually a phenotype (eg. an expression tree).

glyph.assessment.default_constants(ind)[source]

Return a one for each different constant in the primitive set.

Parameters:ind (glyph.gp.individual.AExpressionTree) –
Returns:A value for each constant in the primitive set.
glyph.assessment.const_opt(measure, individual, lsq=False, default_constants=<function default_constants>, f_kwargs=None, **kwargs)[source]

Apply constant optimization

Parameters:
  • measurecallable(individual, *f_args) -> scalar.
  • individual – an individual tha is passed on to measure.
  • bounds – bounds for the constant values (s. scipy.optimize.minimize).
  • method – Type of solver. Should either be ‘leastsq’, or one of scipy.optimize.minimize’s solvers.
Returns:

(popt, measure_opt), popt: the optimal values for the constants; measure_opt: the measure evaluated at popt.

glyph.assessment.const_opt_scalar(measure, individual, lsq=False, default_constants=<function default_constants>, f_kwargs=None, **kwargs)

Apply constant optimization

Parameters:
  • measurecallable(individual, *f_args) -> scalar.
  • individual – an individual tha is passed on to measure.
  • bounds – bounds for the constant values (s. scipy.optimize.minimize).
  • method – Type of solver. Should either be ‘leastsq’, or one of scipy.optimize.minimize’s solvers.
Returns:

(popt, measure_opt), popt: the optimal values for the constants; measure_opt: the measure evaluated at popt.

glyph.assessment.replace_nan(x, rep=inf)[source]

Replace occurences of np.nan in x.

Parameters:
  • x (list, tuple, float, np.ndarray) – Any data structure
  • rep – value to replace np.nan with
Returns:

x without nan’s

glyph.assessment._tt_flatten(tt)[source]

Flatten a tuple of tuples.

glyph.assessment._argcount(func)[source]

Return argument count of func.

glyph.assessment.annotate(func, annotations)[source]

Add annoations to func.

glyph.assessment.returns(func, types)[source]

Check func’s annotation dictionary for return type tuple.

glyph.assessment.tuple_wrap(func)[source]

Wrap func’s return value into a tuple if it is not one already.

GP

Algorithms

class glyph.gp.algorithms.NSGA2(mate_func, mutate_func)[source]

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

class glyph.gp.algorithms.SPEA2(mate_func, mutate_func)[source]

Implementation of the SPEA2 algorithm as described in Essentials of Metaheuristics

class glyph.gp.algorithms.DeapEaSimple(mate_func, mutate_func)[source]

Basically a copy of deap.algorithms.eaSimple algorithm.

class glyph.gp.algorithms.AgeFitness(mate_func, mutate_func, select, create_func)[source]

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

Breeding

glyph.gp.breeding.mutuniform(pset, **kwargs)[source]

Factory for mutuniform

glyph.gp.breeding.mutnodereplacement(pset, **kwargs)[source]

Factory for mutnodereplacement

glyph.gp.breeding.mutshrink(pset, **kwargs)[source]

Factory for mutshrink

glyph.gp.breeding.mutshrink(pset, **kwargs)[source]

Factory for mutshrink

glyph.gp.breeding.cxonepoint(**kwargs)[source]

Factory for cxonepoint

glyph.gp.breeding.cxonepointleafbiased(**kwargs)[source]

Factory for cxonepointleafbiased

Constraints

glyph.gp.constraints.build_constraints(null_space, n_trials=30)[source]

Create constraints decorators based on rules.

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

list of constraint decorators

glyph.gp.constraints.apply_constraints(funcs, constraints)[source]

Decorate a list of genetic operators with constraints.

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

constrained operators

Individual

Provide Individual class for gp.

glyph.gp.individual.sc_qout(x, y)[source]

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

glyph.gp.individual.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]

glyph.gp.individual.add_sc(pset, func)[source]

Adds a structural constant to a given primitive set. :param func: callable(x, y) -> float where x and y are the expressions of the left and right subtree :param pset: You may want to use sympy_primitive_set or numpy_primitive_set without symbolic constants. :type pset: deap.gp.PrimitiveSet

glyph.gp.individual.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
glyph.gp.individual.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
glyph.gp.individual.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!

glyph.gp.individual.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)
>>> class Individual(AExpressionTree)
>>>    pset = pset
>>> ind = Individual.from_string("Add(x_0, symc)")
>>> f = numpy_phenotype(ind)
>>> f(1, 1)
2
class glyph.gp.individual.AExpressionTree(content)[source]

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.

hasher

alias of str

terminals

Return terminals that occur in the expression tree.

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

Create a list of individuals of class Individual.

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

lambda function

class glyph.gp.individual.ANDimTree(trees)[source]

A naive tree class representing a vector-valued expression. Each dimension is encoded as a expression tree.

terminals

Return terminals that occur in the expression tree.

class glyph.gp.individual.Measure(values=())[source]

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

glyph.gp.individual.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.

glyph.gp.individual.stringify_for_sympy(f)[source]

Return the expression in a human readable string.

glyph.gp.individual._constant_normal_form(expr, variables=())[source]

experimental

glyph.gp.individual.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

Utils

class glyph.utils.Memoize(fn)[source]

Memoize(fn) - an instance which acts like fn but memoizes its arguments Will only work on functions with non-mutable arguments http://code.activestate.com/recipes/52201/

glyph.utils.random_state(obj, rng=<module 'random' from '/home/docs/checkouts/readthedocs.org/user_builds/glyph/conda/latest/lib/python3.5/random.py'>)[source]

Do work inside this contextmanager with a random state defined by obj.

Looks for _prev_state to seed the rng. On exit, it will write the current state of the rng as _tmp_state to the obj.

Params obj:Any object.
Params rng:Instance of a random number generator.

Argparse helpers

Collection of helper functions for arparse.

glyph.utils.argparse.positive_int(string)[source]

Check whether string is an integer greater than 0.

glyph.utils.argparse.non_negative_int(string)[source]

Check whether string is an integer greater than -1.

glyph.utils.argparse.unit_interval(string)[source]

Check whether string is a float in the interval [0.0, 1.0].

glyph.utils.argparse.ntuple(n, to_type=<class 'float'>)[source]

Check whether string is an n-tuple.

glyph.utils.argparse.readable_file(string)[source]

Check weather file is readable

Numeric

glyph.utils.numeric.rms(y)[source]

Root mean square.

glyph.utils.numeric.rmse(x, y)[source]

Root mean square error.

glyph.utils.numeric.nrmse(x, y)[source]

Normalized, with respect to x, root mean square error.

glyph.utils.numeric.cvrmse(x, y)[source]

Coefficient of variation, with respect to x, of the rmse.

glyph.utils.numeric.hill_climb(fun, x0, args, precision=5, maxfev=100, directions=5, target=0, rng=<module 'numpy.random' from '/home/docs/checkouts/readthedocs.org/user_builds/glyph/conda/latest/lib/python3.5/site-packages/numpy/random/__init__.py'>, **kwargs)[source]

Stochastic hill climber for constant optimization. Try self.directions different solutions per iteration to select a new best individual.

Parameters:
  • fun – function to optimize
  • x0 – initial guess
  • args – additional arguments to pass to fun
  • precision – maximum precision of x0
  • maxfev – maximum number of function calls before stopping
  • directions – number of directions to explore before doing a hill climb step
  • target – stop if fun(x) <= target
  • rng – (seeded) random number generator
Returns:

scipy.optimize.OptimizeResult

class glyph.utils.numeric.SmartConstantOptimizer(method, step_size=10, min_stat=10, threshold=25)[source]

Decorate a minimize method used in scipy.optimize.minimize to cancel non promising constant optimizations.

The stopping criteria is based on the improvment rate :math:`

rac{Delta f}[Delta fev}`.

If the improvment rate is below the q_{threshold} quantile for a given number of function evaluations, optimization is stopped.
__init__(method, step_size=10, min_stat=10, threshold=25)[source]
Params method:see scipy.optimize.minimize method
Params step_size:
 number of function evaluations betweem iterations
Params min_stat:
 minmum sample size before stopping
Params threshold:
 quantile
glyph.utils.numeric.expressional_complexity(ind)[source]

Sum of length of all subtrees of the individual.

Logging

glyph.utils.logging.load_config(config_file, placeholders={}, default_level=20)[source]

Load logging configuration from .yaml file.

glyph.utils.logging.log_level(verbosity)[source]

Convert numeric verbosity to logging log levels.

glyph.utils.logging.print_dict(p_func, d)[source]

Pretty print a dictionary

Parameters:p_func – printer to use (print or logging)
glyph.utils.logging.print_params(p_func, gp_config)[source]

Pretty print a glyph app config

Break condition

class glyph.utils.break_condition.SoftTimeOut(ttl)[source]

Break condition based on a soft time out. Start a new generation as long as there is some time left.

__init__(ttl)[source]
Parameters:ttl – time to live in seconds
glyph.utils.break_condition.timeout(ttl)[source]

Decorate a function. Will raise TimeourError if function call takes longer than the ttl.

Vendored from ffx.

Parameters:ttl – time to live in seconds
glyph.utils.break_condition.max_fitness_on_timeout(max_fitness)[source]

Decorate a function. Associate max_fitness with long running individuals.

Parameters:max_fitness – fitness of aborted individual calls.
Returns:fitness or max_fitness
glyph.utils.break_condition.soft_max_iter(app, max_iter=inf)[source]

Soft breaking condition. Will check after each generation weather maximum number of iterations is exceeded.

Parameters:max_iter – maximum number of function evaluations
Returns:bool(iter) > max_iter
glyph.utils.break_condition.soft_target(app, target=0, error_index=0)[source]

Soft breaking condition. Will check after each generation minimum error is reached.

Parameters:
  • target – value of desired error metric
  • error_index – index in fitness tuple
Returns:

bool(min_error) <= target

glyph.utils.break_condition.break_condition(target=0, error_index=0, ttl=0, max_iter=inf)[source]

Combined breaking condition based on time to live, minimum target and maximum number of iterations.

Example:
>>> app = glyph.application.Application(...)
>>> bc = break_condition(...)
>>> app.run(break_condition=bc)
Parameters:
  • target – value of desired error metric
  • error_index – index in fitness tuple
  • ttl – time to live in seconds
  • max_iter – maximum number of iterations

cli