Tree chromosome represents a tree of genes, which is is used for different tasks of Genetic Programming (GP).

This type of chromosome represents a tree, where each node is represented by GPTreeNode containing IGPGene. Depending on type of genes used to build the tree, it may represent different types of expressions aimed to solve different type of tasks. For example, a particular implementation of IGPGene interface may represent simple algebraic operations and their arguments.

See documentation to IGPGene implementations for additional information about possible Genetic Programming trees.

Inheritance: ChromosomeBase
        /// <summary>
        /// Crossover operator.
        /// </summary>
        ///
        /// <param name="pair">Pair chromosome to crossover with.</param>
        ///
        /// <remarks><para>The method performs crossover between two chromosomes – interchanging
        /// randomly selected sub trees.</para></remarks>
        ///
        public override void Crossover(IChromosome pair)
        {
            var rand           = Generator.Random;
            GPTreeChromosome p = (GPTreeChromosome)pair;

            // check for correct pair
            if (p != null)
            {
                // do we need to use root node for crossover ?
                if ((root.Children == null) || (rand.Next(maxLevel) == 0))
                {
                    // give the root to the pair and use pair's part as a new root
                    root = p.RandomSwap(root);
                }
                else
                {
                    GPTreeNode node = root;

                    for (; ;)
                    {
                        // choose random child
                        int        r     = rand.Next(node.Gene.ArgumentsCount);
                        GPTreeNode child = (GPTreeNode)node.Children[r];

                        // swap the random node, if it is an end node or
                        // random generator "selected" this node
                        if ((child.Children == null) || (rand.Next(maxLevel) == 0))
                        {
                            // swap the node with pair's one
                            node.Children[r] = p.RandomSwap(child);
                            break;
                        }

                        // go further by tree
                        node = child;
                    }
                }
                // trim both of them
                Trim(root, maxLevel);
                Trim(p.root, maxLevel);
            }
        }
Exemplo n.º 2
0
 /// <summary>
 /// Initializes a new instance of the <see cref="GPTreeChromosome"/> class.
 /// </summary>
 ///
 /// <param name="source">Source genetic tree to clone from.</param>
 ///
 /// <remarks><para>This constructor creates new genetic tree as a copy of the
 /// specified <paramref name="source"/> tree.</para></remarks>
 ///
 protected GPTreeChromosome(GPTreeChromosome source)
 {
     root    = (GPTreeNode)source.root.Clone();
     fitness = source.fitness;
 }
Exemplo n.º 3
0
 /// <summary>
 /// Initializes a new instance of the <see cref="GPTreeChromosome"/> class.
 /// </summary>
 /// 
 /// <param name="source">Source genetic tree to clone from.</param>
 /// 
 /// <remarks><para>This constructor creates new genetic tree as a copy of the
 /// specified <paramref name="source"/> tree.</para></remarks>
 /// 
 protected GPTreeChromosome(GPTreeChromosome source)
 {
     root = (GPTreeNode)source.root.Clone();
     fitness = source.fitness;
 }