geneticengine.representations.stackgggp

This module relies on much of the GE implementation.

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

Exceptions

GeneticEngineError

Common base class for all non-exit exceptions.

Classes

Grammar

RandomSource

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

RepresentationWithCrossover

RepresentationWithMutation

Representation

TreeNode

Base class for protocol classes.

Genotype

ListWrapper

StackBasedGGGPRepresentation

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

Functions

apply_constructor(ty, args)

get_arguments(n)

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_union(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)

find_element_that_meets_mh(stack, metahandler)

create_tree_using_stacks(g, r[, failures_limit])

Package Contents

exception geneticengine.representations.stackgggp.GeneticEngineError

Bases: Exception

Common base class for all non-exit exceptions.

Initialize self. See help(type(self)) for accurate signature.

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]]
considered_subtypes = []
expansion_depthing = False
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__()
get_all_symbols()

All symbols in the current grammar, including terminals.

Return type:

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

collect_types(ty)
Parameters:

ty (type)

get_all_mentioned_symbols()
Return 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)
is_reachable(t1, t2)
Parameters:
  • t1 (Type)

  • t2 (Type)

Return type:

bool

reaches_leaf(t, visited=None)

Returns whether a given type reaches a leaf type, or None if it causes a loop.

Loops should be ignored only if there is an alternative path.

Parameters:
  • t (Type)

  • visited (set | None)

Return type:

bool | None

usable_grammar()

Returns a subset of the grammar that is actually reachable.

Return type:

Grammar

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.

abstractmethod randint(min, max)
Parameters:
  • min (int)

  • max (int)

Return type:

int

abstractmethod random_float(min, max)
Parameters:
  • min (float)

  • max (float)

Return type:

float

choice(choices)
Parameters:

choices (list[T])

Return type:

T

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

  • weights (list[float])

Return type:

T

shuffle(lst)
Parameters:

lst (list[T])

pop_random(lst)
Parameters:

lst (list[T])

Return type:

T

random_bool()
Return type:

bool

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

  • sigma (float)

Return type:

float

class geneticengine.representations.stackgggp.RepresentationWithCrossover

Bases: Generic[g]

abstractmethod crossover(random, parent1, parent2)
Parameters:
Return type:

tuple[g, g]

class geneticengine.representations.stackgggp.RepresentationWithMutation

Bases: Generic[g]

abstractmethod mutate(random, genotype, **kwargs)
Parameters:
Return type:

g

class geneticengine.representations.stackgggp.Representation

Bases: Generic[g, p]

abstractmethod create_genotype(random, **kwargs)
Parameters:

random (geneticengine.random.sources.RandomSource)

Return type:

g

abstractmethod genotype_to_phenotype(genotype)
Parameters:

genotype (g)

Return type:

p

geneticengine.representations.stackgggp.apply_constructor(ty, args)
Parameters:
  • ty (Type)

  • args (list[Any])

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[T](Protocol):
    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]
gengy_synthesis_context: LocalSynthesisContext
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_union(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)
Parameters:
  • min (int)

  • max (int)

Return type:

int

random_float(min, max)
Parameters:
  • min (float)

  • max (float)

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.find_element_that_meets_mh(stack, metahandler)
geneticengine.representations.stackgggp.create_tree_using_stacks(g, r, failures_limit=100)
Parameters:
class geneticengine.representations.stackgggp.StackBasedGGGPRepresentation(grammar, gene_length=1024, failures_limit=100)

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:
grammar
gene_length = 1024
failures_limit = 100
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, genotype, **kwargs)
Parameters:
Return type:

Genotype

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

tuple[Genotype, Genotype]