def grow(self, min_depth=1, max_depth=3): """It creates a random tree based on the GROW algorithm. References: S. Lukw. Two Fast Tree-Creation Algorithms for Genetic Programming. IEEE Transactions on Evolutionary Computation (2000). Args: min_depth (int): Minimum depth of the tree. max_depth (int): Maximum depth of the tree. Returns: A random tree based on the GROW algorithm. """ # Re-initialize the terminals to provide diversity self._initialize_terminals() # If minimum depth equals the maximum depth if min_depth == max_depth: # Generates a terminal identifier terminal_id = r.generate_integer_random_number(0, self.n_terminals) # Return the terminal node with its id and corresponding position return Node(terminal_id, 'TERMINAL', self.terminals[terminal_id].position) # Generates a node identifier node_id = r.generate_integer_random_number(0, len(self.functions) + self.n_terminals) # If the identifier is a terminal if node_id >= len(self.functions): # Gathers its real identifier terminal_id = node_id - len(self.functions) # Return the terminal node with its id and corresponding position return Node(terminal_id, 'TERMINAL', self.terminals[terminal_id].position) # Generates a new function node function_node = Node(self.functions[node_id], 'FUNCTION') # For every possible function argument for i in range(c.N_ARGS_FUNCTION[self.functions[node_id]]): # Calls recursively the grow function and creates a temporary node node = self.grow(min_depth + 1, max_depth) # If it is not the root if not i: # The left child receives the temporary node function_node.left = node # If it is the first node else: # The right child receives the temporary node function_node.right = node # Flag to identify whether the node is a left child node.flag = False # The parent of the temporary node is the function node node.parent = function_node return function_node
import numpy as np from opytimizer.core.node import Node # Creating two new Nodes n1 = Node(name='0', type='TERMINAL', value=np.array(1)) n2 = Node(name='1', type='TERMINAL', value=np.array(2)) # Outputting information about one of the nodes print(n1) print(f'Post Order: {n1.post_order} | Size: {n1.n_nodes}.') # Additionally, one can stack nodes to create a tree t = Node(name='SUM', type='FUNCTION', left=n1, right=n2) # Defining `n1` and `n2` parent as `t` n1.parent = t n2.parent = t # Outputting information about the tree print(t) print( f'Post Order: {t.post_order} | Size: {t.n_nodes} | Minimum Depth: {t.min_depth} | Maximum Depth: {t.max_depth}.' )
import numpy as np from opytimizer.core.node import Node # Creates two new Nodes n1 = Node(name="0", category="TERMINAL", value=np.array(1)) n2 = Node(name="1", category="TERMINAL", value=np.array(2)) # Outputting information about one of the nodes print(n1) print(f"Post Order: {n1.post_order} | Size: {n1.n_nodes}.") # Additionally, one can stack nodes to create a tree t = Node(name="SUM", category="FUNCTION", left=n1, right=n2) # Defines `n1` and `n2` parent as `t` n1.parent = t n2.parent = t # Outputting information about the tree print(t) print( f"Post Order: {t.post_order} | Size: {t.n_nodes} | Minimum Depth: {t.min_depth} | Maximum Depth: {t.max_depth}." )