コード例 #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);
        }
コード例 #2
0
        internal string Generate(Chromosome genome, GenerationStats currentGenerationStats)
        {
            _genome = genome;
            _currentGenerationStats = currentGenerationStats;

            string code = "";

            bool compileAgain      = true;
            bool correctionApplied = false;

            while (compileAgain)
            {
                try
                {
                    codonPosition = -1;
                    GrammaticalObject go = (GrammaticalObject)Activator.CreateInstance(StartingGO.GetType());
                    go.instance = this;
                    go.Initialize(go.GetInitialProductionRules());
                    code = ConvertToCode(StartingProductionRule, go);
                    break;
                }
                catch (ErrorCheck.ErrorCheckFailException e)
                {
                    compileAgain = false;
                    List <GrammaticalObject> stackTrace = e.exceptionGO.GetStackTrace();

                    _currentGenerationStats.failedErrorChecks++;

                    for (int stackTracePosition = 0; stackTracePosition < stackTrace.Count && !compileAgain; stackTracePosition++)
                    {
                        int initialPosition = stackTrace[stackTracePosition].FirstUsedIndex;
                        int finalPosition   = stackTrace[stackTracePosition].LastUsedIndex;

                        if (CanCorrectChromosome(_genome, initialPosition, finalPosition))
                        {
                            compileAgain      = true;
                            correctionApplied = true;
                            CorrectChromosome(_genome, initialPosition, finalPosition);
                        }
                    }
                    if (!compileAgain)
                    {
                        _currentGenerationStats.failedErrorCorrections++;
                        throw new ErrorCorrectionFailedException();
                    }
                }
            }

            if (correctionApplied)
            {
                _currentGenerationStats.successfulErrorCorrections++;
            }
            return(code);
        }
コード例 #3
0
        public GenerationStats GetClone()
        {
            GenerationStats stats = new GenerationStats();

            stats.bestChromosome             = bestChromosome.GetClone();
            stats.compilationErrors          = compilationErrors;
            stats.executedEvaluations        = executedEvaluations;
            stats.executionExceptions        = executionExceptions;
            stats.failedErrorChecks          = failedErrorChecks;
            stats.failedErrorCorrections     = failedErrorCorrections;
            stats.fitnessValues              = fitnessValues.ToList();
            stats.generationErrors           = generationErrors;
            stats.passedErrorChecks          = passedErrorChecks;
            stats.successfulErrorCorrections = successfulErrorCorrections;

            return(stats);
        }
コード例 #4
0
        private Tuple <double, bool> Evaluate(Chromosome chromosome, bool saveBackupCodons, GrammaticalEvolution ge)
        {
            GrammaticalEvolution newGE                = ge.GetClone();
            string    code                            = null;
            object    result                          = null;
            Exception generationException             = null;
            CompilerErrorCollection compilationErrors = null;
            Exception       executionException        = null;
            GenerationStats s                         = new GenerationStats();

            try
            {
                code = newGE.Generate(chromosome, s);
            }
            catch (GrammaticalEvolution.ErrorCorrectionFailedException e)
            {
                generationException = e;
            }
            catch (GrammaticalEvolution.EndOfCodonQueueException e)
            {
                generationException = e;
            }
            if (generationException == null)
            {
                try
                {
                    result = Executor.Execute(Parameters, code);
                }
                catch (Executor.CompilationErrorException e)
                {
                    compilationErrors = e.Errors;
                }
                catch (Executor.ExecutionExceptionException e)
                {
                    executionException = e.InnerException;
                }
            }

            if (generationException == null && compilationErrors == null && executionException == null && saveBackupCodons)
            {
                chromosome.BackupCodons = chromosome.ToList();
            }

            return(new Tuple <double, bool>(Calculator(result, generationException, compilationErrors, executionException), s.failedErrorCorrections > 0));
        }
コード例 #5
0
 public Generation() : base()
 {
     Stats = new GenerationStats();
 }