예제 #1
0
 protected virtual IEnumerable <IDeploymentChromosome> Mutate(IEnumerable <IDeploymentChromosome> offspring)
 {
     foreach (var child in offspring)
     {
         if (RandomProvider.GetRandom() <= MutationProbability)
         {
             yield return(MutationOperator.Mutate(child));
         }
         else
         {
             yield return(child);
         }
     }
 }
        private void CreateNextGeneration()
        {
            var startingIndex = UsingElitism ? NumberOfElite : 0;

            if (UsingElitism)
            {
                Population.SortByFitness();
                for (int i = 0; i < NumberOfElite; i++)
                {
                    Population.AddChromosome(i, Population.Chromosomes[i]);
                }

                startingIndex = NumberOfElite;
            }

            var populationSize = Population.PopulationSize;

            for (int i = startingIndex; i < populationSize; i++)
            {
                // TODO -> Should I make sure that both parents are different ??

                // selection operation
                var fatherChromosome = SelectionOperator.PopulationSelection(Population);
                var motherChromosome = SelectionOperator.PopulationSelection(Population);

                // TODO -> return more than one child always!!!
                // crossover operation
                var childChromosome = CrossoverOperator.Crossover(fatherChromosome, motherChromosome); // TODO -> some crossovers can return more than one child

                // mutate operation
                MutationOperator.Mutate(childChromosome);

                // add child chromsome to the next generation population
                CalculateFitness(childChromosome);
                Population.AddChromosome(i, childChromosome);
            }

            // prepare for next evolution
            Population.SetNextGeneration();
            SelectionOperator.SetNextGeneration();

            Generation++;
        }