Пример #1
0
        public Program Simplify(FitnessEvaluation fitnessEvaluation, StringBuilder stringBuilder)
        {
            int      pc           = 0;
            var      callTree     = new CallTree(_varnumber, _code, ref pc);
            var      constantsSet = new ConstantsSet(_constantsSet);
            CallTree simplifiedCallTree;

            if (fitnessEvaluation != null)
            {
                Print(stringBuilder);
            }
            do
            {
                callTree.Reset();
                simplifiedCallTree = callTree.Simplify(constantsSet, fitnessEvaluation != null ? stringBuilder : null);
                if (fitnessEvaluation != null && simplifiedCallTree != null)
                {
                    stringBuilder.AppendLine("After removing redundant code:");

                    var simplifiedprogram = new Program(_varnumber, new List <int>(simplifiedCallTree.Encode()),
                                                        constantsSet,
                                                        WorkingVariablesCount);
                    simplifiedprogram.Print(stringBuilder);
                    if (AnalyzeTestCasedifferences(fitnessEvaluation, simplifiedprogram, stringBuilder))
                    {
                        break;
                    }
                }
                CallTree tmpCallTree =
                    (simplifiedCallTree ?? callTree).PurgeUnassignedVariables(constantsSet,
                                                                              new List <int>(), stringBuilder);

                if (tmpCallTree != null)
                {
                    simplifiedCallTree = tmpCallTree;
                    if (fitnessEvaluation != null)
                    {
                        stringBuilder.AppendLine("After removing unassigned variables:");
                        var simplifiedprogram = new Program(_varnumber, new List <int>(simplifiedCallTree.Encode()),
                                                            constantsSet,
                                                            WorkingVariablesCount);
                        simplifiedprogram.Print(stringBuilder);
                        if (AnalyzeTestCasedifferences(fitnessEvaluation, simplifiedprogram, stringBuilder))
                        {
                            break;
                        }
                    }
                }

                tmpCallTree =
                    (simplifiedCallTree ?? callTree).PurgeUnusedAssignmentsToVariables(constantsSet, new List <int>(), stringBuilder);

                if (tmpCallTree != null)
                {
                    simplifiedCallTree = tmpCallTree;
                    if (fitnessEvaluation != null)
                    {
                        stringBuilder.AppendLine("After removing assignments to variables not used again:");
                        var simplifiedprogram = new Program(_varnumber, new List <int>(simplifiedCallTree.Encode()),
                                                            constantsSet,
                                                            WorkingVariablesCount);
                        simplifiedprogram.Print(stringBuilder);
                        if (AnalyzeTestCasedifferences(fitnessEvaluation, simplifiedprogram, stringBuilder))
                        {
                            break;
                        }
                    }
                }

                tmpCallTree = (simplifiedCallTree ?? callTree).PurgeRedundantUsagesOfAssignedVariables(constantsSet, new Dictionary <int, CallTree>());

                if (tmpCallTree != null)
                {
                    simplifiedCallTree = tmpCallTree;
                    if (fitnessEvaluation != null)
                    {
                        stringBuilder.AppendLine("After removing redundant usages of assigned variables:");
                        var simplifiedprogram = new Program(_varnumber, new List <int>(simplifiedCallTree.Encode()),
                                                            constantsSet,
                                                            WorkingVariablesCount);
                        simplifiedprogram.Print(stringBuilder);
                        if (AnalyzeTestCasedifferences(fitnessEvaluation, simplifiedprogram, stringBuilder))
                        {
                            break;
                        }
                    }
                }

                if (simplifiedCallTree != null && simplifiedCallTree.Equals(callTree))
                {
                    stringBuilder.AppendLine("Simplified call tree is equal to original call tree, giving up");
                    new Program(_varnumber, new List <int>(callTree.Encode()), constantsSet, WorkingVariablesCount).Print(stringBuilder);
                    //break;
                }
                callTree = simplifiedCallTree ?? callTree;
            } while (simplifiedCallTree != null);
            return(new Program(_varnumber, new List <int>(callTree.Encode()), constantsSet, WorkingVariablesCount));
        }