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() { 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("=========================="); } }