Class TreeChromosome<T>

java.lang.Object
net.bmahe.genetics4j.core.chromosomes.TreeChromosome<T>
Type Parameters:
T - the type of data stored in tree nodes
All Implemented Interfaces:
Chromosome

public class TreeChromosome<T> extends Object implements Chromosome
A chromosome implementation that represents genetic information as a tree structure.

TreeChromosome is essential for genetic programming and other evolutionary approaches that require hierarchical or nested representations. The tree structure allows for variable-length solutions and naturally supports recursive data types and operations.

This chromosome type is particularly suitable for:

  • Genetic Programming: Evolving mathematical expressions, programs, and functions
  • Decision trees: Classification and regression tree learning
  • Parse trees: Grammar evolution and language processing
  • Hierarchical structures: Neural network topologies, circuit designs
  • Symbolic regression: Finding mathematical models that fit data
  • Rule evolution: Evolving conditional logic and decision rules

Key characteristics:

  • Variable structure: Tree depth and branching can vary between individuals
  • Hierarchical representation: Natural support for nested and recursive structures
  • Type-safe nodes: Generic type parameter ensures consistent node data types
  • Immutable backbone: Tree structure is fixed after construction

The chromosome contains a single root node that represents the entire tree structure. The tree size (total number of nodes) is calculated recursively from the root node. For the purpose of the Chromosome interface, this counts as one "allele" since it represents a single cohesive genetic unit.

Tree genetic operators typically work by:

  • Crossover: Exchanging subtrees between parent chromosomes
  • Mutation: Replacing subtrees or individual nodes
  • Growth/Pruning: Adding or removing branches to control complexity
See Also:
  • Field Details

  • Constructor Details

    • TreeChromosome

      public TreeChromosome(TreeNode<T> _root)
      Creates a new tree chromosome with the specified root node.
      Parameters:
      _root - the root node of the tree structure
      Throws:
      IllegalArgumentException - if root is null
  • Method Details

    • getNumAlleles

      public int getNumAlleles()
      Returns the number of alleles in this chromosome.

      For tree chromosomes, this always returns 1 since the entire tree is considered a single genetic unit (allele). To get the total number of nodes in the tree, use getSize().

      Specified by:
      getNumAlleles in interface Chromosome
      Returns:
      1, representing the tree as a single allele
    • getRoot

      public TreeNode<T> getRoot()
      Returns the root node of this tree chromosome.
      Returns:
      the root node containing the entire tree structure
    • getSize

      public int getSize()
      Returns the total number of nodes in the tree.

      This method recursively counts all nodes in the tree starting from the root node, including internal nodes and leaves.

      Returns:
      the total number of nodes in the tree
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • equals

      public boolean equals(Object obj)
      Overrides:
      equals in class Object
    • toString

      public String toString()
      Overrides:
      toString in class Object