geneticengine.grammar.grammar

Module Contents

Classes

DepthRange

ProductionSummary

GrammarSummary

Grammar

Functions

extract_grammar(considered_subtypes, starting_symbol)

The extract_grammar takes in all the productions of the grammar (nodes)

exception geneticengine.grammar.grammar.InvalidGrammarException

Bases: Exception

Exception to be raised when a passed node is neither abstract nor typed.

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

class geneticengine.grammar.grammar.DepthRange

Bases: NamedTuple

depth_min: int
depth_max: int
class geneticengine.grammar.grammar.ProductionSummary

Bases: NamedTuple

production_frequencies: dict[int, int]
number_of_recursive_productions: int
alternatives: dict[Any, list]
total_productions: int
average_productions_per_non_terminal: float
average_non_terminals_per_production: dict
class geneticengine.grammar.grammar.GrammarSummary

Bases: NamedTuple

depth_range: DepthRange
number_of_non_terminals: int
production_stats: ProductionSummary
class geneticengine.grammar.grammar.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

geneticengine.grammar.grammar.extract_grammar(considered_subtypes, starting_symbol, expansion_depthing=False)

The extract_grammar takes in all the productions of the grammar (nodes) and a starting symbol (starting_symbol). It goes through all the nodes and constructs a complete grammar that can then be used for search algorithms such as Genetic Programming and Hill Climbing.

Parameters:
  • nodes (list) – A list of objects representing tree nodes. Make sure that any node can be produced be the starting symbol.

  • starting_symbol (object) – The starting symbol of each tree. Makes sure every generated tree by the returned grammar starts with this symbol. Make sure that the starting symbol can produce any object of nodes.

  • considered_subtypes (list[type])

  • expansion_depthing (bool)

Returns:

The grammar