geneticengine.representations.stackgggp ======================================= .. py:module:: geneticengine.representations.stackgggp .. autoapi-nested-parse:: This module relies on much of the GE implementation. The only difference is the genotype to phenotype mapping, which uses stacks. Exceptions ---------- .. autoapisummary:: geneticengine.representations.stackgggp.GeneticEngineError Classes ------- .. autoapisummary:: geneticengine.representations.stackgggp.Grammar geneticengine.representations.stackgggp.RandomSource geneticengine.representations.stackgggp.RepresentationWithCrossover geneticengine.representations.stackgggp.RepresentationWithMutation geneticengine.representations.stackgggp.Representation geneticengine.representations.stackgggp.TreeNode geneticengine.representations.stackgggp.Genotype geneticengine.representations.stackgggp.ListWrapper geneticengine.representations.stackgggp.StackBasedGGGPRepresentation Functions --------- .. autoapisummary:: geneticengine.representations.stackgggp.apply_constructor geneticengine.representations.stackgggp.get_arguments geneticengine.representations.stackgggp.get_generic_parameter geneticengine.representations.stackgggp.get_generic_parameters geneticengine.representations.stackgggp.is_abstract geneticengine.representations.stackgggp.is_generic_list geneticengine.representations.stackgggp.is_union geneticengine.representations.stackgggp.is_metahandler geneticengine.representations.stackgggp.add_to_stacks geneticengine.representations.stackgggp.find_element_that_meets_mh geneticengine.representations.stackgggp.create_tree_using_stacks Package Contents ---------------- .. py:exception:: GeneticEngineError Bases: :py:obj:`Exception` Common base class for all non-exit exceptions. Initialize self. See help(type(self)) for accurate signature. .. py:class:: Grammar(starting_symbol, considered_subtypes = None, expansion_depthing = False) .. py:attribute:: starting_symbol :type: type .. py:attribute:: alternatives :type: dict[type, list[type]] .. py:attribute:: distanceToTerminal :type: dict[Any, int] .. py:attribute:: all_nodes :type: set[type] .. py:attribute:: recursive_prods :type: set[type] .. py:attribute:: terminals :type: set[type] .. py:attribute:: non_terminals :type: set[type] .. py:attribute:: abstract_dist_to_t :type: dict[type, dict[type, int]] .. py:attribute:: considered_subtypes :value: [] .. py:attribute:: expansion_depthing :value: False .. py:method:: validate() .. py:method:: register_alternative(nonterminal, nodetype) Register a production A->B Call multiple times with same A to register many possible alternatives. .. py:method:: register_type(ty) .. py:method:: __repr__() .. py:method:: get_all_symbols() All symbols in the current grammar, including terminals. .. py:method:: collect_types(ty) .. py:method:: get_all_mentioned_symbols() .. py:method:: get_distance_to_terminal(ty) Returns the current distance to terminal of a given type. .. py:method:: get_min_tree_depth() Returns the minimum depth a tree must have. .. py:method:: get_max_node_depth() Returns the maximum minimum depth a node can have. .. py:method:: preprocess() Computes distanceToTerminal via a fixpoint algorithm. .. py:method:: get_weights() .. py:method:: update_weights(learning_rate, extra_weights) .. py:method:: is_reachable(t1, t2) .. py:method:: 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. .. py:method:: usable_grammar() Returns a subset of the grammar that is actually reachable. .. py:method:: 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 .. py:class:: RandomSource Bases: :py:obj:`abc.ABC` Helper class that provides a standard way to create an ABC using inheritance. .. py:method:: randint(min, max) :abstractmethod: .. py:method:: random_float(min, max) :abstractmethod: .. py:method:: choice(choices) .. py:method:: choice_weighted(choices, weights) .. py:method:: shuffle(lst) .. py:method:: pop_random(lst) .. py:method:: random_bool() .. py:method:: normalvariate(mean, sigma) .. py:class:: RepresentationWithCrossover Bases: :py:obj:`Generic`\ [\ :py:obj:`g`\ ] .. py:method:: crossover(random, parent1, parent2) :abstractmethod: .. py:class:: RepresentationWithMutation Bases: :py:obj:`Generic`\ [\ :py:obj:`g`\ ] .. py:method:: mutate(random, genotype, **kwargs) :abstractmethod: .. py:class:: Representation Bases: :py:obj:`Generic`\ [\ :py:obj:`g`\ , :py:obj:`p`\ ] .. py:method:: create_genotype(random, **kwargs) :abstractmethod: .. py:method:: genotype_to_phenotype(genotype) :abstractmethod: .. py:function:: apply_constructor(ty, args) .. py:class:: TreeNode Bases: :py:obj:`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: ... .. py:attribute:: gengy_labeled :type: bool .. py:attribute:: gengy_distance_to_term :type: int .. py:attribute:: gengy_nodes :type: int .. py:attribute:: gengy_weighted_nodes :type: int .. py:attribute:: gengy_types_this_way :type: dict[type, list[Any]] .. py:attribute:: gengy_init_values :type: tuple[Any] .. py:attribute:: gengy_synthesis_context :type: LocalSynthesisContext .. py:function:: get_arguments(n) :param n: production :return: list((argname, argtype)) .. py:function:: get_generic_parameter(ty) When given Annotated[T, ] or List[T], this function returns T. .. py:function:: get_generic_parameters(ty) Annotated[T, ] or List[T], this function returns Dict[T,] .. py:function:: is_abstract(t) Returns whether a class is a Protocol or AbstractBaseClass. .. py:function:: is_generic_list(ty) Returns whether a type is List[T] for any T. .. py:function:: is_union(ty) Returns whether a type is List[T] for any T. .. py:function:: 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 .. py:class:: Genotype .. py:attribute:: dna :type: list[int] .. py:class:: ListWrapper Bases: :py:obj:`geneticengine.random.sources.RandomSource` .. py:attribute:: dna :type: list[int] .. py:attribute:: index :type: int :value: 0 .. py:method:: randint(min, max) .. py:method:: random_float(min, max) .. py:function:: add_to_stacks(stacks, t, v) .. py:function:: find_element_that_meets_mh(stack, metahandler) .. py:function:: create_tree_using_stacks(g, r, failures_limit=100) .. py:class:: StackBasedGGGPRepresentation(grammar, gene_length = 1024, failures_limit = 100) Bases: :py:obj:`geneticengine.representations.api.Representation`\ [\ :py:obj:`Genotype`\ , :py:obj:`geneticengine.solutions.tree.TreeNode`\ ], :py:obj:`geneticengine.representations.api.RepresentationWithMutation`\ [\ :py:obj:`Genotype`\ ], :py:obj:`geneticengine.representations.api.RepresentationWithCrossover`\ [\ :py:obj:`Genotype`\ ] This representation uses a list of integers to guide the generation of trees in the phenotype. .. py:attribute:: grammar .. py:attribute:: gene_length :value: 1024 .. py:attribute:: failures_limit :value: 100 .. py:method:: create_genotype(random, **kwargs) .. py:method:: genotype_to_phenotype(genotype) .. py:method:: mutate(random, genotype, **kwargs) .. py:method:: crossover(random, parent1, parent2, **kwargs)