public Dna(Dna Pai1, Dna Pai2, Random randNum, int MutationRate)
        {
            //Construtor para o cruzamento na classe Crossover
            this.MutationRate = MutationRate;
            int copyMark    = randNum.Next(Pai1.progression.Length - 1);
            int geneticLoad = copyMark;

            Array.Copy(Pai1.progression, copyMark, progression, copyMark, geneticLoad);
            Array.Copy(Pai2.progression, progression.Length - copyMark,
                       progression, progression.Length - copyMark, geneticLoad);


            Fitness(this);
        }
        private void Mutation(ref Dna Individuo, int MutationRate)
        {
            string[] whiteNotes = new string[8]
            {
                "0", "C", "D", "E", "F", "G", "A", "B"
            };

            num = new Random(Environment.TickCount);

            for (int i = 0; i < MutationRate * 0.10; i++)
            {
                Individuo.progression[num.Next(1, Individuo.progression.Length - 1)] =
                    Array.IndexOf(whiteNotes, whiteNotes[num.Next(1, whiteNotes.Length - 1)]);
            }
        }
        private void Fitness(Dna Individuo)
        {
            int[] scale = new int[8];

            scale[0] = 0;
            scale[1] = Array.IndexOf(whiteNotes, whiteNotes[CheckIndexScale(Individuo.progression[0], 0)]);
            scale[2] = Array.IndexOf(whiteNotes, whiteNotes[CheckIndexScale(Individuo.progression[0], 1)]);
            scale[3] = Array.IndexOf(whiteNotes, whiteNotes[CheckIndexScale(Individuo.progression[0], 2)]);
            scale[4] = Array.IndexOf(whiteNotes, whiteNotes[CheckIndexScale(Individuo.progression[0], 3)]);
            scale[5] = Array.IndexOf(whiteNotes, whiteNotes[CheckIndexScale(Individuo.progression[0], 4)]);
            scale[6] = Array.IndexOf(whiteNotes, whiteNotes[CheckIndexScale(Individuo.progression[0], 5)]);
            scale[7] = Array.IndexOf(whiteNotes, whiteNotes[CheckIndexScale(Individuo.progression[0], 6)]);

            //fourths
            if (Individuo.progression[1] == scale[4])
            {
                fitness += 20;
            }
            // suspended
            if (Individuo.progression[1] == scale[4]
                &&
                Individuo.progression[2] == scale[3]
                ||
                Individuo.progression[2] == scale[4]
                &&
                Individuo.progression[3] == scale[3])
            {
                fitness += 30;
            }
            //fifths
            if (Individuo.progression[1] == scale[5])
            {
                fitness += 20;
            }

            //Leading
            if (Individuo.progression[3] == scale[7])
            {
                fitness += 30;
            }
            //Resolution/Tension
            if (Individuo.progression[3] == scale[5] || Individuo.progression[2] == scale[5] && Individuo.progression[3] == scale[1])
            {
                fitness += 30;
            }
        }
        static void Main()
        {
            //Application.EnableVisualStyles();
            //Application.SetCompatibleTextRenderingDefault(false);
            //Application.Run(new Form1());

            Console.Write("Populacao: ");
            int sizePopulation = int.Parse(Console.ReadLine());

            Console.Write("Mutacao em %: ");
            int mutationRate = int.Parse(Console.ReadLine());

            Console.Write("Numero de Geracoes: ");
            int maxGeneration = int.Parse(Console.ReadLine());

            Console.WriteLine();

            Dna[] Population = new Dna[sizePopulation];

            for (int i = 0; i < sizePopulation; i++)
            {
                Population[i] = new Dna(new Random(Environment.TickCount + i));
            }

            Crossover crossover = new Crossover(mutationRate);

            crossover.Cross(ref Population, maxGeneration);

            for (int i = 0; i < sizePopulation; i++)
            {
                Console.WriteLine(i + " " + Population[i].fitness);
            }

            Console.WriteLine("######\n The best chord is... ");
            for (int i = 0; i < Population[0].GetChordProgression().Length; i++)
            {
                Console.Write(Population[0].GetChordProgression()[i] + " | ");
            }

            Console.ReadLine();
        }
        public void Cross(ref Dna[] Population, int maxGeneration)
        {
            for (int i = 0; i < maxGeneration; i++)
            {
                Classification(ref Population);

                Random num           = new Random(Environment.TickCount + i);
                Dna    newPopulation = new Dna(Population[num.Next(Population.Length - 1)],
                                               Population[num.Next(Population.Length - 1)],
                                               new Random(Environment.TickCount + i),
                                               MutationRate);

                for (int j = Population.Length - 1; j > 0; j--)
                {
                    if (newPopulation.fitness <= Population[j].fitness)
                    {
                        Population[j - 1] = newPopulation;
                        break;
                    }
                }
            }
        }
Example #6
0
        private void CreatePopulation()
        {
            int sizePopulation = int.Parse(txtSizePopulation.Text);
            int mutationRate   = int.Parse(txtMutation.Text);
            int maxGeneration  = 500;

            Dna[] Population = new Dna[sizePopulation];

            for (int i = 0; i < sizePopulation; i++)
            {
                Population[i] = new Dna(new Random(Environment.TickCount + i));
            }

            Crossover crossover = new Crossover(mutationRate);

            crossover.Cross(ref Population, maxGeneration);


            //TA ERRADO A SAIDA
            for (int i = 0; i < Population[0].GetChordProgression().Length; i++)
            {
                MessageBox.Show(Population[0].GetChordProgression()[i]);
            }
        }