Пример #1
0
        /// <summary>
        ///	Takes 2 parent gene vectors, selects a midpoint and then swaps the ends
        ///	of each genome creating 2 new genomes which are stored in baby1 and
        ///	baby2.
        /// </summary>
        /// <param name="mum"></param>
        /// <param name="dad"></param>
        /// <param name="baby1"></param>
        /// <param name="baby2"></param>
        private void Crossover(List <Gene> mum, List <Gene> dad, ref List <Gene> baby1, ref List <Gene> baby2)
        {
            //just return parents as offspring dependent on the rate
            //or if parents are the same
            if ((GAUtils.RandDouble() > CrossoverRate) || (mum == dad))
            {
                baby1 = mum;
                baby2 = dad;
                return;
            }

            //determine a crossover point
            int crossOverPoint = GAUtils.RandInt(0, mum.Count - 1); // = 29

            for (int i = 0; i < mum.Count; i++)
            {
                var crossed = Gene.Cross(mum[i], dad[i], i < crossOverPoint);
                baby1.Add(crossed[0]);
                baby2.Add(crossed[1]);
            }

            // swap the bits
            // for (int i = 0; i < crossOverPoint; ++i)
            // {
            //     baby1.Add(mum[i]);
            //     baby2.Add(dad[i]);
            // }

            // for (int i = crossOverPoint; i < mum.Count; ++i)
            // {
            //     baby1.Add(dad[i]);
            //     baby2.Add(mum[i]);
            // }
        }
Пример #2
0
 public Gene(int numberOfBits)
 {
     Bits = new List <int>();
     // create a random bit string (random genes)
     for (int i = 0; i < numberOfBits; ++i)
     {
         Bits.Add(GAUtils.RandInt(0, 1));
     }
 }
Пример #3
0
 /// <summary>
 ///	iterates through each genome flipping the bits acording to the
 ///	mutation rate
 /// </summary>
 /// <param name="genes"></param>
 private void Mutate(List <Gene> genes)
 {
     // Go through each gene
     for (int i = 0; i < genes.Count; i++)
     {
         Gene gene = genes[i];
         for (int j = 0; j < gene.Bits.Count; j++)
         {
             //do we flip this bit?
             if (GAUtils.RandDouble() < MutationRate)
             {
                 //flip the bit
                 gene.Bits[j] = gene.Bits[j] == 0 ? 1 : 0;
             }
         }
     }
 }
Пример #4
0
        /// <summary>
        ///	selects a member of the population by using roulette wheel
        ///	selection as described in the text.
        /// </summary>
        /// <returns></returns>
        private TGenome RouletteWheelSelection()
        {
            double fSlice = GAUtils.RandDouble() * TotalFitnessScore;

            double cfTotal = 0.0;

            int selectedGenome = 0;

            for (int i = 0; i < Genomes.Count; ++i)
            {
                cfTotal += Genomes[i].Fitness;

                if (cfTotal > fSlice)
                {
                    selectedGenome = i;
                    break;
                }
            }

            return(Genomes[selectedGenome]);
        }