Example #1
0
        private GeneticAlgorithm CreateCFGGPSample()
        {
            GeneticAlgorithm ga = new GeneticAlgorithm();

            #region Problem Configuration
            CFGPythonProblem cfgPythonProblem = new CFGPythonProblem();
            cfgPythonProblem.Name        = "Smallest Problem";
            cfgPythonProblem.Description = "Smallest (described in: http://dl.acm.org/citation.cfm?id=2754769)";
            var     provider = new BenchmarkSuiteListInstanceProvider();
            var     instance = provider.GetDataDescriptors().Single(x => x.Name.Equals("Smallest"));
            CFGData data     = (CFGData)provider.LoadData(instance);
            data.Input  = File.ReadAllLines(@"Data\Smallest-Input.txt");
            data.Output = File.ReadAllLines(@"Data\Smallest-Output.txt");
            cfgPythonProblem.Load(data);

            // configure remaining problem parameters
            cfgPythonProblem.MaximumSymbolicExpressionTreeLengthParameter.Value.Value = 250;
            cfgPythonProblem.MaximumSymbolicExpressionTreeDepthParameter.Value.Value  = 100;
            #endregion
            #region Algorithm Configuration
            ga.Problem     = cfgPythonProblem;
            ga.Name        = "Genetic Programming - CFG Python";
            ga.Description = "A standard genetic programming algorithm to solve a cfg python problem";
            SamplesUtils.ConfigureGeneticAlgorithmParameters <LexicaseSelector, SubtreeCrossover, MultiSymbolicExpressionTreeManipulator>(
                ga, 100, 1, 10, 0.05, 5);
            var mutator = (MultiSymbolicExpressionTreeManipulator)ga.Mutator;
            mutator.Operators.SetItemCheckedState(mutator.Operators
                                                  .OfType <FullTreeShaker>()
                                                  .Single(), false);
            mutator.Operators.SetItemCheckedState(mutator.Operators
                                                  .OfType <OnePointShaker>()
                                                  .Single(), false);
            #endregion
            return(ga);
        }
Example #2
0
        public void SemanticsTest()
        {
            BenchmarkSuiteInstanceProvider prov = new BenchmarkSuiteListInstanceProvider();
            var prob = new CFGPythonProblem();

            prob.Load(prov.LoadData(prov.GetDataDescriptors().First(x => x.Name == "Smallest")));

            var grammar = prob.Grammar;

            var root = (SymbolicExpressionTreeTopLevelNode)grammar.ProgramRootSymbol.CreateTreeNode();

            root.SetGrammar(grammar.CreateExpressionTreeGrammar());
            //var root = new SymbolicExpressionTreeNode(new ProgramRootSymbol());
            var start = new SymbolicExpressionTreeNode(new StartSymbol());

            root.AddSubtree(start);

            var gstart = new SymbolicExpressionTreeNode(grammar.StartSymbol);

            start.AddSubtree(gstart);
            var predefined = new SymbolicExpressionTreeNode(GetSymbol(grammar, "'i0 = int(); i1 = int(); i2 = int()'\r\n'b0 = bool(); b1 = bool(); b2 = bool()'\r\n'res0 = int()'\r\n<code>"));

            gstart.AddSubtree(predefined);
            var codestatementSym = GetSymbol(grammar, "<code><statement>'\\n'");
            var code0            = new SymbolicExpressionTreeNode(codestatementSym);
            var code1            = new SymbolicExpressionTreeNode(codestatementSym);
            var code2            = new SymbolicExpressionTreeNode(GetSymbol(grammar, "<statement>'\\n'"));

            //predefined.AddSubtree(code1);
            predefined.AddSubtree(code0);
            code0.AddSubtree(code1);
            code1.AddSubtree(code2);

            var assign0 = new SymbolicExpressionTreeNode(GetSymbol(grammar, "<assign>"));
            var assign1 = new SymbolicExpressionTreeNode(GetSymbol(grammar, "<assign>"));

            code2.AddSubtree(assign0);
            //code0.AddSubtree(assign1);

            var if0 = new SymbolicExpressionTreeNode(GetSymbol(grammar, "<if>"));

            code1.AddSubtree(if0);

            var bool_assign0 = new SymbolicExpressionTreeNode(GetSymbol(grammar, "<bool_var>' = '<bool>"));
            var int_assign1  = new SymbolicExpressionTreeNode(GetSymbol(grammar, "<int_var>' = '<int>"));

            assign0.AddSubtree(bool_assign0);
            assign1.AddSubtree(int_assign1);

            var bool_var0 = new SymbolicExpressionTreeNode(GetSymbol(grammar, "<bool_var>"));
            var int_var1  = new SymbolicExpressionTreeNode(GetSymbol(grammar, "<int_var>"));
            var b0        = new SymbolicExpressionTreeNode(GetSymbol(grammar, "'b0'"));
            var i1        = new SymbolicExpressionTreeNode(GetSymbol(grammar, "'res0'"));

            bool_assign0.AddSubtree(bool_var0);
            int_assign1.AddSubtree(int_var1);
            bool_var0.AddSubtree(b0);
            int_var1.AddSubtree(i1);

            var bool_const0 = new SymbolicExpressionTreeNode(GetSymbol(grammar, "<bool_const>"));
            var int_const1  = new SymbolicExpressionTreeNode(GetSymbol(grammar, "'int('<number>'.0)'"));
            //var num0 = new SymbolicExpressionTreeNode(GetSymbol(grammar, "<num>"));
            var num1          = new SymbolicExpressionTreeNode(GetSymbol(grammar, "<num>"));
            var boolconstTrue = new SymbolicExpressionTreeNode(GetSymbol(grammar, "'True'"));
            var intconst2     = new SymbolicExpressionTreeNode(GetSymbol(grammar, "'2'"));

            bool_assign0.AddSubtree(bool_const0);
            int_assign1.AddSubtree(int_const1);
            bool_const0.AddSubtree(boolconstTrue);
            int_const1.AddSubtree(num1);
            num1.AddSubtree(intconst2);

            var ifthenelse0 = new SymbolicExpressionTreeNode(GetSymbol(grammar, "'if '<bool>':{:\\n'<code>':}else:{:\\n'<code>':}'"));
            var b0_2        = new SymbolicExpressionTreeNode(GetSymbol(grammar, "'b0'"));

            if0.AddSubtree(ifthenelse0);
            ifthenelse0.AddSubtree(b0_2);

            var statement1  = new SymbolicExpressionTreeNode(GetSymbol(grammar, "<statement>'\\n'"));
            var assign2     = new SymbolicExpressionTreeNode(GetSymbol(grammar, "<assign>"));
            var int_assign2 = new SymbolicExpressionTreeNode(GetSymbol(grammar, "<int_var>' = '<int>"));
            var i1_2        = new SymbolicExpressionTreeNode(GetSymbol(grammar, "'res0'"));
            var intconst7   = new SymbolicExpressionTreeNode(GetSymbol(grammar, "'7'"));

            ifthenelse0.AddSubtree(statement1);
            statement1.AddSubtree(assign2);
            assign2.AddSubtree(int_assign2);
            int_assign2.AddSubtree(i1_2);
            int_assign2.AddSubtree(intconst7);

            var statement2  = new SymbolicExpressionTreeNode(GetSymbol(grammar, "<statement>'\\n'"));
            var assign3     = new SymbolicExpressionTreeNode(GetSymbol(grammar, "<assign>"));
            var int_assign3 = new SymbolicExpressionTreeNode(GetSymbol(grammar, "<int_var>' = '<int>"));
            var i1_3        = new SymbolicExpressionTreeNode(GetSymbol(grammar, "'res0'"));
            var intconst8   = new SymbolicExpressionTreeNode(GetSymbol(grammar, "'9'"));

            ifthenelse0.AddSubtree(statement2);
            statement2.AddSubtree(assign3);
            assign3.AddSubtree(int_assign3);
            int_assign3.AddSubtree(i1_3);
            int_assign3.AddSubtree(intconst8);

            var while0         = new SymbolicExpressionTreeNode(GetSymbol(grammar, "'loopBreak% = 0\\nwhile '<bool>':{:\\n'<code>'\\nif loopBreak% > loopBreakConst or stop.value:{:\\nbreak\\n:}loopBreak% += 1\\n:}'"));
            var b0_3           = new SymbolicExpressionTreeNode(GetSymbol(grammar, "'b0'"));
            var code4          = new SymbolicExpressionTreeNode(codestatementSym);
            var statement3     = new SymbolicExpressionTreeNode(GetSymbol(grammar, "<statement>'\\n'"));
            var bool_assign1   = new SymbolicExpressionTreeNode(GetSymbol(grammar, "<bool_var>' = '<bool>"));
            var bool_var1      = new SymbolicExpressionTreeNode(GetSymbol(grammar, "<bool_var>"));
            var b0_4           = new SymbolicExpressionTreeNode(GetSymbol(grammar, "'b0'"));
            var bool_const1    = new SymbolicExpressionTreeNode(GetSymbol(grammar, "<bool_const>"));
            var boolconstFalse = new SymbolicExpressionTreeNode(GetSymbol(grammar, "'False'"));

            bool_assign1.AddSubtree(bool_var1);
            bool_var1.AddSubtree(b0_4);
            bool_assign1.AddSubtree(bool_const1);
            bool_const1.AddSubtree(boolconstFalse);
            statement3.AddSubtree(bool_assign1);

            //var statement4 = new SymbolicExpressionTreeNode(GetSymbol(grammar, "<statement>'\\n'"));
            var assign4     = new SymbolicExpressionTreeNode(GetSymbol(grammar, "<assign>"));
            var int_assign4 = new SymbolicExpressionTreeNode(GetSymbol(grammar, "<int_var>' = '<int>"));
            var i1_4        = new SymbolicExpressionTreeNode(GetSymbol(grammar, "'res0'"));
            var intconst9   = new SymbolicExpressionTreeNode(GetSymbol(grammar, "'1'"));

            //statement4.AddSubtree(assign4);
            assign4.AddSubtree(int_assign4);
            int_assign4.AddSubtree(i1_4);
            int_assign4.AddSubtree(intconst9);

            code4.AddSubtree(statement3);
            code4.AddSubtree(assign4);

            ((List <string>)((CFGProduction)while0.Symbol).parts)[0] = "x = 5\n" + ((List <string>)((CFGProduction)while0.Symbol).parts)[0];
            while0.AddSubtree(b0_3);
            while0.AddSubtree(code4);

            code0.AddSubtree(while0);


            var tree = new SymbolicExpressionTree(root);
            var code = CFGSymbolicExpressionTreeStringFormatter.StaticFormat(tree);

            System.Console.WriteLine(code);


            //PythonProcessSemanticHelper ppsh = new PythonProcessSemanticHelper(prob.ProblemData.Variables.GetVariableNames(), 10);
            traceCodeWithVariables = String.Format(traceCode, String.Join("', '", prob.ProblemData.Variables.GetVariableNames().Where(x => !String.IsNullOrWhiteSpace(x))), 10);

            var bla = PythonHelper.FormatToProgram(tree, prob.ProblemData.LoopBreakConst, prob.ProblemData.FullHeader, prob.ProblemData.FullFooter);
            //  var blub = ppsh.EvaluateAndTraceProgram(prob.PythonProcess,
            var blub = EvaluateAndTraceProgram(prob.PythonProcess,
                                               bla,
                                               PythonHelper.ConvertToPythonValues(prob.ProblemData.Input, prob.ProblemData.TrainingIndices),
                                               PythonHelper.ConvertToPythonValues(prob.ProblemData.Output, prob.ProblemData.TrainingIndices),
                                               prob.ProblemData.TrainingIndices, prob.ProblemData.FullHeader, prob.ProblemData.FullFooter, tree);

            Console.WriteLine(blub);
        }