Beispiel #1
0
 /// <summary>
 /// Создаем геномы и заполняем их генами
 /// </summary>
 private void CreateGenomes()
 {
     for (var i = 0; i < PopulationSize; i++)
     {
         Genome genome = new Genome();
         genome.CreateGenes();
         ThisGeneration.Add(genome);
     }
 }
Beispiel #2
0
        /// <summary>
        /// Создание следующего поколения
        /// </summary>
        private void CreateNextGeneration()
        {
            NextGeneration.Clear();
            Genome genome = null;

            if (Elitism)
            {
                genome = ThisGeneration[PopulationSize - 1];
            }

            for (var p = 0; p < PopulationSize; p += 2)
            {
                int    parentIndexFirst = RouletteSelection();
                int    parentIndexSecond = RouletteSelection();
                Genome parentFirst, parentSecond, childFirst, childSecond;

                parentFirst  = ThisGeneration[parentIndexFirst];
                parentSecond = ThisGeneration[parentIndexSecond];

                if (Rand.NextDouble() < CrossoverRate)
                {
                    parentFirst.Crossover(ref parentSecond, out childFirst, out childSecond);
                }
                else
                {
                    childFirst  = parentFirst;
                    childSecond = parentSecond;
                }
                childFirst.Mutate();
                childSecond.Mutate();

                NextGeneration.Add(childFirst);
                NextGeneration.Add(childSecond);
            }
            if (Elitism && genome != null)
            {
                NextGeneration[0] = genome;
            }

            ThisGeneration.Clear();
            for (var p = 0; p < PopulationSize; p++)
            {
                ThisGeneration.Add(NextGeneration[p]);
            }
        }