public void TestLearnSubstringOneExample() { Result <Grammar> grammar = CompileGrammar(); SynthesisEngine prose = ConfigureSynthesis(grammar.Value); State input = State.CreateForExecution(grammar.Value.InputSymbol, "Toby Miller"); var examples = new Dictionary <State, object> { { input, "Miller" } }; var spec = new ExampleSpec(examples); var scoreFeature = new RankingScore(grammar.Value); ProgramSet topPrograms = prose.LearnGrammarTopK(spec, scoreFeature, 1, null); ProgramNode topProgram = topPrograms.RealizedPrograms.First(); var x = topProgram.PrintAST(); var y = ProgramNode.Parse(x, grammar.Value); // var y is null. ==> SDK method var se = new ASTSerialization.Serialization(grammar.Value); var xe = se.PrintXML(topProgram); xe.Save(SavedProgramAST); topProgram = se.Parse(xe); // var topProgram is ok. ==> ASTSerialization.Serialization method var output = topProgram.Invoke(input) as string; Assert.AreEqual("Miller", output); State input2 = State.CreateForExecution(grammar.Value.InputSymbol, "Courtney Lynch"); var output2 = topProgram.Invoke(input2) as string; Assert.AreEqual("Lynch", output2); }
private static void LearnFromNewExample() { Console.Out.Write("Provide a new input-output example (e.g., \"(Sumit Gulwani)\",\"Gulwani\"): "); try { string input = Console.ReadLine(); if (input != null) { var startFirstExample = input.IndexOf("\"", StringComparison.Ordinal) + 1; var endFirstExample = input.IndexOf("\"", startFirstExample + 1, StringComparison.Ordinal) + 1; var startSecondExample = input.IndexOf("\"", endFirstExample + 1, StringComparison.Ordinal) + 1; var endSecondExample = input.IndexOf("\"", startSecondExample + 1, StringComparison.Ordinal) + 1; if ((startFirstExample >= endFirstExample) || (startSecondExample >= endSecondExample)) { throw new Exception("Invalid example format. Please try again. input and out should be between quotes"); } var inputExample = input.Substring(startFirstExample, endFirstExample - startFirstExample - 1); var outputExample = input.Substring(startSecondExample, endSecondExample - startSecondExample - 1); var inputState = State.CreateForExecution(Grammar.InputSymbol, inputExample); Examples.Add(inputState, outputExample); } } catch (Exception) { throw new Exception("Invalid example format. Please try again. input and out should be between quotes"); } var spec = new ExampleSpec(Examples); Console.Out.WriteLine("Learning a program for examples:"); foreach (var example in Examples) { Console.WriteLine("\"{0}\" -> \"{1}\"", example.Key.Bindings.First().Value, example.Value); } var scoreFeature = new RankingScore(Grammar); ProgramSet topPrograms = _prose.LearnGrammarTopK(spec, scoreFeature, 4, null); if (topPrograms.IsEmpty) { throw new Exception("No program was found for this specification."); } _topProgram = topPrograms.RealizedPrograms.First(); Console.Out.WriteLine("Top 4 learned programs:"); var counter = 1; foreach (var program in topPrograms.RealizedPrograms) { if (counter > 4) { break; } Console.Out.WriteLine("=========================="); Console.Out.WriteLine("Program {0}: ", counter); Console.Out.WriteLine(program.PrintAST(ASTSerializationFormat.HumanReadable)); counter++; } }
public IEnumerable <Transformation> LearnTransformations(List <Tuple <string, string> > examples, int numberOfPrograms = 1, string ranking = "specific") { var spec = CreateExampleSpec(examples); //TODO: this is not thread safe. If multiple instances of Refazer are changing //the value of the ranking scores, we can have a problem. RankingScore.ScoreForContext = ranking.Equals("specific") ? 100 : -100; var learned = _prose.LearnGrammarTopK(spec, "Score", numberOfPrograms); var uniqueTransformations = new List <ProgramNode>(); //filter repetitive transformations foreach (var programNode in learned) { var exists = false; foreach (var uniqueTransformation in uniqueTransformations) { if (programNode.ToString().Equals(uniqueTransformation.ToString())) { exists = true; break; } } if (!exists) { uniqueTransformations.Add(programNode); } } uniqueTransformations = uniqueTransformations.Count > numberOfPrograms ? uniqueTransformations.GetRange(0, numberOfPrograms) : uniqueTransformations; return(uniqueTransformations.Select(e => new PythonTransformation(e))); }
private static void LearnPrograms() { var spec = new ExampleSpec(examples); Console.Out.WriteLine("Learning a program for examples: "); foreach (var example in examples) { Console.WriteLine("\"" + example.Key.Bindings.First().Value + "\" -> \"" + example.Value + "\""); } var scoreFeature = new RankingScore(grammar); var topPrograms = prose.LearnGrammarTopK(spec, scoreFeature, 4, null); if (topPrograms.IsEmpty()) { throw new Exception("No program was found for this specification."); } topProgram = topPrograms.First(); Console.Out.WriteLine("Top 4 learned programs: "); foreach (var program in topPrograms) { Console.Out.WriteLine("-----------------------------"); Console.Out.WriteLine(program.PrintAST(Microsoft.ProgramSynthesis.AST.ASTSerializationFormat.HumanReadable)); } }
private static void LearnFromNewExample() { Console.Out.Write("Provide a new input-output example as \"input\", \"output\":\n"); try { string input = Console.ReadLine(); var startFirstExample = input.IndexOf("\"") + 1; var endFirstExample = input.IndexOf("\"", startFirstExample + 1) + 1; var startSecondExample = input.IndexOf("\"", endFirstExample + 1) + 1; var endSecondExample = input.IndexOf("\"", startSecondExample + 1) + 1; if ((startFirstExample >= endFirstExample) || (startSecondExample >= endSecondExample)) { throw new Exception("Invalid example format. Please try again. input and out should be between quotes"); } var inputExample = input.Substring(startFirstExample, endFirstExample - startFirstExample - 1); var outputExample = input.Substring(startSecondExample, endSecondExample - startSecondExample - 1); var inputState = State.CreateForExecution(grammar.InputSymbol, inputExample); examples.Add(inputState, outputExample); } catch (Exception) { throw new Exception("Invalid example format. Please try again. input and out should be between quotes"); } var spec = new ExampleSpec(examples); Console.Out.WriteLine("Learning a program for examples:"); foreach (var example in examples) { Console.WriteLine("\"" + example.Key.Bindings.First().Value + "\" -> \"" + example.Value + "\""); } var scoreFeature = new RankingScore(grammar); ProgramSet topPrograms = prose.LearnGrammarTopK(spec, scoreFeature, 4, null); if (topPrograms.IsEmpty) { throw new Exception("No program was found for this specification."); } topProgram = topPrograms.RealizedPrograms.First(); Console.Out.WriteLine("Top 4 learned programs:"); var counter = 1; foreach (var program in topPrograms.RealizedPrograms) { if (counter > 4) { break; } Console.Out.WriteLine("=========================="); Console.Out.WriteLine("Program " + counter + ": "); Console.Out.WriteLine(program.PrintAST(Microsoft.ProgramSynthesis.AST.ASTSerializationFormat.HumanReadable)); counter++; } }
public IEnumerable <Transformation> LearnTransformations(List <Tuple <string, string> > examples, int numberOfPrograms = 1, string ranking = "specific") { var spec = CreateExampleSpec(examples); //TODO: this is not thread safe. If multiple instances of Refazer are changing //the value of the ranking scores, we can have a problem. RankingScore.ScoreForContext = ranking.Equals("specific") ? 100 : -100; var scoreFeature = new RankingScore(Grammar.Value); DomainLearningLogic learningLogic = new WitnessFunctions(Grammar.Value); _prose = new SynthesisEngine(Grammar.Value, new SynthesisEngine.Config { LogListener = new LogListener(), Strategies = new[] { new DeductiveSynthesis(learningLogic) }, UseThreads = false, CacheSize = int.MaxValue }); var learned = _prose.LearnGrammarTopK(spec, scoreFeature, 1); var uniqueTransformations = new List <ProgramNode>(); //filter repetitive transformations foreach (var programNode in learned) { var exists = false; foreach (var uniqueTransformation in uniqueTransformations) { if (programNode.ToString().Equals(uniqueTransformation.ToString())) { exists = true; break; } } if (!exists) { uniqueTransformations.Add(programNode); } } uniqueTransformations = uniqueTransformations.Count > numberOfPrograms ? uniqueTransformations.GetRange(0, numberOfPrograms) : uniqueTransformations; return(uniqueTransformations.Select(e => new PythonTransformation(e))); }
public ActionResult LearnTransformation([FromBody] LearnTransformRequestBody requestBody) { try { Console.WriteLine(requestBody.Instance); ProgramSet learned; using (var ctx = new Context()) { var inputExamples = Utils.GetInputOutputExamplesModified(ctx, requestBody.InputOutputExamples, SmtPrefix, requestBody.DeclareStatements); var spec = Utils.CreateExampleSpec(_grammar, inputExamples); RankingScore.ScoreForContext = 100; var scoreFeature = new RankingScore(_grammar.Value); DomainLearningLogic learningLogic = new WitnessFunctions(_grammar.Value); _prose = new SynthesisEngine(_grammar.Value, new SynthesisEngine.Config { LogListener = new LogListener(), Strategies = new ISynthesisStrategy[] { new DeductiveSynthesis(learningLogic) }, UseThreads = false, CacheSize = int.MaxValue }); learned = _prose.LearnGrammarTopK(spec, scoreFeature); } var finalPrograms = learned.RealizedPrograms.Select(program => new FinalProgram(program.ToString(), program.PrintAST())).ToList(); if (finalPrograms.Count == 0) { Console.WriteLine("No Programs Found"); } foreach (var program in finalPrograms) { Console.WriteLine(program.HumanReadableAst); } return(Ok(JsonConvert.SerializeObject(finalPrograms))); } catch (Exception ex) { Console.WriteLine(ex.StackTrace); Console.WriteLine(ex.TargetSite); Console.WriteLine("Error: " + ex.Message); return(StatusCode(500, ex.Message)); } }
public void TestLearnSubstringNegativeAbsPosRanking() { Result <Grammar> grammar = CompileGrammar(); SynthesisEngine prose = ConfigureSynthesis(grammar.Value); State firstInput = State.CreateForExecution(grammar.Value.InputSymbol, "(Toby Miller)"); var examples = new Dictionary <State, object> { { firstInput, "Toby Miller" } }; var spec = new ExampleSpec(examples); var scoreFeature = new RankingScore(grammar.Value); ProgramSet topPrograms = prose.LearnGrammarTopK(spec, scoreFeature, 1, null); ProgramNode topProgram = topPrograms.RealizedPrograms.First(); var output = topProgram.Invoke(firstInput) as string; Assert.AreEqual("Toby Miller", output); State secondInput = State.CreateForExecution(grammar.Value.InputSymbol, "(Courtney Lynch)"); output = topProgram.Invoke(secondInput) as string; Assert.AreEqual("Courtney Lynch", output); }
private static void Main(string[] args) { _prose = ConfigureSynthesis(); int[][] inputTest = new int[4][]; inputTest[0] = new int[] { 0, 2, 2, 0, 8, 3 }; inputTest[1] = new int[] { 0, 0, 0, 0, 0, 0 }; inputTest[2] = new int[] { 4, 1, 1, 1, 1, 1 }; inputTest[3] = new int[] { 9, 8, 4, 3, 0, 0 }; // expected program Recolor(image, 5) int[][] outputTest1 = new int[4][]; outputTest1[0] = new int[] { 0, 5, 5, 0, 5, 5 }; outputTest1[1] = new int[] { 0, 0, 0, 0, 0, 0 }; outputTest1[2] = new int[] { 5, 5, 5, 5, 5, 5 }; outputTest1[3] = new int[] { 5, 5, 5, 5, 0, 0 }; // expected program Filter(image, 2) int[][] outputTest2 = new int[4][]; outputTest2[0] = new int[] { 0, 2, 2, 0, 0, 0 }; outputTest2[1] = new int[] { 0, 0, 0, 0, 0, 0 }; outputTest2[2] = new int[] { 0, 0, 0, 0, 0, 0 }; outputTest2[3] = new int[] { 0, 0, 0, 0, 0, 0 }; // expected program Recolor(Filter(image, 2), 3) int[][] outputTest3 = new int[4][]; outputTest3[0] = new int[] { 0, 3, 3, 0, 0, 0 }; outputTest3[1] = new int[] { 0, 0, 0, 0, 0, 0 }; outputTest3[2] = new int[] { 0, 0, 0, 0, 0, 0 }; outputTest3[3] = new int[] { 0, 0, 0, 0, 0, 0 }; State inputState = State.CreateForExecution(Grammar.InputSymbol, inputTest); //Examples.Add(inputState, outputTest1); //Examples.Add(inputState, outputTest2); Examples.Add(inputState, outputTest3); //Examples.Add(inputState, inputTest); //var spec = new ExampleSpec(Examples); var spec = new PartialImageSpec(Examples); Console.WriteLine("Learning a program for examples:"); foreach (KeyValuePair <State, object> example in Examples) { Console.WriteLine("\"{0}\" -> \"{1}\"", example.Key.Bindings.First().Value, example.Value); } var scoreFeature = new RankingScore(Grammar); ProgramSet topPrograms = _prose.LearnGrammarTopK(spec, scoreFeature, 4, null); if (topPrograms.IsEmpty) { Console.WriteLine(spec); // throw new Exception("No program was found for this specification."); } _topProgram = topPrograms.RealizedPrograms.First(); Console.WriteLine("Top 4 learned programs:"); var counter = 1; foreach (ProgramNode program in topPrograms.RealizedPrograms) { if (counter > 4) { break; } Console.WriteLine("=========================="); Console.WriteLine("Program {0}: ", counter); Console.WriteLine(program.PrintAST(ASTSerializationFormat.HumanReadable)); counter++; } }
private static void LearnFromNewExample() { Console.Out.Write("Provide a new input-output example all integers only (e.g., {[1, 2, 3, 4], 7}:"); //\"(Sumit Gulwani)\",\"Gulwani\"): "); try { string input = Console.ReadLine(); if (input != null) { int startpos = input.IndexOf("["); int endpos = input.IndexOf("]"); int final_endpos = input.IndexOf("}"); int num_char_res = final_endpos - (endpos + 3); int des_result = int.Parse(input.Substring(endpos + 3, num_char_res).ToString()); int num_char = endpos - startpos; string substr = input.Substring(startpos + 1, num_char - 1); char[] separator = { ',' }; string[] substrlist = substr.Split(separator); var numbers = new List <int>(); foreach (string x in substrlist) { string temp = x.Trim(); numbers.Add(int.Parse(temp)); } if (input[0] != '{' || input[1] != '[') { throw new Exception( "Invalid example format. Please try again. Follow exact format, including presense/absence of whitespaces"); } int[] inputExample = numbers.ToArray(); int outputExample = des_result; State inputState = State.CreateForExecution(Grammar.InputSymbol, inputExample); Examples.Add(inputState, outputExample); } } catch (Exception) { throw new Exception("Invalid example format. Please try again. Follow exact format, including presense/absence of whitespaces"); } var spec = new ExampleSpec(Examples); Console.Out.WriteLine("Learning a program for examples:"); foreach (KeyValuePair <State, object> example in Examples) { int[] temp = (int[])example.Key.Bindings.First().Value; Console.WriteLine("\"{0}\" -> \"{1}\"", example.Key.Bindings.First().Value, example.Value); } var scoreFeature = new RankingScore(Grammar); ProgramSet topPrograms = _prose.LearnGrammarTopK(spec, scoreFeature, 50, null); if (topPrograms.IsEmpty) { throw new Exception("No program was found for this specification."); } _topProgram = topPrograms.RealizedPrograms.First(); Console.Out.WriteLine("Top 4 learned programs:"); var counter = 1; foreach (ProgramNode program in topPrograms.RealizedPrograms) { if (counter > 50) { break; } Console.Out.WriteLine("=========================="); Console.Out.WriteLine("Program {0}: ", counter); Console.Out.WriteLine(program.PrintAST(ASTSerializationFormat.HumanReadable)); counter++; } }
private static void LearnFromNewExample() { Console.Out.Write("Enter 4 elements of the input:\n"); try { Dictionary <uint?, uint?> d = new Dictionary <uint?, uint?>(); for (int t = 0; t < 4; t++) { //Console.WriteLine("Please enter integer" + (t+1) +": "); d.Add((uint)Convert.ToInt32(Console.ReadLine()), (uint)t); } Console.WriteLine("Please enter desired output: "); uint output = (uint)Convert.ToInt32(Console.ReadLine()); State inputState = State.CreateForExecution(Grammar.InputSymbol, d); Examples.Add(inputState, output); } catch (Exception) { throw new Exception("Invalid example"); } var spec = new ExampleSpec(Examples); Console.Out.WriteLine("Learning a program for examples:"); foreach (KeyValuePair <State, object> example in Examples) { Dictionary <uint?, uint?> temp = (Dictionary <uint?, uint?>)example.Key.Bindings.First().Value; Console.Write("["); foreach (uint?m in temp.Keys) { Console.Write(m + ","); } Console.Write("] "); Console.Write("-> \"{0}\"\n", example.Value); } var scoreFeature = new RankingScore(Grammar); ProgramSet topPrograms = _prose.LearnGrammarTopK(spec, scoreFeature, 4, null); if (topPrograms.IsEmpty) { throw new Exception("No program was found for this specification."); } _topProgram = topPrograms.RealizedPrograms.First(); Console.Out.WriteLine("Top 4 learned programs:"); var counter = 1; foreach (ProgramNode program in topPrograms.RealizedPrograms) { if (counter > 4) { break; } Console.Out.WriteLine("=========================="); Console.Out.WriteLine("Program {0}: ", counter); Console.Out.WriteLine(program.PrintAST(ASTSerializationFormat.HumanReadable)); counter++; } }
private static void LearnFromNewExample() { Task task; while (true) { try { Console.Out.Write("Enter a task name: "); _taskName = Console.ReadLine().Trim(); task = TaskLoader.LoadTask("tasks/" + _taskName + ".json"); break; } catch (FileNotFoundException) { Console.Out.WriteLine("Could not find task " + _taskName + ".json. Try again."); } catch (DirectoryNotFoundException) { Console.Out.WriteLine("Could not find task " + _taskName + ".json. Try again."); } catch (Exception) { Console.Out.WriteLine("Unable to parse " + _taskName + ".json. Try again."); } } Console.Out.WriteLine(@"List the functions that this task is invariant under (i.e. g such that F(x)=y => F(g(x))=g(y) c Color mapping r Rotation f Reflection t Translation"); List <Invariant> invariants = new List <Invariant>(); while (true) { string invInput = Console.ReadLine().Trim(); foreach (char c in invInput) { switch (c) { case 'c': invariants.Add(new ColormapInvariant()); break; case 'r': invariants.Add(new RotationInvariant()); break; case 'f': invariants.Add(new ReflectionInvariant()); break; case 't': invariants.Add(new TranslationInvariant()); break; default: Console.Out.WriteLine("Unknown invariant " + c); continue; } } break; } Console.Out.WriteLine(@"List the functions that this task is equivalent under (i.e. g such that F(x)=y => F(g(x))=y c Color mapping r Rotation f Reflection t Translation"); List <Invariant> equivalences = new List <Invariant>(); while (true) { string invInput = Console.ReadLine().Trim(); foreach (char c in invInput) { switch (c) { case 'c': equivalences.Add(new ColormapInvariant()); break; case 'r': equivalences.Add(new RotationInvariant()); break; case 'f': equivalences.Add(new ReflectionInvariant()); break; case 't': equivalences.Add(new TranslationInvariant()); break; default: Console.Out.WriteLine("Unknown equivalence " + c); continue; } } break; } Example[] train = task.train; Console.Out.WriteLine("Learning a program for input examples:\n"); Examples.Clear(); foreach (Example example in train) { example.Print(); Console.Out.WriteLine(); State inputState = State.CreateForExecution(Grammar.InputSymbol, new Image(example.input)); Examples.Add(inputState, new AbstractImage(new Image(example.output))); } if (equivalences.Count() > 0 || invariants.Count() > 0) { Console.Out.WriteLine("and generated examples:\n"); foreach (Invariant inv in invariants) { inv.reseed(); foreach (Example example in train) { Image newIn = inv.generate(new Image(example.input)); Image newOut = inv.generate(new Image(example.output)); Example newExample = Example.FromImages(newIn, newOut); newExample.Print(); Console.Out.WriteLine(); State newInState = State.CreateForExecution(Grammar.InputSymbol, newIn); Examples.Add(newInState, new AbstractImage(newOut)); } } foreach (Invariant eq in equivalences) { eq.reseed(); foreach (Example example in train) { Image newIn = eq.generate(new Image(example.input)); Image newOut = new Image(example.output); Example newExample = Example.FromImages(newIn, newOut); newExample.Print(); Console.Out.WriteLine(); State newInState = State.CreateForExecution(Grammar.InputSymbol, newIn); Examples.Add(newInState, new AbstractImage(newOut)); } } } //var spec = new ExampleSpec(Examples); var spec = new AbstractImageSpec(Examples); var scoreFeature = new RankingScore(Grammar); int K = 4; ProgramSet topPrograms = _prose.LearnGrammarTopK(spec, scoreFeature, K, null); if (topPrograms.IsEmpty) { Console.Out.WriteLine("No program was found for this specification."); } else { _topProgram = topPrograms.RealizedPrograms.First(); Console.Out.WriteLine("Top " + K + " learned programs:"); var counter = 1; foreach (ProgramNode program in topPrograms.RealizedPrograms) { if (counter > 4) { break; } Console.Out.WriteLine("=========================="); Console.Out.WriteLine("Program {0}: ", counter); Console.Out.WriteLine(program.PrintAST(ASTSerializationFormat.HumanReadable)); counter++; } Console.Out.WriteLine("=========================="); } }
private static void LearnFromNewExample() { Console.Out.Write("Provide a new input-output example (e.g., \"10 True\",\"20 False\"): "); try { string line = Console.ReadLine(); if (line == null) { throw new Exception("No Input"); } string[] splittedLine = line.Split(' '); var inputExample = UInt32.Parse(splittedLine[0]); var outputExample = Boolean.Parse(splittedLine[1]); State inputState = State.CreateForExecution(Grammar.InputSymbol, inputExample); // TODO: Check example existence if (Examples.ContainsKey(inputState)) { if ((bool)Examples[inputState] != outputExample) { throw new Exception("Inconsistent Example"); } } else { Examples.Add(inputState, outputExample); } } catch (Exception e) { Console.WriteLine(e.ToString()); throw e; //throw new Exception("Invalid example format. Please try again. input and out should be between quotes"); } var spec = new ExampleSpec(Examples); Console.Out.WriteLine("Learning a program for examples:"); foreach (KeyValuePair <State, object> example in Examples) { Console.WriteLine("\"{0}\" -> \"{1}\"", example.Key.Bindings.First().Value, example.Value); } var scoreFeature = new RankingScore(Grammar); ProgramSet topPrograms = _prose.LearnGrammarTopK(spec, scoreFeature, 1, null); if (topPrograms.IsEmpty) { throw new Exception("No program was found for this specification."); } _topProgram = topPrograms.RealizedPrograms.First(); Console.Out.WriteLine("Top 4 learned programs:"); var counter = 1; foreach (ProgramNode program in topPrograms.RealizedPrograms) { if (counter > 4) { break; } Console.Out.WriteLine("=========================="); Console.Out.WriteLine("Program {0}: ", counter); Console.Out.WriteLine(program.PrintAST(ASTSerializationFormat.HumanReadable)); counter++; } }