Esempio n. 1
0
        public GrammaticalEvolution GetClone()
        {
            GrammaticalEvolution ge = new GrammaticalEvolution();

            if (BluePrintGOs != null)
            {
                ge.BluePrintGOs = new List <GrammaticalObject>();
                foreach (GrammaticalObject go in BluePrintGOs)
                {
                    ge.BluePrintGOs.Add(go.GetClone());
                }
            }
            ge.codonPosition           = codonPosition;
            ge.StartingGO              = StartingGO == null ? null : StartingGO.GetClone();
            ge.StartingProductionRule  = StartingProductionRule == null ? null : new ProductionRule(StartingProductionRule.ToArray());
            ge._currentGenerationStats = _currentGenerationStats == null ? null : _currentGenerationStats.GetClone();
            ge._genome = _genome == null ? null : _genome.GetClone();
            return(ge);
        }
Esempio n. 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);
        }