Exemple #1
0
        /// <summary>
        /// Applies a global aggregator (typically sum, max or average) to child chromosomes fitnesses, given the individual fitness function
        /// </summary>
        /// <param name="childChromosomes">the child chromosomes to be aggregated</param>
        /// <param name="individualFitness">the individual fitness function to evaluate child chromosomes</param>
        /// <param name="aggregator">the global aggregator for the child fitnesses</param>
        /// <returns>an aggregated global fitness</returns>
        private static double ApplyAggregator(IEnumerable <IChromosome> childChromosomes, IFitness individualFitness, Func <IEnumerable <IChromosome>, Func <IChromosome, double>, double> aggregator)
        {
            var chromosomesEnumerated = childChromosomes as IList <IChromosome> ?? childChromosomes.ToList();

            foreach (var childChromosome in chromosomesEnumerated)
            {
                childChromosome.Fitness = individualFitness.Evaluate(childChromosome);
            }

            return(aggregator(chromosomesEnumerated, individualFitness.Evaluate));
        }
        /// <summary>
        /// Parallel fitness evaluation.
        /// </summary>
        /// <param name="fitness">Fitness.</param>
        /// <param name="population">Input Population</param>
        public override void EvaluateFitness(IFitness fitness, IList <IIndividual> population)
        {
            Parallel.ForEach(population, ind =>
            {
                if (!ind.Fitness.HasValue)
                {
                    ind.Fitness = fitness.Evaluate(ind);
                }
            });

            //  population.Individuals = population.Individuals.OrderByDescending(c => c.Fitness.Value).ToList();
        }
Exemple #3
0
 protected override void _evaluation()
 {
     Parallel.ForEach(_population, new ParallelOptions {
         MaxDegreeOfParallelism = 8
     }, (x) =>
     {
         if (x.Fitness == null)
         {
             x.Fitness = _selector.Evaluate(x)[0];
         }
     });
 }
        protected override IList <IChromosome> PerformSelectChromosomes(IPopulation population, IList <IChromosome> offspring, IList <IChromosome> parents)
        {
            var newPopulation = offspring.Concat(parents).ToList();

            foreach (var chromosome in newPopulation)
            {
                if (!chromosome.Fitness.HasValue)
                {
                    chromosome.Fitness = _fitness.Evaluate(chromosome);
                }
            }
            return(newPopulation.OrderByDescending(p => p.Fitness).Take(population.MaxSize).ToList());
        }
        /// <summary>
        /// Linear fitness evaluation.
        /// </summary>
        /// <param name="fitness">Fitness.</param>
        /// <param name="population">Input Population</param>
        public virtual void EvaluateFitness(IFitness fitness, IList <IIndividual> population)
        {
            foreach (var ind in population)
            {
                //non-fitness value
                if (!ind.Fitness.HasValue)
                {
                    // recalculation fitness for cur. individual
                    ind.Fitness = fitness.Evaluate(ind);
                }
            }

            // sorts population by fitness
            // population.Individuals = population.Individuals.OrderByDescending(c => c.Fitness.Value).ToList();
        }
Exemple #6
0
 private void ShowResult(IChromosome chromosome, IFitness fitness)
 {
     for (var i = 0; i < 30; i++)
     {
         Console.Write(fitness.Evaluate(chromosome) + " ");
         foreach (var gene in chromosome.GetGenes())
         {
             var server       = (Server)gene.Value;
             var serviceNames = server.Services.Select(x => x.Name.ToString()).ToList();
             Console.Write("Сервер - " + server.Name + ":{ ");
             serviceNames.ForEach(x => Console.Write(x + ";"));
             Console.Write("} ");
         }
         Console.WriteLine();
         Console.WriteLine("Next");
     }
 }
 protected override void _evaluation()
 {
     Parallel.ForEach(_population, new ParallelOptions {
         MaxDegreeOfParallelism = 8
     }, (x) =>
     {
         if (x.Fitness == null)
         {
             //#TODO Add object with Coverage and Fitness
             x.Fitness = _selector.Evaluate(x)[0];
             Console.WriteLine("Processing {0} on thread {1}", x.Fitness.Value, Thread.CurrentThread.ManagedThreadId);
         }
         else
         {
             Console.WriteLine("Already Set!");
         }
     });
 }
 protected override void _evaluation()
 {
     Parallel.ForEach(_population, new ParallelOptions {
         MaxDegreeOfParallelism = 8
     }, (x) =>
     {
         if (x.Fitness == null)
         {
             //This is a good example of using OO to prevent things from happening by using objects and our custom type
             //Example of encapsulation - information hiding
             //TODO fix reflection in high performance code
             x.Fitness = _selector.Evaluate(x)[0];
             Console.WriteLine("Returned {0} on thread {1}", x.Fitness, Thread.CurrentThread.ManagedThreadId);
         }
         else
         {
             Console.WriteLine("Already Set!");
         }
     });
 }
        protected override void _evaluation()
        {
            Parallel.ForEach(_population, new ParallelOptions {
                MaxDegreeOfParallelism = 8
            }, (x) =>
            {
                if (x.Fitness == null)
                {
                    x.Fitness = _selector.Evaluate(x)[0];
#if DEBUG
                    Console.WriteLine("Processing {0} on thread {1}", x.Fitness.Value, Thread.CurrentThread.ManagedThreadId);
#endif
                }
                else
                {
#if DEBUG
                    Console.WriteLine("Already Set!");
#endif
                }
            });
        }
        protected override IList <IChromosome> PerformSelectChromosomes(IPopulation population,
                                                                        IList <IChromosome> offspring, IList <IChromosome> parents)
        {
            // evaluate fitness of all
            Parallel.ForEach(offspring.Concat(parents),
                             new ParallelOptions()
            {
                MaxDegreeOfParallelism = Global.Config.ThreadsCount
            },
                             x => fitness.Evaluate(x));

            IList <IChromosome> resultOffspring;

            // if not enough offsprings => take from parents
            if (offspring.Count < population.MinSize)
            {
                var parentsToAdd = selection.SelectChromosomes(population.MinSize - offspring.Count,
                                                               new Generation(parents.Count, parents));
                resultOffspring = offspring;
                foreach (var chromosome in parentsToAdd)
                {
                    resultOffspring.Add(chromosome);
                }
            }
            // if too many offsprings => take right number from them
            else if (offspring.Count > population.MinSize)
            {
                resultOffspring = selection.SelectChromosomes(population.MinSize,
                                                              new Generation(offspring.Count, offspring));
            }
            else
            {
                resultOffspring = offspring;
            }
            // perform elitism (take n best samples from parents and add it to offsprings)
            resultOffspring = elitism.SelectChromosomes(population, resultOffspring, parents);

            return(resultOffspring);
        }
Exemple #11
0
 public void EvaluateFitness(IFitness <T> function)
 {
     m_Fitness = function.Evaluate(this);
 }
Exemple #12
0
 public double[] Evaluate(StubIndividual item)
 {
     Console.WriteLine(item.ToString());
     return(_fitness.Evaluate(item));
 }
Exemple #13
0
 public void EvaluateFitness(IFitness function)
 {
     Fitness = function.Evaluate(this);
 }