Example #1
0
    def test_2_integer_inputs_easy(self):
        """
        The hypothesis should simplify to single interval of one of the 2 variables.
        """

        function = f.two_inputs_bit_more_complex

        code_solution_1 = [
            "def {}(a, b):".format(function.__name__),
            "    return (a>=0.5 and a<=2.5)"
        ]

        code_solution_2 = [
            "def {}(a, b):".format(function.__name__),
            "    return (b>=0.5 and b<=2.5)"
        ]

        r = Rules()
        r.add(a=0, b=0, output=0)
        r.add(a=1, b=1, output=1)
        r.add(a=2, b=2, output=1)
        r.add(a=3, b=3, output=0)

        solution = r.solve(function)

        try:
            self.assertEqual(solution.implementation, code_solution_1)
        except AssertionError:
            self.assertEqual(solution.implementation, code_solution_2)
Example #2
0
    def test_complex_function2(self):
        """
        Test a 10 input function.
        """
        function = f.complex_2
        code = ['def {}(a, b, c, d, e, f, g, h, i, j):'.format(function.__name__),
                '    return a and b and c and d and e and f and g and h and i and j']

        # Random rules.
        r = Rules()

        kwargs = {'a': True, 'b': True, 'c': True, 'd': True, 'e': True, 'f': True, 'g': True, 'h': True, 'i': True,
                  'j': True}

        true_kwargs = copy.copy(kwargs)
        true_kwargs['output'] = True
        r.add(**true_kwargs)
        r.add(**true_kwargs)

        # 1 outlier, the model should learn to ignore it.
        false_kwargs = copy.copy(kwargs)
        false_kwargs['output'] = False
        r.add(**false_kwargs)

        solution = r.solve(function, self)

        self.assertEqual(solution.implementation, code)
Example #3
0
    def test_double_input_will_not_have_effect(self):
        """Even though the input is fed twice the result is the same."""

        solution = ['def {}(a):'.format(f.double_input.__name__),
                    '    return a']

        r = Rules(a=True, output=True)
        r.add(a=True, output=True)
        s = r.solve(f.double_input)
        self.assertEqual(s.implementation, solution)
Example #4
0
    def test_raise_exception_with_non_boolean_output(self):
        """Replaces True output for number 10, then it has a non boolean output, should raise NotImplementedWithMLYet"""

        function = f.only_boolean
        r = Rules()
        r.add(a=True, output=10)
        r.add(a=True, output=10)
        r.add(b=True, output=False)

        with self.assertRaises(NotImplementedWithMLYet):
            r.solve(function)
Example #5
0
    def test_identity(self):
        """
        Test the identity function when the input 'a' has 1 outlier out of 3 samples.
        """

        code = ['def {}(a):'.format(f.identity.__name__),
                '    return a']

        # 2 correct samples.
        r = Rules(a=True, output=True)
        r.add(a=True, output=True)

        # 1 outlier, the model should learn to ignore it.
        r.add(a=True, output=False)
        solution = r.solve(f.identity, self)

        self.assertEqual(solution.implementation, code)
Example #6
0
    def test_and(self):
        """
        Test the and function when the input has 1 outlier out of 3 samples.
        """
        function = f.and_f
        code = ['def {}(a, b):'.format(function.__name__),
                '    return a and b']

        # 2 correct samples
        r = Rules(a=True, b=True, output=True)
        r.add(a=True, b=True, output=True)

        # 1 outlier, the model should learn to ignore it.
        r.add(a=True, b=True, output=False)
        solution = r.solve(function, self)

        self.assertEqual(solution.implementation, code)
Example #7
0
    def test_simple_integer_input(self):
        """
        Simple integer input
        """

        function = f.simple

        code = ["def {}(a):".format(function.__name__), "    return a>=2.5"]

        r = Rules()
        r.add(a=0, output=0)
        r.add(a=1, output=0)
        r.add(a=2, output=0)
        r.add(a=3, output=1)

        solution = r.solve(function)

        self.assertEqual(solution.implementation, code)
Example #8
0
    def test_complex_function(self):
        """
        Test a bit more complex function
        """
        function = f.complex
        code = ['def {}(a, b, c, d):'.format(function.__name__),
                '    return a and b and c or d']

        # 3 correct samples.
        r = Rules(a=True, b=True, c=True, output=True)

        r.add(d=True, output=True)
        r.add(d=True, output=True)

        # 1 outlier, the model should learn to ignore it.
        r.add(d=True, output=False)
        solution = r.solve(function, self)

        self.assertEqual(solution.implementation, code)
Example #9
0
    def test_false_and(self):
        """Has the same combination twice with a output=False while having another one with a contradiction """
        function = f.false_and
        code = ['def {}(a, b):'.format(function.__name__),
                '',
                '    return False']

        r = Rules()

        # 2 correct samples.
        r.add(a=True, b=True, output=False)
        r.add(a=True, b=True, output=False)

        # 1 outlier, the model should learn to ignore it.
        r.add(a=True, b=True, output=True)

        solution = r.solve(function)

        self.assertEqual(solution.implementation, code)
Example #10
0
    def test_many_integer_inputs_easy(self):
        """
        The hypothesis should simplify to single interval of one of the 2 variables.
        """

        function = f.many_inputs_bit_more_complex

        code_abstract_solution = [
            "def {}(a, b, c, d, e, f):".format(function.__name__),
            "    return ({var}>=0.5 and {var}<=2.5)"
        ]

        r = Rules()
        r.add(a=0, b=0, c=0, d=0, e=0, f=0, output=0)
        r.add(a=1, b=1, c=1, d=1, e=1, f=1, output=1)
        r.add(a=2, b=2, c=2, d=2, e=2, f=2, output=1)
        r.add(a=3, b=3, c=3, d=3, e=3, f=3, output=0)

        solution = r.solve(function)

        variables = [
            'a',
            'b',
            'c',
            'd',
            'e',
            'f',
        ]

        for var in variables:
            try:
                code = [
                    code_abstract_solution[0],
                    code_abstract_solution[1].format(var=var)
                ]
                self.assertEqual(solution.implementation, code)
                return  # happy ending
            except AssertionError:
                pass  # still nothing

        raise Exception  #
Example #11
0
    def test_simple2_integer_input(self):
        """
        Bit more complex Simple integer input
        """

        function = f.bit_more_complex

        code = [
            "def {}(a):".format(function.__name__),
            "    return (a>=0.5 and a<=2.5)"
        ]

        r = Rules()
        r.add(a=0, output=0)
        r.add(a=1, output=1)
        r.add(a=2, output=1)
        r.add(a=3, output=0)

        solution = r.solve(function)

        self.assertEqual(solution.implementation, code)
Example #12
0
    def test_2_integer_inputs_bit_more_complex(self):
        """
        Here the QM simplification is tested. There are 2 right solutions.
        """

        function = f.two_inputs_bit_more_complex

        code_solution_1 = [
            "def {}(a, b):".format(function.__name__),
            "    return (b>=2.5 and b<=5.5) or a<=1.5"
        ]

        code_solution_2 = [
            "def {}(a, b):".format(function.__name__),
            "    return (b>=2.5 and b<=5.5) or b<=1.5"
        ]

        r = Rules()
        r.add(a=4, b=6, output=0)
        r.add(a=5, b=5, output=1)
        r.add(a=6, b=4, output=1)
        r.add(a=3, b=3, output=1)
        r.add(a=2, b=2, output=0)
        r.add(a=1, b=1, output=1)

        solution = r.solve(function)

        # Tries 2 valid solutions.
        try:
            self.assertEqual(solution.implementation, code_solution_1)
        except AssertionError:
            self.assertEqual(solution.implementation, code_solution_2)
Example #13
0
    def test_living_rule(self):
        """
        1. Any live cell with fewer than two live neighbours dies, as if caused by underpopulation.
        2. Any live cell with more than three live neighbours dies, as if by overcrowding.
        3. Any live cell with two or three live neighbours lives on to the next generation.
        4. Any dead cell with exactly three live neighbours becomes a live cell.
        """

        r = Rules(rule1=Code(code_str='sum(neighbors) < 2'), output=False)
        r.add(rule2=Code('sum(neighbors) > 3'), output=False)
        r.add(alive=True,
              rule3=Code(code_str='sum(neighbors) == 2'),
              output=True)
        r.add(alive=True,
              rule4=Code(code_str='sum(neighbors) == 3'),
              output=True)
        r.add(alive=False,
              rule5=Code(code_str='sum(neighbors) == 3'),
              output=True)

        r.solve(is_alive, self)
Example #14
0
    def test_not_identity(self):
        """
        Test the not-identity function when the input 'a' has 4 outliers out of 9 samples.
        """
        function = f.not_identity
        code = ['def {}(a):'.format(function.__name__),
                '    return not a']

        # Correct samples
        r = Rules(a=False, output=True)
        r.add(a=False, output=True)
        r.add(a=False, output=True)
        r.add(a=False, output=True)
        r.add(a=False, output=True)

        # Outliers
        r.add(a=False, output=False)
        r.add(a=False, output=False)
        r.add(a=False, output=False)
        r.add(a=False, output=False)

        solution = r.solve(function, self)

        self.assertEqual(solution.implementation, code)
Example #15
0
    def test_2_integer_inputs(self):
        """
        The hypothesis that solves this problem is a perfect square on the plane with coordinates (a, b)
        """

        function = f.two_inputs_bit_more_complex

        code = [
            "def {}(a, b):".format(function.__name__),
            "    return (a>=1.0 and a<=2.0) and (b>=1.0 and b<=2.0)"
        ]

        r = Rules()
        r.add(a=1, b=0, output=0)
        r.add(a=0, b=1, output=0)
        r.add(a=1, b=1, output=1)
        r.add(a=2, b=2, output=1)
        r.add(a=3, b=2, output=0)
        r.add(a=2, b=3, output=0)

        solution = r.solve(function)

        self.assertEqual(solution.implementation, code)
Example #16
0
    def test_2_integer_inputs_variant(self):
        """
        Variant of the test above. It is no longer a square.
        """

        function = f.two_inputs_bit_more_complex

        code = [
            "def {}(a, b):".format(function.__name__),
            "    return (b>=1.0 and b<=2.0) and ((a>=1.5 and a<=2.0) or a<=0.5)"
        ]

        r = Rules()
        r.add(a=1, b=0, output=0)
        r.add(a=0, b=1, output=1)
        r.add(a=1, b=1, output=0)
        r.add(a=2, b=2, output=1)
        r.add(a=3, b=2, output=0)
        r.add(a=2, b=3, output=0)

        solution = r.solve(function)

        self.assertEqual(solution.implementation, code)
Example #17
0
    pass


# A simple and function.
r = Rules(a=True, b=True, output=True)
r.solve(and_function)


@s.solve()
def if_function(a, b):
    pass


# A function with an if statement.
r = Rules(a=False, b=True, output=1)  # non-boolean output
r.add(a=True, b=False, output=0)  # non-boolean output
r.solve(if_function)


@s.solve()
def recursive(a):
    pass


# Will do recursion, extremely cool!!!
args = {'a': Code(code_str='not a')}
out = Output(recursive, args)
r = Rules(a=False, output=0, default=out)
r.solve(recursive)