geneticengine.grammar.grammar

Attributes

INF_VALUE

Exceptions

InvalidGrammarException

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

Classes

DepthRange

ProductionSummary

GrammarSummary

Grammar

Functions

is_mentioned_by(target, ty)

all_with_recursion(s)

Returns whether there is a True in all elements. Recursion (None) is allows if there is at least another path.

extract_grammar(considered_subtypes, starting_symbol)

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

Module Contents

geneticengine.grammar.grammar.INF_VALUE = 1000000
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
geneticengine.grammar.grammar.is_mentioned_by(target, ty)
Parameters:
  • target (Type)

  • ty (Type)

Return type:

bool

geneticengine.grammar.grammar.all_with_recursion(s)

Returns whether there is a True in all elements. Recursion (None) is allows if there is at least another path.

Parameters:

s (list[bool | None])

Return type:

bool

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