Пример #1
0
        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++;
            }
        }
Пример #2
0
        public PartialImageSpec WitnessRecolor_SingleParam(GrammarRule rule, PartialImageSpec spec, ExampleSpec colorSpec)
        {
            var result = new Dictionary <State, object>();

            foreach (var example in spec.PartialImageExamples)
            {
                State inputState = example.Key;
                var   output     = example.Value as int[][];
                int   color      = (int)colorSpec.Examples[inputState];
                // create blank preimage
                int[][] preimage = new int[output.Length][];
                for (int i = 0; i < preimage.Length; i++)
                {
                    preimage[i] = new int[output[i].Length];
                }
                // loop through all pixels of output image
                for (int i = 0; i < output.Length; i++)
                {
                    for (int j = 0; j < output[0].Length; j++)
                    {
                        // if output is 0 then the preimage must also be 0 because recolor does not affect pixels with color 0
                        if (output[i][j] == 0)
                        {
                            preimage[i][j] = 0;
                        }
                        // If it's the number on output, it could be anything on input
                        else if (output[i][j] == color)
                        {
                            preimage[i][j] = 10;
                        }
                        // If it's 10, then great (that means "any nonzero positive number in [1-9]!"
                        //  We'll just assume it satisfied our color, and set the preimage appropriately
                        else if (output[i][j] == 10)
                        {
                            preimage[i][j] = 10;
                        }
                        // If it's a positive number that isn't 10, that's a bad sign
                        // It means we couldn't have done recolor with the color we were provided!
                        else if (output[i][j] > 0 && output[i][j] < 10)
                        {
                            Console.WriteLine("Ending Early in WitnessRecolor_SingleParam, found multiple nonzero values on output");
                            return(null);
                        }
                        // Negative case, the output is an over-specified negative number
                        else if (output[i][j] < 0)
                        {
                            // If our output value is -color, that won't work since that would mean
                            // we applied a recolor(image, color) and got an output that wasn't color
                            if (output[i][j] == -color)
                            {
                                Console.WriteLine("Ending Early in WitnessRecolor_SingleParam, found -color on output for Recolor(image, color)");
                                return(null);
                            }
                            // Otherwise, that's alright! We can just set the preimage to be whatever we want (10)
                            else
                            {
                                preimage[i][j] = 10;
                            }
                        }
                        else
                        {
                            throw new NotSupportedException();
                        }
                    }
                }
                result[inputState] = preimage;
            }
            return(new PartialImageSpec(result));
        }
Пример #3
0
        public PartialImageSpec WitnessFilter_SingleParam(GrammarRule rule, PartialImageSpec spec, ExampleSpec colorSpec)
        {
            var result = new Dictionary <State, object>();

            foreach (var example in spec.PartialImageExamples)
            {
                State inputState = example.Key;
                var   output     = example.Value as int[][];
                int   color      = (int)colorSpec.Examples[inputState];
                // create blank preimage
                int[][] preimage = new int[output.Length][];
                for (int i = 0; i < preimage.Length; i++)
                {
                    preimage[i] = new int[output[i].Length];
                }

                // loop through all pixels of output image
                for (int i = 0; i < output.Length; i++)
                {
                    for (int j = 0; j < output[0].Length; j++)
                    {
                        // if output is 0 then the preimage must be any color other than color, so -color
                        if (output[i][j] == 0)
                        {
                            preimage[i][j] = -color;
                        }
                        else if (output[i][j] == color)
                        {
                            preimage[i][j] = color;
                        }
                        // We don't care what the output is, we just know it's nonzero and positive, like our color!
                        else if (output[i][j] == 10)
                        {
                            preimage[i][j] = color;
                        }
                        // We found a positive value, but it wasn't our expected value after applying filter,
                        // so return null! (unless it's 10!)
                        else if (output[i][j] > 0 && output[i][j] < 10)
                        {
                            Console.WriteLine("Ending early on WitnessFilter_SingleParam, multiple nonzero values on output");
                            return(null);
                        }
                        // Negative case, the output is an over-specified negative number
                        else if (output[i][j] < 0)
                        {
                            // If our output value is -color, that's only a problem
                            // when our input is color. So set preimage to -color
                            if (output[i][j] == -color)
                            {
                                preimage[i][j] = -color;
                            }
                            // Pretty sure this scenario is impossible?
                            else
                            {
                                throw new Exception("Inconceivable!");
                            }
                        }
                        else
                        {
                            throw new NotSupportedException();
                        }
                    }
                }
                result[inputState] = preimage;
            }
            return(new PartialImageSpec(result));
        }
Пример #4
0
        public DisjunctiveExamplesSpec WitnessFilter_ColorParam(GrammarRule rule, PartialImageSpec spec)
        {
            var result = new Dictionary <State, IEnumerable <object> >();

            foreach (KeyValuePair <State, object> example in spec.PartialImageExamples)
            {
                State inputState = example.Key;
                var   output     = example.Value as int[][];
                // Loop through all pixels of output image and confirm there's only 1 nonzero color value
                int candidate = -11;
                for (int i = 0; i < output.Length; i++)
                {
                    for (int j = 0; j < output[i].Length; j++)
                    {
                        if (output[i][j] != 0)
                        {
                            // First time we encounter a nonzero value, so set candidate to be that
                            if (candidate == -11)
                            {
                                candidate = output[i][j];
                            }
                            // Second unique nonzero value found; means we didn't run Filter
                            else if (candidate != output[i][j])
                            {
                                Console.WriteLine("Ending Early on WitnessFilter_ColorParam, found multiple nonzero values on output");
                                return(null);
                            }
                        }
                    }
                }
                // Didn't get a single nonzero entry, so return null
                if (candidate == -11)
                {
                    Console.WriteLine("Ending Early on WitnessFilter_ColorParam, all zeroes on output");
                    return(null);
                }
                var occurrences = new List <int>();
                // Negative number, meaning it can be anything but -candidate in [1-9]
                if (candidate < 0)
                {
                    for (int i = 1; i < 10; i++)
                    {
                        if (i == -candidate)
                        {
                            continue;
                        }
                        occurrences.Add(i);
                    }
                }
                // This could be literally *any* nonzero, positive value in [1-9]!
                else if (candidate == 10)
                {
                    for (int i = 1; i < 10; i++)
                    {
                        occurrences.Add(i);
                    }
                }
                // Otherwise, it's a normal number in [1-9] and we can add it
                else
                {
                    occurrences.Add(candidate);
                }
                result[inputState] = occurrences.Cast <object>();
            }
            return(new DisjunctiveExamplesSpec(result));
        }
Пример #5
0
        //public DisjunctiveExamplesSpec WitnessRecolor_ColorParam_DepSingle(GrammarRule rule, ExampleSpec spec, PartialImageSpec singleSpec)
        public DisjunctiveExamplesSpec WitnessRecolor_ColorParam(GrammarRule rule, PartialImageSpec spec)
        {
            var result = new Dictionary <State, IEnumerable <object> >();

            foreach (KeyValuePair <State, object> example in spec.PartialImageExamples)
            {
                State inputState = example.Key;
                var   output     = example.Value as int[][];
                int   candidate  = -11;
                for (int i = 0; i < output.Length; i++)
                {
                    for (int j = 0; j < output[i].Length; j++)
                    {
                        if (output[i][j] != 0)
                        {
                            // First nonzero found!
                            if (candidate == -11)
                            {
                                candidate = output[i][j];
                            }
                            // Second unique nonzero --> invalid entry
                            else if (candidate != output[i][j])
                            {
                                Console.WriteLine("Ending Early on WitnessRecolor_ColorParam, found multiple nonzero values on output");
                                return(null);
                            }
                        }
                    }
                }
                // No candidates found, so return null
                if (candidate == -11)
                {
                    Console.WriteLine("Ending Early on WitnessRecolor_ColorParam, all zeroes on output");
                    return(null);
                }
                var occurrences = new List <int>();
                // Negative number, meaning it can be anything but -candidate
                if (candidate < 0)
                {
                    for (int i = 1; i < 10; i++)
                    {
                        if (i == -candidate)
                        {
                            continue;
                        }
                        occurrences.Add(i);
                    }
                }
                // Could be any number at all
                else if (candidate == 10)
                {
                    Console.WriteLine("Got here");
                    for (int i = 1; i < 10; i++)
                    {
                        occurrences.Add(i);
                    }
                }
                // Normal number so add it
                else
                {
                    occurrences.Add(candidate);
                }
                result[inputState] = occurrences.Cast <object>();
            }
            return(new DisjunctiveExamplesSpec(result));
        }