AForge.Genetic.GPCustomTree.Crossover C# (CSharp) Метод

Crossover() публичный Метод

Crossover operator.

The method performs crossover between two chromosomes – interchanging randomly selected sub trees.

public Crossover ( IChromosome pair ) : void
pair IChromosome Pair chromosome to crossover with.
Результат void
        public override void Crossover(IChromosome pair)
        {
            GPCustomTree p = (GPCustomTree)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
                {
                    GPCustomTreeNode node = root;

                    for (; ; )
                    {
                        // choose random child
                        int r = rand.Next(node.Gene.ArgumentsCount);
                        GPCustomTreeNode child = (GPCustomTreeNode)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);
            }
        }

Usage Example

Пример #1
0
        public MelodySequence Generate()
        {
            NoteGene baseGene = new NoteGene(GPGeneType.Function);
            baseGene.Function = NoteGene.FunctionTypes.Concatenation;
            GPCustomTree tree = new GPCustomTree(baseGene);
            if (base_seq != null)
            {
                tree.Generate(base_seq.ToArray());
                tree.Mutate();
                tree.Crossover(tree);

                int length = base_seq.Length;
                int depth = (int)Math.Ceiling(Math.Log(length, 2));
                GPCustomTree.MaxInitialLevel = depth - 2;
                GPCustomTree.MaxLevel = depth + 5;
            }
            var selection = new EliteSelection();
            pop = new Population(30, tree, fitnessFunction, selection);

            pop.AutoShuffling = true;
            pop.CrossoverRate = 0.9;
            pop.MutationRate = 0.1;

            int percentage = MaxGenerations / 100;

            for (int i = 0; i < MaxGenerations; i++)
            {
                if(i>percentage)
                {
                    if (OnPercentage != null)
                        OnPercentage(this, i, pop.FitnessAvg);
                    percentage += MaxGenerations / 100;
                }

                pop.RunEpoch();
                if ((int)(i) % 100 == 0)
                    Console.WriteLine(i / (float)MaxGenerations * 100 + "% : " + pop.FitnessAvg);
            }

            GPCustomTree best = pop.BestChromosome as GPCustomTree;
            var notes = best.GenerateNotes();
            return new MelodySequence(notes);
        }