class_docstr: A GSGP class, inherited from GP.
  
  This is the designed class to define GSGP-related
  variables and methods.
  
  References:
      A. Moraglio, K. Krawiec, and C. G. Johnson.
      Geometric semantic genetic programming.
      Lecture Notes in Computer Science (2012).
  
      G. H. de Rosa, J. P. Papa, and L. P. Papa.
      Feature selection using geometric semantic genetic programming.
      Proceedings of the Genetic and Evolutionary Computation Conference Companion (2017).
class_docstr_tok: ["A","GSGP","class",",","inherited","from","GP",".","This","is","the","designed","class","to","define","GSGP-related","variables","and","methods",".","References",":","A.","Moraglio",",","K.","Krawiec",",","and","C.","G.","Johnson",".","Geometric","semantic","genetic","programming",".","Lecture","Notes","in","Computer","Science","(","2012",")",".","G.","H.","de","Rosa",",","J.","P.","Papa",",","and","L.","P.","Papa",".","Feature","selection","using","geometric","semantic","genetic","programming",".","Proceedings","of","the","Genetic","and","Evolutionary","Computation","Conference","Companion","(","2017",")","."]
docstr: Actually performs the mutation on a single tree.
  
  Args:
      tree: A Node instance to be mutated.
      n_variables: Number of variables.
      max_nodes: Maximum number of nodes to be searched.
  
  Returns:
      (Node): A mutated tree.
docstr_tok: ["Actually","performs","the","mutation","on","a","single","tree",".","Args",":","tree",":","A","Node","instance","to","be","mutated",".","n_variables",":","Number","of","variables",".","max_nodes",":","Maximum","number","of","nodes","to","be","searched",".","Returns",":","(","Node",")",":","A","mutated","tree","."]
code: def _mutate(
      self, tree: Node, n_variables: int, max_nodes: int
  ) -> Node:
      """Actually performs the mutation on a single tree.
  
      Args:
          tree: A Node instance to be mutated.
          n_variables: Number of variables.
          max_nodes: Maximum number of nodes to be searched.
  
      Returns:
          (Node): A mutated tree.
  
      """
  
      mutated_tree = copy.deepcopy(tree)
      mutation_point = int(
          r.generate_uniform_random_number(2, max_nodes)
      )
      sub_tree, _ = mutated_tree.find_node(mutation_point)
  
      # If the mutation point's parent is not a root (this may happen when the mutation point is a function),
      # and find_node() stops at a terminal node whose father is a root
      if sub_tree:
          position = r.generate_uniform_random_number(size=n_variables)
          position_hash = sha1(
              repr(position).encode("ascii")
          ).hexdigest()[:4]
  
          terminal = Node(position_hash, "TERMINAL", position)
  
          operator_id = r.generate_integer_random_number(0, 3)
          if operator_id == 0:
              terminal.value = np.exp(terminal.value)
          elif operator_id == 1:
              terminal.value = np.fabs(np.sin(terminal.value))
          elif operator_id == 2:
              terminal.value = np.cos(np.sin(terminal.value))
  
          if r.generate_uniform_random_number() <= 0.5:
              root = Node("SUM", "FUNCTION")
          else:
              root = Node("MUL", "FUNCTION")
  
          root.parent = None
          root.left = sub_tree
          root.right = terminal
  
          sub_tree.parent = root
          terminal.parent = root
          terminal.flag = False
  
          return root
  
      return mutated_tree
  
file_path: opytimizer/opytimizer/optimizers/evolutionary/gsgp.py
imports: import copy
  from hashlib import sha1
  from typing import Any, Dict, Optional
  import numpy
  import opytimizer.math.general
  import opytimizer.math.random
  from opytimizer.core.node import Node
  from opytimizer.optimizers.evolutionary.gp import GP
  from opytimizer.spaces.tree import TreeSpace
  from opytimizer.utils import logging
label_desc: Use image node_id 3 for calling the GSGP obj's underlying member method code with example usage: obj._mutate(tree, n_variables, max_nodes) and returns: mutated_tree, root