Exemplo n.º 1
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);
        }
Exemplo n.º 2
0
        public T CreateChild <T>() where T : GrammaticalObject
        {
            GrammaticalObject blueprintGO = instance.BluePrintGOs.Find(x => x.GetType() == typeof(T));

            if (blueprintGO == null)
            {
                throw new BluePrintGONotFound(typeof(T));
            }

            GrammaticalObject go = blueprintGO.GetClone();

            go.instance = instance;
            go.Initialize(this);//Creates a copy that has the same production rules as the parent

            go.Parent = this;

            return((T)go);
        }