Exemple #1
0
        public GeneticAlgorithm(int selectedFitnessFunction, int populationSize, int iterationsNumber, int selectedMethod)
        {
            this.selectedMethod = selectedMethod;
            ISelectionMethod selectionMethot = GetSelectionMethod();

            DoubleArrayChromosome chromosome      = new DoubleArrayChromosome(new AForge.Math.Random.StandardGenerator(), new AForge.Math.Random.StandardGenerator(), new AForge.Math.Random.StandardGenerator(), 2);
            FitnessFunction       fitnessfunction = new FitnessFunction(selectedFitnessFunction);
            Population            population      = new Population(populationSize, chromosome, fitnessfunction, selectionMethot);

            double[,] tmp = new double[iterationsNumber, 2];
            this.result   = new double[iterationsNumber, 2];
            int i;

            for (i = 0; i < iterationsNumber; i++)
            {
                population.RunEpoch();
                DoubleArrayChromosome bestChromosome = (DoubleArrayChromosome)population.BestChromosome;
                double x = (double)bestChromosome.Value.GetValue(0);
                double y = (double)bestChromosome.Value.GetValue(1);
                tmp[i, 0] = x;
                tmp[i, 1] = y;
                if (fitnessfunction.Evaluate(bestChromosome) == 100)
                {
                    break;
                }
            }
            this.result = new double[i, 2];
            for (int j = 0; j < i; j++)
            {
                result[j, 0] = tmp[j, 0];
                result[j, 1] = tmp[j, 1];
            }
        }
Exemple #2
0
 public void Evaluate()
 {
     Parallel.ForEach(GenePool, genotype =>
     {
         genotype.Fitness = FitnessFunction.Evaluate(genotype);
     });
 }
Exemple #3
0
 private void UpdateFitnesses()
 {
     foreach (Program p in m_Progs)
     {
         if (p.FitnessIsDirty)
         {
             p.Fitness = FitnessFunction.Evaluate(p);
         }
     }
 }
Exemple #4
0
        /// <summary>
        ///  Evaluates the fitness of all individuals in the population
        /// </summary>
        /// <param name="pop">the population</param>
        /// <param name="count">propagating information where spanners are counted</param>
        public void Evaluate(Population pop, bool count)
        {
            double average = 0;

            pop.GetIndividuals().ForEach(x =>
            {
                double fit = fitness.Evaluate(x, count);
                x.SetFitnessValue(fit);
                average += fit;
            });
            // set average of population for console output
            pop.SetAverage(average / pop.GetPopulationSize());
        }