Example #1
0
        private static GeneticAlgorithmResult AverageResults(List <GeneticAlgorithmResult> results)
        {
            GeneticAlgorithmResult averageResult = new GeneticAlgorithmResult();

            averageResult.stats = new List <GenerationStats>();

            List <List <GenerationStats> > unitedStats = results.Select(x => x.stats).ToList();

            for (int i = 0; i < results[0].stats.Count; i++)
            {
                GenerationStats stat = new GenerationStats();

                stat.compilationErrors          = unitedStats.Select(x => x[i].compilationErrors).Average();
                stat.executedEvaluations        = unitedStats.Select(x => x[i].executedEvaluations).Average();
                stat.executionExceptions        = unitedStats.Select(x => x[i].executionExceptions).Average();
                stat.failedErrorChecks          = unitedStats.Select(x => x[i].failedErrorChecks).Average();
                stat.failedErrorCorrections     = unitedStats.Select(x => x[i].failedErrorCorrections).Average();
                stat.generationErrors           = unitedStats.Select(x => x[i].generationErrors).Average();
                stat.passedErrorChecks          = unitedStats.Select(x => x[i].passedErrorChecks).Average();
                stat.successfulErrorCorrections = unitedStats.Select(x => x[i].successfulErrorCorrections).Average();

                stat.fitnessValues = new List <double>();
                for (int j = 0; j < unitedStats[0][0].fitnessValues.Count; j++)
                {
                    stat.fitnessValues.Add(unitedStats.Select(x => x[i].fitnessValues[j]).Average());
                }

                averageResult.stats.Add(stat);
            }

            return(averageResult);
        }
Example #2
0
        private static Tuple <GeneticAlgorithmResult, GeneticAlgorithmResult> ExtractData(int populationSize, int generationNumber, int maximumChromosomeSize)
        {
            int    elitismSize            = GrammaticalEvolution.random.Next(1, 5);
            int    tournamentSize         = GrammaticalEvolution.random.Next(2, 6);
            double crossoverProbability   = GrammaticalEvolution.random.NextDouble();
            double mutationProbability    = GrammaticalEvolution.random.NextDouble();
            double duplicationProbability = GrammaticalEvolution.random.NextDouble();
            double pruningProbability     = GrammaticalEvolution.random.NextDouble();
            int    initialChromosomeSize  = GrammaticalEvolution.random.Next(64, maximumChromosomeSize);

            Console.WriteLine("Inizio test di PECLE...");
            GeneticAlgorithmResult pecleResult = PECLETest(populationSize, generationNumber, initialChromosomeSize, maximumChromosomeSize, elitismSize, tournamentSize, crossoverProbability, mutationProbability, duplicationProbability, pruningProbability);

            Console.WriteLine("Inizio test di Default...");
            GeneticAlgorithmResult noImprovementsResult = PECLETest_NoImprovements(populationSize, generationNumber, initialChromosomeSize, maximumChromosomeSize, elitismSize, tournamentSize, crossoverProbability, mutationProbability, duplicationProbability, pruningProbability);

            return(new Tuple <GeneticAlgorithmResult, GeneticAlgorithmResult>(pecleResult, noImprovementsResult));
        }
Example #3
0
        public GeneticAlgorithmResult Evolve(int generations, double crossoverProbability, double mutationProbability, double duplicationProbability, double pruningProbability)
        {
            GeneticAlgorithmResult result = new GeneticAlgorithmResult();

            result.stats = new List <GenerationStats>();

            //Evaluate and save the results of the first generation

            Console.WriteLine("Eseguendo generazione iniziale");

            Evaluate(Population.CurrentGeneration);

            Population.CurrentGeneration.Stats.fitnessValues  = Population.CurrentGeneration.Select(x => x.Fitness.Value).ToList();
            Population.CurrentGeneration.Stats.bestChromosome = Population.CurrentGeneration.OrderByDescending(x => x.Fitness).First();

            result.stats.Add(Population.CurrentGeneration.Stats);
            for (int i = 1; i < generations; i++)
            {
                Console.WriteLine("Eseguendo generazione " + (i + 1) + " di " + generations);
                Generation newGeneration = new Generation();

                //Finds all the individuals with a valid fitness and takes the best ones
                int elitesNumber = Math.Min(Population.CurrentGeneration.FindAll(x => x.Fitness.HasValue).Count, ElitismSize);

                List <Chromosome> elites = Population.CurrentGeneration.OrderByDescending(x => x.Fitness.Value).Take(elitesNumber).Select(x => x.GetClone()).ToList();

                int newGenerationSize = Population.CurrentGeneration.Count - elitesNumber;

                while (newGeneration.Count < newGenerationSize)
                {
                    Tuple <Chromosome, Chromosome> parents = Selection.Select(Population.CurrentGeneration);

                    Tuple <Chromosome, Chromosome> children = Crossover.Cross(parents.Item1, parents.Item2, crossoverProbability);

                    //If there is room for only one individual, choose randomly
                    if (newGeneration.Count == newGenerationSize - 1)
                    {
                        if (GrammaticalEvolution.random.NextDouble() > 0.5)
                        {
                            newGeneration.Add(children.Item1);
                        }
                        else
                        {
                            newGeneration.Add(children.Item2);
                        }
                    }
                    else
                    {
                        newGeneration.Add(children.Item1);
                        newGeneration.Add(children.Item2);
                    }
                }

                foreach (Chromosome c in newGeneration)
                {
                    Mutation.Mutate(c, mutationProbability);
                }

                Evaluate(newGeneration);

                Prune(newGeneration, pruningProbability);
                Duplicate(newGeneration, duplicationProbability);

                newGeneration.AddRange(elites);

                newGeneration.Stats.fitnessValues  = newGeneration.Select(x => x.Fitness.Value).ToList();
                newGeneration.Stats.bestChromosome = newGeneration.OrderByDescending(x => x.Fitness).First().GetClone();

                Population.Add(newGeneration);

                //We remove the chromosomes of older generations (but store their fitness)
                if (!SaveChromosomes && Population.Count > 1)
                {
                    Population[Population.Count - 2].Clear();
                }

                result.stats.Add(newGeneration.Stats);
            }
            return(result);
        }