glyph package

Submodules

glyph.application module

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

class AFactory[source]

Bases: object

static add_options(parser)[source]

Add available parser options.

classmethod create(config, *args, **kwargs)[source]
classmethod get_from_mapping(key)[source]
class AlgorithmFactory[source]

Bases: glyph.application.AFactory

Factory class for gp algorithms.

static add_options(parser)[source]

Add available parser options.

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

Bases: object

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 default_console_app().

Parameters:
  • config (dict or argparse.Namespace) – Container holding all configs
  • gp_runner – Instance of GPRunner
  • checkpoint_file – Path to checkpoint_file
  • callbacks
static add_options(parser)[source]

Add available parser options.

assessment_runner
checkpoint()[source]

Checkpoint current state of evolution.

classmethod from_checkpoint(file_name)[source]

Create application from checkpoint file.

logbook
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.
workdir
class ConstraintsFactory[source]

Bases: glyph.application.AFactory

static add_options(parser)[source]

Add available parser options.

class CreateFactory[source]

Bases: glyph.application.AFactory

Factory class for creation

add_options()[source]

Add available parser options.

class GPRunner(IndividualClass, algorithm_factory, assessment_runner, callbacks=(<function update_pareto_front>, <function update_logbook_record>))[source]

Bases: object

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.

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.

class MateFactory[source]

Bases: glyph.application.AFactory

Factory class for gp mating functions.

static add_options(parser)[source]

Add available parser options.

class MutateFactory[source]

Bases: glyph.application.AFactory

Factory class for gp mutation functions.

static add_options(parser)[source]

Add available parser options.

class ParallelizationFactory[source]

Bases: glyph.application.AFactory

Factory class for parallel execution schemes.

static add_options(parser)[source]

Add available parser options.

class SelectFactory[source]

Bases: glyph.application.AFactory

Factory class for selection

static add_options(parser)[source]

Add available parser options.

create_stats(n)[source]

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

create_tmp_dir(prefix='run-')[source]

Create directory with current time as signature.

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=(<function make_checkpoint>, <function log>))[source]

Factory function for a console application.

default_gprunner(Individual, assessment_runner, callbacks=(<function update_pareto_front>, <function update_logbook_record>), **kwargs)[source]

Create a default GPRunner instance.

For config options see MateFactory, MutateFactory, AlgorithmFactory.

get_mapping(group)[source]
load(file_name)[source]

Load data saved with safe().

log(app)[source]
make_checkpoint(app)[source]
safe(file_name, **kwargs)[source]

Dump kwargs to file.

to_argparse_namespace(d)[source]

Return argparse.Namespace object created from dictionary d.

update_logbook_record(runner)[source]
update_pareto_front(runner)[source]

glyph.assessment module

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

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

Bases: object

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.

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

Update the fitness of each individual in population that has an invalid fitness.

Parameters:population – a squence of individuals.
__getstate__()[source]

Modify pickling behavior for the class.

All the attributes except ‘parallel’ can be pickled.

__setstate__(state)[source]

Modify unpickling behavior for the class.

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.

setup()[source]

Default implementation.

class SingleProcessFactory[source]

Bases: object

class map

Bases: object

map(func, *iterables) –> map object

Make an iterator that computes the function using arguments from each of the iterables. Stops when the shortest iterable is exhausted.

annotate(func, annotations)[source]

Add annoations to func.

const_opt(measure, individual, lsq=False, default_constants=<function default_constants>, f_kwargs=None, **kwargs)[source]

Apply constant optimization

Parameters:
  • measure – callable(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.

const_opt_leastsq(measure, individual, default_constants=<function default_constants>, f_kwargs=None, **kwargs)[source]
const_opt_scalar(*args, **kwargs)[source]
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.
expressional_complexity(ind)[source]

Sum of length of all subtrees of the individual.

max_fitness_on_timeout(max_fitness, timeout)[source]

Decorate a function. Associate max_fitness with long running individuals.

Parameters:
  • max_fitness – fitness of aborted individual calls.
  • timeout – time until timeout
Returns:

fitness or max_fitness

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).

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

returns(func, types)[source]

Check func’s annotation dictionary for return type tuple.

tuple_wrap(func)[source]

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

glyph.observer module

class ProgressObserver[source]

Bases: object

Animates the progress of the evolutionary optimization.

Note

Uses matplotlib’s interactive mode.

__call__(app)[source]

Note

To be used as a callback in glyph.application.Application.

Parameters:app (glyph.application.Application) –
get_limits(x, factor=1.1)[source]

Calculates the plot range given an array x.

Module contents