Пример #1
0
        /// <summary>
        /// Crossover individuals
        /// </summary>
        /// <param name="indiv1">Parent individual 1</param>
        /// <param name="indiv2">Parent individual 2</param>
        /// <returns>Child individual</returns>
        private static Individual Crossover(Individual indiv1, Individual indiv2)
        {
            Individual newSol = new Individual();

            // Loop through genes
            for (int i = 0; i < indiv1.Size(); i++)
            {
                // Crossover
                if (rnd.NextDouble() <= uniformRate)
                {
                    newSol.SetGene(i, indiv1.GetGene(i));
                }
                else
                {
                    newSol.SetGene(i, indiv2.GetGene(i));
                }
            }
            return(newSol);
        }
Пример #2
0
 /// <summary>
 /// Mutate an individual
 /// </summary>
 /// <param name="indiv">Individual to mutate</param>
 private static void Mutate(Individual indiv)
 {
     // Loop through genes
     for (int i = 0; i < indiv.Size(); i++)
     {
         if (rnd.NextDouble() <= mutationRate)
         {
             // Create random gene
             int gene = (int)Math.Round((double)rnd.Next() % randomGeneRange);
             indiv.SetGene(i, gene);
         }
     }
 }