Ejemplo n.º 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
Ejemplo n.º 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
Ejemplo n.º 3
0
    def create_grow_tree(depth):
        """ Creates a tree using the grow method with depth `depth`. Note
        that since grow ends when all leaf nodes are 0-arity, it is
        possible that the tree generated by this method has a depth
        less than `depth`.

        Args:
            depth: int

        Returns:
            Node instance
        """
        func = Function.random_function()
        root = Node(func)

        while True:
            if root.grow(depth) is None:
                break

        return root
Ejemplo n.º 4
0
    def create_grow_tree(depth):
        """ Creates a tree using the grow method with depth `depth`. Note
        that since grow ends when all leaf nodes are 0-arity, it is
        possible that the tree generated by this method has a depth
        less than `depth`.

        Args:
            depth: int

        Returns:
            Node instance
        """
        func = Function.random_function()
        root = Node(func)

        while True:
            if root.grow(depth) is None:
                break

        return root
Ejemplo n.º 5
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
Ejemplo n.º 6
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