geneticengine.representations.stackgggp

This module relies on much of the GE implementation.

The only difference is the genotype to phenotype mapping, which uses stacks.

Package Contents

Classes

Grammar

RandomSource

Helper class that provides a standard way to create an ABC using

RepresentationWithCrossover

Abstract base class for generic types.

RepresentationWithMutation

Abstract base class for generic types.

Representation

Abstract base class for generic types.

TreeNode

Base class for protocol classes.

Genotype

ListWrapper

StackBasedGGGPRepresentation

This representation uses a list of integers to guide the generation of

Functions

get_arguments(n)

param n:

production

get_generic_parameter(ty)

When given Annotated[T, <annotations>] or List[T], this function returns

get_generic_parameters(ty)

Annotated[T, <annotations>] or List[T], this function returns

is_abstract(t)

Returns whether a class is a Protocol or AbstractBaseClass.

is_generic_list(ty)

Returns whether a type is List[T] for any T.

is_metahandler(ty)

Returns if type is a metahandler. AnnotatedType[int, IntRange(3,10)] is

add_to_stacks(stacks, t, v)

create_tree_using_stacks(g, r[, max_depth])

class geneticengine.representations.stackgggp.Grammar(starting_symbol, considered_subtypes=None, expansion_depthing=False)
Parameters:
  • starting_symbol (type)

  • considered_subtypes (list[type] | None)

  • expansion_depthing (bool)

starting_symbol: type
alternatives: dict[type, list[type]]
distanceToTerminal: dict[Any, int]
all_nodes: set[type]
recursive_prods: set[type]
terminals: set[type]
non_terminals: set[type]
abstract_dist_to_t: dict[type, dict[type, int]]
validate()
register_alternative(nonterminal, nodetype)

Register a production A->B Call multiple times with same A to register many possible alternatives.

Parameters:
  • nonterminal (type)

  • nodetype (type)

register_type(ty)
Parameters:

ty (type)

__repr__()

Return repr(self).

get_all_symbols()

All symbols in the current grammar, including terminals.

Return type:

tuple[set[type], set[type], set[type]]

get_distance_to_terminal(ty)

Returns the current distance to terminal of a given type.

Parameters:

ty (type)

Return type:

int

get_min_tree_depth()

Returns the minimum depth a tree must have.

get_max_node_depth()

Returns the maximum minimum depth a node can have.

preprocess()

Computes distanceToTerminal via a fixpoint algorithm.

Return type:

None

get_weights()
update_weights(learning_rate, extra_weights)
get_branching_average_proxy(r, get_nodes_depth_specific, n_individuals=100, max_depth=17)

Get a proxy for the average branching factor of a grammar.

This proxy is a dictionary with the number of non-terminals in each depth of the grammar, obtained by generating <n_individuals> random individuals with depth <max_depth> and analyzing those.

Parameters:
  • n_individuals (int)

  • max_depth (int)

get_grammar_properties_summary()

Returns a summary of grammar properties:

  • A depth range (minimum depth and maximum depth of the grammar)

  • The number of Non-Terminal symbols in the grammar

  • A summary of production statistics:
    • Frequency of Productions in the Right Hand side

    • The number of recursive productions

    • Per non-terminal, all the alternative productions

    • The total number of productions

    • The average number of productions per non-terminal

    • The average non-terminals per production for each non-terminal

Return type:

GrammarSummary

class geneticengine.representations.stackgggp.RandomSource

Bases: abc.ABC

Helper class that provides a standard way to create an ABC using inheritance.

abstract randint(min, max, prod='')
Parameters:
  • min (int)

  • max (int)

  • prod (str)

Return type:

int

abstract random_float(min, max, prod='')
Parameters:
  • min (float)

  • max (float)

  • prod (str)

Return type:

float

choice(choices, prod='')
Parameters:
  • choices (list[T])

  • prod (str)

Return type:

T

choice_weighted(choices, weights, prod='')
Parameters:
  • choices (list[T])

  • weights (list[float])

  • prod (str)

Return type:

T

shuffle(lst)
Parameters:

lst (list[T])

pop_random(lst)
Parameters:

lst (list[T])

Return type:

T

random_bool(prod='')
Parameters:

prod (str)

Return type:

bool

normalvariate(mean, sigma, prod)
Parameters:
  • mean (float)

  • sigma (float)

  • prod (str)

Return type:

float

class geneticengine.representations.stackgggp.RepresentationWithCrossover

Bases: Generic[g]

Abstract base class for generic types.

A generic type is typically declared by inheriting from this class parameterized with one or more type variables. For example, a generic mapping type might be defined as:

class Mapping(Generic[KT, VT]):
    def __getitem__(self, key: KT) -> VT:
        ...
    # Etc.

This class can then be used as follows:

def lookup_name(mapping: Mapping[KT, VT], key: KT, default: VT) -> VT:
    try:
        return mapping[key]
    except KeyError:
        return default
abstract crossover(random, parent1, parent2)
Parameters:
Return type:

tuple[g, g]

class geneticengine.representations.stackgggp.RepresentationWithMutation

Bases: Generic[g]

Abstract base class for generic types.

A generic type is typically declared by inheriting from this class parameterized with one or more type variables. For example, a generic mapping type might be defined as:

class Mapping(Generic[KT, VT]):
    def __getitem__(self, key: KT) -> VT:
        ...
    # Etc.

This class can then be used as follows:

def lookup_name(mapping: Mapping[KT, VT], key: KT, default: VT) -> VT:
    try:
        return mapping[key]
    except KeyError:
        return default
abstract mutate(random, internal)
Parameters:
Return type:

g

class geneticengine.representations.stackgggp.Representation

Bases: Generic[g, p]

Abstract base class for generic types.

A generic type is typically declared by inheriting from this class parameterized with one or more type variables. For example, a generic mapping type might be defined as:

class Mapping(Generic[KT, VT]):
    def __getitem__(self, key: KT) -> VT:
        ...
    # Etc.

This class can then be used as follows:

def lookup_name(mapping: Mapping[KT, VT], key: KT, default: VT) -> VT:
    try:
        return mapping[key]
    except KeyError:
        return default
abstract create_genotype(random, **kwargs)
Parameters:

random (geneticengine.random.sources.RandomSource)

Return type:

g

abstract genotype_to_phenotype(internal)
Parameters:

internal (g)

Return type:

p

class geneticengine.representations.stackgggp.TreeNode

Bases: Protocol

Base class for protocol classes.

Protocol classes are defined as:

class Proto(Protocol):
    def meth(self) -> int:
        ...

Such classes are primarily used with static type checkers that recognize structural subtyping (static duck-typing), for example:

class C:
    def meth(self) -> int:
        return 0

def func(x: Proto) -> int:
    return x.meth()

func(C())  # Passes static type check

See PEP 544 for details. Protocol classes decorated with @typing.runtime_checkable act as simple-minded runtime protocols that check only the presence of given attributes, ignoring their type signatures. Protocol classes can be generic, they are defined as:

class GenProto(Protocol[T]):
    def meth(self) -> T:
        ...
gengy_labeled: bool
gengy_distance_to_term: int
gengy_nodes: int
gengy_weighted_nodes: int
gengy_types_this_way: dict[type, list[Any]]
gengy_init_values: tuple[Any]
geneticengine.representations.stackgggp.get_arguments(n)
Parameters:

n – production

Returns:

list((argname, argtype))

Return type:

list[tuple[str, type]]

geneticengine.representations.stackgggp.get_generic_parameter(ty)

When given Annotated[T, <annotations>] or List[T], this function returns T.

Parameters:

ty (type[Any])

Return type:

type

geneticengine.representations.stackgggp.get_generic_parameters(ty)

Annotated[T, <annotations>] or List[T], this function returns Dict[T,]

Parameters:

ty (type[Any])

Return type:

list[type]

geneticengine.representations.stackgggp.is_abstract(t)

Returns whether a class is a Protocol or AbstractBaseClass.

Parameters:

t (type)

Return type:

bool

geneticengine.representations.stackgggp.is_generic_list(ty)

Returns whether a type is List[T] for any T.

Parameters:

ty (type[Any])

geneticengine.representations.stackgggp.is_metahandler(ty)

Returns if type is a metahandler. AnnotatedType[int, IntRange(3,10)] is an example of a Metahandler.

Verification is done using the __metadata__, which is the first argument of Annotated

Parameters:

ty (type)

Return type:

bool

class geneticengine.representations.stackgggp.Genotype
dna: list[int]
class geneticengine.representations.stackgggp.ListWrapper

Bases: geneticengine.random.sources.RandomSource

dna: list[int]
index: int = 0
randint(min, max, prod='')
Parameters:
  • min (int)

  • max (int)

  • prod (str)

Return type:

int

random_float(min, max, prod='')
Parameters:
  • min (float)

  • max (float)

  • prod (str)

Return type:

float

geneticengine.representations.stackgggp.add_to_stacks(stacks, t, v)
Parameters:
  • stacks (dict[type, list[Any]])

  • t (type)

  • v (Any)

geneticengine.representations.stackgggp.create_tree_using_stacks(g, r, max_depth=10)
Parameters:
class geneticengine.representations.stackgggp.StackBasedGGGPRepresentation(grammar, max_depth, gene_length=256)

Bases: geneticengine.representations.api.Representation[Genotype, geneticengine.solutions.tree.TreeNode], geneticengine.representations.api.RepresentationWithMutation[Genotype], geneticengine.representations.api.RepresentationWithCrossover[Genotype]

This representation uses a list of integers to guide the generation of trees in the phenotype.

Parameters:
create_genotype(random, **kwargs)
Parameters:

random (geneticengine.random.sources.RandomSource)

Return type:

Genotype

genotype_to_phenotype(genotype)
Parameters:

genotype (Genotype)

Return type:

geneticengine.solutions.tree.TreeNode

mutate(random, internal, **kwargs)
Parameters:
Return type:

Genotype

crossover(random, parent1, parent2, **kwargs)
Parameters:
Return type:

tuple[Genotype, Genotype]