geneticengine.grammar.metahandlers.smt

Submodules

Package Contents

Classes

Grammar

RandomSource

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

MetaHandlerGenerator

MetaHandlers are type refinements.

SMT

MetaHandlers are type refinements.

Functions

is_metahandler(ty)

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

simplify_type(t)

Attributes

p_expr

class geneticengine.grammar.metahandlers.smt.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.grammar.metahandlers.smt.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.grammar.metahandlers.smt.MetaHandlerGenerator

Bases: Protocol

MetaHandlers are type refinements.

They override the generation procedure of the base type.

generate(r, g, rec, new_symbol, depth, base_type, context)

Generates an instance of type base_type, according to some criterion.

Parameters:
  • r (Source) – Random source for generation

  • g (Grammar) – Grammar to follow in the generation

  • rec (Callable[[int, Type], Any]) – The method to generate a new instance of type and maximum depth d

  • depth (int) – the current depth budget

  • base_type (Type) – The inner type being annotated

  • argname (str) – The name of the field of the parent object which is being generated

  • context (dict[str, str]) – The names and types of all fields in the parent object

  • context

geneticengine.grammar.metahandlers.smt.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

geneticengine.grammar.metahandlers.smt.p_expr
geneticengine.grammar.metahandlers.smt.simplify_type(t)
Parameters:

t (type)

Return type:

type

class geneticengine.grammar.metahandlers.smt.SMT(restriction_as_str='true==true')

Bases: geneticengine.grammar.metahandlers.base.MetaHandlerGenerator

MetaHandlers are type refinements.

They override the generation procedure of the base type.

generate(r, g, rec, new_symbol, depth, base_type, context)

Generates an instance of type base_type, according to some criterion.

Parameters:
  • r (Source) – Random source for generation

  • g (Grammar) – Grammar to follow in the generation

  • rec (Callable[[int, Type], Any]) – The method to generate a new instance of type and maximum depth d

  • depth (int) – the current depth budget

  • base_type (Type) – The inner type being annotated

  • argname (str) – The name of the field of the parent object which is being generated

  • context (dict[str, str]) – The names and types of all fields in the parent object

  • context

__repr__()

Return repr(self).