Пример #1
0
    def grow(self, depth=None):
        """ Grows a random child node by 1, limited by `depth` (if provided)
            and arity restrictions.  Returns the new node (or None if no
            node can be expanded).

        Args:
            depth: int (default=None)

        Returns:
            Node instance (or None)
        """
        # Creates a random permutation of child nodes
        nodes_depths = list(self.descendants_and_self_with_depths())
        shuffle(nodes_depths)

        for node, d in nodes_depths:
            if len(node.children) >= node.func.arity:
                continue

            if d >= depth:
                continue

            if d == depth - 1:
                func = Function.random_terminal()
            else:
                func = Function.random_function()
            child = Node(func)
            node.add_child(child)
            return child

        # If no children can be expanded, return None
        return None
Пример #2
0
    def grow(self, depth=None):
        """ Grows a random child node by 1, limited by `depth` (if provided)
            and arity restrictions.  Returns the new node (or None if no
            node can be expanded).

        Args:
            depth: int (default=None)

        Returns:
            Node instance (or None)
        """
        # Creates a random permutation of child nodes
        nodes_depths = list(self.descendants_and_self_with_depths())
        shuffle(nodes_depths)

        for node, d in nodes_depths:
            if len(node.children) >= node.func.arity:
                continue

            if d >= depth:
                continue

            if d == depth - 1:
                func = Function.random_terminal()
            else:
                func = Function.random_function()
            child = Node(func)
            node.add_child(child)
            return child

        # If no children can be expanded, return None
        return None
Пример #3
0
    def create_full_tree(depth):
        """ Creates a tree using the full method with depth `depth`.
            Returns the root node.

        Args:
            depth: int

        Returns:
            Node instance
        """
        if depth == 0:
            # Generate a leaf node
            terminal = Function.random_terminal()
            node = Node(terminal)
            return node
        else:
            # Generate an intermediate node
            func = Function.random_function()
            node = Node(func)

            for _ in range(func.arity):
                node.add_child(TreeMethods.create_full_tree(depth - 1))
            return node
Пример #4
0
    def create_full_tree(depth):
        """ Creates a tree using the full method with depth `depth`.
            Returns the root node.

        Args:
            depth: int

        Returns:
            Node instance
        """
        if depth == 0:
            # Generate a leaf node
            terminal = Function.random_terminal()
            node = Node(terminal)
            return node
        else:
            # Generate an intermediate node
            func = Function.random_function()
            node = Node(func)

            for _ in range(func.arity):
                node.add_child(TreeMethods.create_full_tree(depth - 1))
            return node