geneticengine.grammar

Submodules

Attributes

__ALL__

Classes

Grammar

Functions

extract_grammar(considered_subtypes, starting_symbol)

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

Package Contents

class geneticengine.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]]
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

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

geneticengine.grammar.__ALL__