Exemplo n.º 1
0
 /// <summary>
 /// Создаем геномы и заполняем их генами
 /// </summary>
 private void CreateGenomes()
 {
     for (var i = 0; i < PopulationSize; i++)
     {
         Genome genome = new Genome();
         genome.CreateGenes();
         ThisGeneration.Add(genome);
     }
 }
Exemplo n.º 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]);
            }
        }
Exemplo n.º 3
0
        /// <summary>
        /// Сортируем популяции и сортируем в порядке пригодности
        /// </summary>
        private void RankPopulation()
        {
            TotalFitness = 0;
            for (var p = 0; p < PopulationSize; p++)
            {
                Genome genome = ThisGeneration[p];
                genome.Fitness = FitnessFunction(genome.Genes);
                TotalFitness  += genome.Fitness;
            }
            ThisGeneration.Sort(new GenomeComparison());

            double fitness = 0.0;

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