コード例 #1
0
ファイル: SubstringTest.cs プロジェクト: dolphingarlic/prose
        public void TestLearnSubstringTwoExamples()
        {
            Result <Grammar> grammar = CompileGrammar();
            SynthesisEngine  prose   = ConfigureSynthesis(grammar.Value);

            State firstInput  = State.CreateForExecution(grammar.Value.InputSymbol, "Toby Miller");
            State secondInput = State.CreateForExecution(grammar.Value.InputSymbol, "Courtney Lynch");
            var   examples    = new Dictionary <State, object> {
                { firstInput, "Miller" }, { secondInput, "Lynch" }
            };
            var spec = new ExampleSpec(examples);

            ProgramSet learnedSet = prose.LearnGrammar(spec);
            IEnumerable <ProgramNode> programs = learnedSet.RealizedPrograms;
            var output = programs.First().Invoke(firstInput) as string;

            Assert.AreEqual("Miller", output);
            var output2 = programs.First().Invoke(secondInput) as string;

            Assert.AreEqual("Lynch", output2);
            State differentInput = State.CreateForExecution(grammar.Value.InputSymbol, "Yun Jasinska");

            output = programs.First().Invoke(differentInput) as string;
            Assert.AreEqual("Jasinska", output);
        }
コード例 #2
0
ファイル: Helper.cs プロジェクト: MichaBe/PROSEHelloWorld
        public static ProgramSet Learn(Grammar g, Spec s, string logXmlFilename)
        {
            var engine = new SynthesisEngine(
                grammar: g,
                config: new SynthesisEngine.Config
            {
                Strategies = new ISynthesisStrategy[]
                {
                    new EnumerativeSynthesis(
                        new EnumerativeSynthesis.Config
                    {
                        MaximalSize = 101 * 101 * 11 * 2
                    }
                        ),
                    new ComponentBasedSynthesis(null, null, null)
                },
                UseThreads  = true,
                LogListener = new LogListener()
            });

            var consistentPrograms = engine.LearnGrammar(s);

            engine.Configuration.LogListener.SaveLogToXML(logXmlFilename);

            return(consistentPrograms);
        }
コード例 #3
0
ファイル: Utils.cs プロジェクト: xornand/prose
        public static ProgramNode Learn(Grammar grammar, Spec spec)
        {
            var engine = new SynthesisEngine(grammar, new SynthesisEngine.Config
            {
                UseThreads  = false,
                LogListener = new LogListener(),
            });
            ProgramSet consistentPrograms = engine.LearnGrammar(spec);

            engine.Configuration.LogListener.SaveLogToXML("learning.log.xml");

            //foreach (ProgramNode p in consistentPrograms.RealizedPrograms) {
            //    Console.WriteLine(p);
            //}

            ProgramNode bestProgram = consistentPrograms.TopK("Score").FirstOrDefault();

            if (bestProgram == null)
            {
                WriteColored(ConsoleColor.Red, "No program :(");
                return(null);
            }
            var score = bestProgram["Score"];

            WriteColored(ConsoleColor.Cyan, $"[score = {score:F3}] {bestProgram}");
            return(bestProgram);
        }
コード例 #4
0
ファイル: SubstringTest.cs プロジェクト: dolphingarlic/prose
        public void TestLearnSubstringPositiveAbsPosSecOcurrence()
        {
            Result <Grammar> grammar = CompileGrammar();
            SynthesisEngine  prose   = ConfigureSynthesis(grammar.Value);

            State firstInput  = State.CreateForExecution(grammar.Value.InputSymbol, "16-Feb-2016");
            State secondInput = State.CreateForExecution(grammar.Value.InputSymbol, "14-Jan-2012");
            var   examples    = new Dictionary <State, object> {
                { firstInput, "16" }, { secondInput, "12" }
            };
            var spec = new ExampleSpec(examples);

            ProgramSet learnedSet = prose.LearnGrammar(spec);

            IEnumerable <ProgramNode> programs = learnedSet.RealizedPrograms;
            ProgramNode firstProgram           = programs.First();
            var         output = firstProgram.Invoke(firstInput) as string;

            Assert.AreEqual("16", output);
            output = firstProgram.Invoke(secondInput) as string;
            Assert.AreEqual("12", output);
            State differentInput = State.CreateForExecution(grammar.Value.InputSymbol, "15-Apr-1500");

            output = programs.First().Invoke(differentInput) as string;
            Assert.AreEqual("00", output);
        }
コード例 #5
0
ファイル: SubstringTest.cs プロジェクト: dolphingarlic/prose
        public void TestLearnSubstringPositiveAbsPos()
        {
            //parse grammar file
            Result <Grammar> grammar = CompileGrammar();
            //configure the prose engine
            SynthesisEngine prose = ConfigureSynthesis(grammar.Value);

            //create the example
            State input    = State.CreateForExecution(grammar.Value.InputSymbol, "19-Feb-1960");
            var   examples = new Dictionary <State, object> {
                { input, "Feb" }
            };
            var spec = new ExampleSpec(examples);

            //learn the set of programs that satisfy the spec
            ProgramSet learnedSet = prose.LearnGrammar(spec);

            //run the first synthesized program in the same input and check if
            //the output is correct
            IEnumerable <ProgramNode> programs = learnedSet.RealizedPrograms;
            var output = programs.First().Invoke(input) as string;

            Assert.AreEqual("Feb", output);

            State differentInput = State.CreateForExecution(grammar.Value.InputSymbol, "15-Jan-2000");

            output = programs.First().Invoke(differentInput) as string;
            Assert.AreEqual("Jan", output);
        }
コード例 #6
0
        public void TestLearnAdd()
        {
            var grammar = DSLCompiler.LoadGrammarFromFile("../../../ProseDSLModels.grammar");

            printGrammar(grammar);
            SynthesisEngine prose = new SynthesisEngine(grammar.Value);

            double[] inp1 = new double[4] {
                10.9, 15.0, -14.5, 12.3
            };
            double[] out1  = add(inp1);
            var      input = State.Create(grammar.Value.InputSymbol, inp1);

            var examples = new Dictionary <State, object> {
                { input, out1 }
            };
            var spec       = new ExampleSpec(examples);
            var learnedSet = prose.LearnGrammar(spec);
            var output     = (double[])learnedSet.RealizedPrograms.First().Invoke(input);

            stdoutprogram(learnedSet.RealizedPrograms.First().ToString(), "Add");
            Assert.AreEqual(23.7, output[0], 0.001d);
            TestContext.WriteLine("Running random exmaples for LearnAdd");
            //test 1000 random examples
            for (int i = 0; i < 1000; ++i)
            {
                var iTmp   = genRnd();
                var inpTmp = State.Create(grammar.Value.InputSymbol, iTmp);
                var oTmp   = add(iTmp);
                var outTmp = (double[])learnedSet.RealizedPrograms.First().Invoke(inpTmp);
                TestContext.WriteLine("Excpt [{0}]", string.Join(", ", oTmp));
                TestContext.WriteLine("Actul [{0}]", string.Join(", ", outTmp));
                Assert.AreEqual(oTmp[0], outTmp[0], 0.001d);
            }
        }
コード例 #7
0
        public void TestLearnSub()
        {
            var grammar = DSLCompiler.LoadGrammarFromFile("../../../ProseDSLModels.grammar");

            printGrammar(grammar);
            SynthesisEngine prose = new SynthesisEngine(grammar.Value);

            double[] inp1 = new double[4] {
                10.9, 15.0, -14.5, 12.3
            };
            double[] out1 = new double[4] {
                0.0, 4.1, -25.4, 1.4
            };
            var input = State.Create(grammar.Value.InputSymbol, inp1);

            var examples = new Dictionary <State, object> {
                { input, out1 }
            };
            var spec       = new ExampleSpec(examples);
            var learnedSet = prose.LearnGrammar(spec);
            var output     = (double[])learnedSet.RealizedPrograms.First().Invoke(input);

            Assert.AreEqual(0.0, output[0], 0.001d);
            Assert.AreEqual(4.1, output[1], 0.001d);
            Assert.AreEqual(-25.4, output[2], 0.001d);
            Assert.AreEqual(1.4, output[3], 0.001d);
        }
コード例 #8
0
        public static ProgramNode Learn(Grammar grammar, Spec spec,
                                        Feature <double> scorer, DomainLearningLogic witnessFunctions)
        {
            var engine = new SynthesisEngine(grammar, new SynthesisEngine.Config
            {
                Strategies = new ISynthesisStrategy[]
                {
                    new EnumerativeSynthesis(),
                    new DeductiveSynthesis(witnessFunctions)
                },
                UseThreads  = false,
                LogListener = new LogListener(),
            });
            ProgramSet consistentPrograms = engine.LearnGrammar(spec);

            engine.Configuration.LogListener.SaveLogToXML("learning.log.xml");

            //foreach (ProgramNode p in consistentPrograms.RealizedPrograms) {
            //    Console.WriteLine(p);
            //}

            ProgramNode bestProgram = consistentPrograms.TopK(scorer).FirstOrDefault();

            if (bestProgram == null)
            {
                WriteColored(ConsoleColor.Red, "No program :(");
                return(null);
            }
            var score = bestProgram.GetFeatureValue(scorer);

            WriteColored(ConsoleColor.Cyan, $"[score = {score:F3}] {bestProgram}");
            return(bestProgram);
        }
コード例 #9
0
ファイル: SExpProgram.cs プロジェクト: namin/SExp
        static ProgramNode learnFromExamples(Grammar grammar, SynthesisEngine engine, RankingScore rankingScores, Dictionary <string, string> exs)
        {
            var spec = toSpec(grammar, exs);
            var ps   = engine.LearnGrammar(spec);

            Console.WriteLine("all programs: " + ps);
            var best = ps.TopK(rankingScores).First();

            Console.WriteLine("best program: " + best);
            foreach (var ex in spec.Examples)
            {
                System.Diagnostics.Debug.Assert(best.Invoke(ex.Key).Equals(ex.Value));
            }
            return(best);
        }
コード例 #10
0
ファイル: Learner.cs プロジェクト: jhanavi/SayNoToSQL
        private ProgramSet LearnProgramSet(Spec spec, DomainLearningLogic witnessFunctions)
        {
            var engine = new SynthesisEngine(Grammar, new SynthesisEngine.Config
            {
                Strategies = new ISynthesisStrategy[] {
                    new EnumerativeSynthesis(),
                    new DeductiveSynthesis(witnessFunctions)
                },
                UseThreads  = false,
                LogListener = new LogListener(),
            });
            ProgramSet consistentPrograms = engine.LearnGrammar(spec);

            engine.Configuration.LogListener.SaveLogToXML("learning.log.xml");
            return(consistentPrograms);
        }
コード例 #11
0
ファイル: Helper.cs プロジェクト: MichaBe/PROSEHelloWorld
        public static ProgramSet LearnDeductively(Grammar g, Spec s, DomainLearningLogic witness, string logXmlFilename)
        {
            var engine = new SynthesisEngine(
                grammar: g,
                config: new SynthesisEngine.Config
            {
                Strategies  = new[] { new DeductiveSynthesis(witness) },
                UseThreads  = true,
                LogListener = new LogListener()
            }
                );
            var consistentPrograms = engine.LearnGrammar(s);

            engine.Configuration.LogListener.SaveLogToXML(logXmlFilename);

            return(consistentPrograms);
        }
コード例 #12
0
        public void TestLearnStringTransformation()
        {
            var grammar = DSLCompiler.LoadGrammarFromFile("../../../substring.grammar");

            printGrammar(grammar);
            SynthesisEngine prose = new SynthesisEngine(grammar.Value);

            var input    = State.Create(grammar.Value.InputSymbol, "Bjoern Hartmann");
            var examples = new Dictionary <State, object> {
                { input, "B. Hartmann" }
            };
            var spec       = new ExampleSpec(examples);
            var learnedSet = prose.LearnGrammar(spec);
            var output     = learnedSet.RealizedPrograms.First().Invoke(input) as string;

            Assert.AreEqual("Hartmann, B.", output);
        }
コード例 #13
0
ファイル: SubstringTest.cs プロジェクト: dolphingarlic/prose
        public void TestLearnSubstringPositiveAbsPosSecOcurrenceOneExp()
        {
            Result <Grammar> grammar = CompileGrammar();
            SynthesisEngine  prose   = ConfigureSynthesis(grammar.Value);

            State firstInput = State.CreateForExecution(grammar.Value.InputSymbol, "16-Feb-2016");
            var   examples   = new Dictionary <State, object> {
                { firstInput, "16" }
            };
            var spec = new ExampleSpec(examples);

            ProgramSet learnedSet = prose.LearnGrammar(spec);

            Assert.IsTrue(learnedSet.RealizedPrograms.Count() > 1);
            foreach (ProgramNode program in learnedSet.RealizedPrograms)
            {
                var output = program.Invoke(firstInput) as string;
                Assert.AreEqual("16", output);
            }
        }
コード例 #14
0
        private static ProgramSet Learn(Grammar grammar, DomainLearningLogic logic, params Tuple <string, string, Order>[] examples)
        {
            Console.WriteLine();
            var dd = new Dictionary <State, object>();

            foreach (var example in examples)
            {
                var a = example.Item1;
                var b = example.Item2;
                var o = example.Item3;
                switch (o)
                {
                case Order.Greater:
                    Console.WriteLine(String.Format("\"{0}\", \"{1}\"", a, b));
                    break;

                case Order.Less:
                    Console.WriteLine(String.Format("\"{1}\", \"{0}\"", a, b));
                    break;

                case Order.Equal:
                    Console.WriteLine(String.Format("\"{0}\"\n\"{1}\"", a, b));
                    break;
                }
                var   inp    = new Tuple <string, string>(a, b);
                Order output = o;
                var   input  = State.Create(grammar.InputSymbol, inp);
                dd[input] = output;
            }
            var spec   = new ExampleSpec(dd);
            var engine = new SynthesisEngine(grammar, new SynthesisEngine.Config {
                UseThreads = false,
                Strategies = new ISynthesisStrategy[] {
                    new EnumerativeSynthesis(),
                    new DeductiveSynthesis(logic),
                },
                LogListener = new LogListener(),
            });

            return(engine.LearnGrammar(spec));
        }
コード例 #15
0
        public static void Main(string[] args)
        {
            var parseResult = DSLCompiler.ParseGrammarFromFile("MatrixGrammar.grammar");

            parseResult.TraceDiagnostics();
            var grammar = parseResult.Value;

            Console.WriteLine(grammar.Name);
            //var ast = ProgramNode.Parse("matPlus(matPlus(M, M), M))", grammar, ASTSerializationFormat.HumanReadable);
            //double[] inp = new double[] { 2, 2, 1, 2, 3, 4 };
            System.Random random = new System.Random();
            double[]      inp    = new double[10002];
            double[]      opt    = new double[10002];
            inp[0] = 100;
            inp[1] = 100;
            opt[0] = 100;
            opt[1] = 100;
            for (int i = 2; i < 10002; i++)
            {
                double val = random.NextDouble();
                inp[i] = val; // NextDouble already returns double from [0,1)
                opt[i] = 2 * val;
            }
            //double[] opt = new double[] { 2, 2, 2, 4, 6, 8 };
            var input = State.Create(grammar.InputSymbol, inp);
            var spec  = new ExampleSpec(new Dictionary <State, object> {
                [input] = opt
            });
            //var engine = new SynthesisEngine(grammar);
            var engine = new SynthesisEngine(grammar, new SynthesisEngine.Config
            {
                Strategies = new ISynthesisStrategy[]
                {
                    new DeductiveSynthesis(new MatrixLogic(grammar)),
                }
            });
            ProgramSet learned = engine.LearnGrammar(spec);

            Console.Write(learned.Size);
        }
コード例 #16
0
ファイル: Program.cs プロジェクト: andrewhead/PROSE-Examples
    static void testSynthesis(Grammar grammar, Dictionary<State, object> examples)
    {
        var spec = new ExampleSpec(examples);
        var engine = new SynthesisEngine(grammar);
        ProgramSet learned = engine.LearnGrammar(spec);

        Console.WriteLine("Examples:");
        foreach (KeyValuePair<State, object> ex in examples)
            Console.WriteLine("(Input: " + ex.Key + ", Output: " + ex.Value + ")");

        Console.WriteLine("These are the programs that I learned");
        foreach (ProgramNode p in learned.RealizedPrograms)
        {
            Console.WriteLine(p);
            foreach (State inputExample in examples.Keys)
            {
                Console.WriteLine("Test: (Input: " + inputExample + ", Output: " + p.Invoke(inputExample) + ")");
            }
        }
        Console.WriteLine();

    }
コード例 #17
0
        public static List <TProgram> Learn(IEnumerable <TExample> examples, int k)
        {
            Setup();
            var constraints = examples.ToDictionary(
                e => State.CreateForLearning(InputSymbol, e.input),
                e => (object)e.output
                );
            Spec spec = new ExampleSpec(constraints);

            _stopwatch.Restart();
            var programSet = _engine.LearnGrammar(spec);

            // _engine.Learn_GrammarTopK(spec, _scorer, k);
            _stopwatch.Stop();

            Log.Info("Transformer: {0} program(s) synthesized, time elapsed {1} ms.",
                     programSet.Size, _stopwatch.ElapsedMilliseconds);

            // Test soundness.
            // TODO: remove test
            foreach (var p in programSet.RealizedPrograms.Take(k))
            {
                foreach (var e in examples)
                {
                    var tree = p.Invoke(State.CreateForExecution(InputSymbol, e.input)) as SyntaxNode;
                    if (tree == null || !tree.IdenticalTo(e.output))
                    {
                        Console.WriteLine(String.Join(" ", tree.Leaves().Select(l => l.code)));
                        Debug.Assert(false,
                                     String.Format("Program\n{0}\nfailed on test case\n{1}\n", p, e));
                    }
                }
            }

            return(programSet.RealizedPrograms.Take(k)
                   .Select(p => new TProgram(p, p.GetFeatureValue(_scorer), InputSymbol))
                   .ToList());
        }
コード例 #18
0
ファイル: Utils.cs プロジェクト: gustavoasoares/prose
        public static ProgramNode Learn(Grammar grammar, Spec spec)
        {
            var engine = new SynthesisEngine(grammar, new SynthesisEngine.Config
            {
                UseThreads = false,
                LogListener = new LogListener(),
            });
            ProgramSet consistentPrograms = engine.LearnGrammar(spec);
            engine.Configuration.LogListener.SaveLogToXML("learning.log.xml");

            //foreach (ProgramNode p in consistentPrograms.RealizedPrograms) {
            //    Console.WriteLine(p);
            //}

            ProgramNode bestProgram = consistentPrograms.TopK("Score").FirstOrDefault();
            if (bestProgram == null)
            {
                WriteColored(ConsoleColor.Red, "No program :(");
                return null;
            }
            var score = bestProgram["Score"];
            WriteColored(ConsoleColor.Cyan, $"[score = {score:F3}] {bestProgram}");
            return bestProgram;
        }