Пример #1
0
 /// <summary>
 /// Initializes a new instance of the <see cref="GPTreeChromosome"/> class.
 /// </summary>
 /// 
 /// <param name="ancestor">A gene, which is used as generator for the genetic tree.</param>
 /// 
 /// <remarks><para>This constructor creates a randomly generated genetic tree,
 /// which has all genes of the same type and properties as the specified <paramref name="ancestor"/>.
 /// </para></remarks>
 /// 
 public GPCustomTree(NoteGene ancestor)
 {
     // make the ancestor gene to be as temporary root of the tree
     root.Gene = ancestor.Clone();
     // call tree regeneration function
     Generate();
 }
Пример #2
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);
        }