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

Generate() защищенный Метод

Generate chromosome's subtree of specified level.
protected Generate ( GPCustomTreeNode node, int level ) : void
node GPCustomTreeNode Sub tree's node to generate.
level int Sub tree's level to generate.
Результат void
        protected void Generate(GPCustomTreeNode node, int level)
        {
            if(generator != null)
            {
                if (level == 0)
                {
                    node.Gene = root.Gene.CreateNew(GPGeneType.Argument);
                }
                else
                {
                    var notes = generator.Generate();
                    List<Note> notes_cut = new List<Note>();
                    int j = 0;
                    int max = (int)Math.Pow(2,level-1);
                    foreach(Note n in notes.Notes)
                    {
                        if(j>max)
                        {
                            if (n.Velocity <= 0 || n.Pitch < 0)
                                break;
                        }
                        notes_cut.Add(n);
                    }
                    node.Generate(notes_cut);
                }
                return;
            }

            /// OLD !!!
            // create gene for the node
            if (level == 0)
            {
                // the gene should be an argument
                node.Gene = root.Gene.CreateNew(GPGeneType.Argument);
            }
            else
            {
                // the gene can be function or argument
                node.Gene = root.Gene.CreateNew();
            }

            // add children
            if (node.Gene.ArgumentsCount != 0)
            {
                node.Children = new List<GPCustomTreeNode>();
                for (int i = 0; i < node.Gene.ArgumentsCount; i++)
                {
                    // create new child
                    GPCustomTreeNode child = new GPCustomTreeNode();
                    Generate(child, level - 1);
                    // add the new child
                    node.Children.Add(child);
                }
            }
        }

Same methods

GPCustomTree::Generate ( ) : void
GPCustomTree::Generate ( IEnumerable notes ) : void

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);
        }