Exemple #1
0
    def test_board_funcs(self):
        question = Parser.parse("(== (color 1-1) Blue)")
        executor = Executor(question)
        self.assertTrue(executor.execute(self.hypothesis1))
        self.assertFalse(executor.execute(self.empty_hypothesis))

        question = Parser.parse("(== (orient Red) (orient Blue))")
        executor = Executor(question)
        self.assertFalse(executor.execute(self.hypothesis1))
        self.assertTrue(executor.execute(self.hypothesis2))

        question = Parser.parse("(touch Blue Red)")
        executor = Executor(question)
        self.assertTrue(executor.execute(self.hypothesis1))
        self.assertTrue(executor.execute(self.hypothesis2))
        self.assertFalse(executor.execute(self.hypothesis3))

        question = Parser.parse("(== (size Blue) 3)")
        executor = Executor(question)
        self.assertTrue(executor.execute(self.hypothesis1))
        self.assertFalse(executor.execute(self.hypothesis3))

        question = Parser.parse("(bottomright (coloredTiles Red))")
        executor = Executor(question)
        self.assertEqual(executor.execute(self.hypothesis1), (0, 2))
        self.assertEqual(executor.execute(self.hypothesis3), (2, 2))
Exemple #2
0
    def test_parse_error_basic(self):
        with self.assertRaises(ProgramSyntaxError) as cm:
            question = Parser.parse("(== (color 1-1) Blu)")
        exception = cm.exception
        self.assertEqual(exception.error_msg, "Unrecognized token")

        with self.assertRaises(ProgramSyntaxError) as cm1:
            question = Parser.parse("(== (color 1-1 2-2) Blue)")
        exception = cm1.exception
        self.assertEqual(exception.error_msg,
                         "Operand number mismatch. 1 expected, found 2")

        with self.assertRaises(ProgramSyntaxError) as cm2:
            question = Parser.parse("(== (color Red) Blue)")
        exception = cm2.exception
        self.assertEqual(
            exception.error_msg, "Parameter type mismatch. "
            "Expected DataType.LOCATION for parameter 1, get DataType.COLOR")

        with self.assertRaises(ProgramSyntaxError) as cm3:
            question = Parser.parse("(setSize AllTiles)")
        exception = cm3.exception
        self.assertEqual(
            exception.error_msg, "Parameter type mismatch. "
            "Expected DataType.SET_L for parameter 1, get DataType.SET_LITERAL_L"
        )
Exemple #3
0
    def test_lambda_x(self):
        
        question = Parser.parse("(any (map (lambda x0 (== (orient x0) H)) (set AllColors)))")
        executor = Executor(question)
        self.assertTrue(executor.execute(self.hypothesis4))
        self.assertFalse(executor.execute(self.hypothesis5))

        # How many ships' sizes are the same as the number of vertical ships?
        question = Parser.parse("(++ (map (lambda x0 (== (size x0) (++ (map (lambda x1 (== (orient x1) V)) (set AllColors))))) (set AllColors)))")
        executor = Executor(question)
        self.assertEqual(executor.execute(self.hypothesis4), 2)
        self.assertEqual(executor.execute(self.hypothesis5), 1)
Exemple #4
0
    def test_runtime_error(self):
        with self.assertRaises(RuntimeError) as cm:
            question = Parser.parse("(size Purple)")
            executor = Executor(question)
            executor.execute(self.hypothesis1)

        with self.assertRaises(RuntimeError) as cm:
            question = Parser.parse("(touch Water (color 4-5))")
            executor = Executor(question)
            executor.execute(self.hypothesis4)

        with self.assertRaises(RuntimeError) as cm:
            question = Parser.parse("(color 4-5)")
            executor = Executor(question)
            executor.execute(self.hypothesis1)
Exemple #5
0
    def test_set_operations(self):
        question = Parser.parse("(setSize (setDifference (set 1-1 1-2 1-3) (set 1-2 2-1)))")
        executor = Executor(question)
        self.assertEqual(executor.execute(self.empty_hypothesis), 2)

        question = Parser.parse("(setSize (union (set 1-1 1-2 1-3) (set 1-2 2-1)))")
        executor = Executor(question)
        self.assertEqual(executor.execute(self.empty_hypothesis), 4)

        question = Parser.parse("(setSize (intersection (set 1-1 1-2 1-3) (set 1-2 1-2 2-1)))")
        executor = Executor(question)
        self.assertEqual(executor.execute(self.empty_hypothesis), 1)

        question = Parser.parse("(setSize (union (set 1-1 1-3 1-2) (set 2-1 1-2 1-3)))")
        executor = Executor(question)
        self.assertEqual(executor.execute(self.empty_hypothesis), 4)
Exemple #6
0
    def test_basic(self):
        question1 = Parser.parse("(> 3 1)")
        executor1 = Executor(question1)
        self.assertTrue(executor1.execute(self.empty_hypothesis))

        question2 = Parser.parse("(== Water Water)")
        executor2 = Executor(question2)
        self.assertTrue(executor2.execute(self.empty_hypothesis))

        question3 = Parser.parse("(+ 2 5)")
        executor3 = Executor(question3)
        self.assertEqual(executor3.execute(self.empty_hypothesis), 7)

        question4 = Parser.parse("(and (not (< 4 9)) (== (+ 1 3) 4))")
        executor4 = Executor(question4)
        self.assertFalse(executor4.execute(self.empty_hypothesis))
Exemple #7
0
    def test_primitives(self):
        question1 = Parser.parse("Red")
        executor1 = Executor(question1)
        self.assertEqual(executor1.execute(self.empty_hypothesis), 2)

        question2 = Parser.parse("2-1")
        executor2 = Executor(question2)
        self.assertEqual(executor2.execute(self.empty_hypothesis), (1, 0))

        question3 = Parser.parse("H")
        executor3 = Executor(question3)
        self.assertEqual(executor3.execute(self.empty_hypothesis), "H")

        question4 = Parser.parse("6")
        executor4 = Executor(question4)
        self.assertEqual(executor4.execute(self.empty_hypothesis), 6)
Exemple #8
0
def run_multiple(hs, time=100):
    for _ in range(time):
        belief = eig.Bayes(hs)
        context = eig.Context(hs, belief)
        context.observe(OBS)
        question = Parser.parse(PROG)
        executor = Executor(question)
        eig_s = eig.compute_eig(executor, context)
Exemple #9
0
 def test_eig_full_api(self):
     belief = Bayes(self.hs1)
     context = Context(self.hs1, belief)
     context.observe(self.observation1)
     
     question = Parser.parse("Red")
     executor = Executor(question)
     eig = compute_eig(executor, context)
     # no IG for asking this question
     self.assertAlmostEqual(eig, 0)
     
     question = Parser.parse("(any (map (lambda y0 (== (color y0) Red)) (set 1-1 1-2 1-3)))")
     executor = Executor(question)
     eig = compute_eig(executor, context)
     # 4 valid hypothesis, 2 answers are True, 2 are False for this question
     # entropy([0.5, 0.5]) = 1
     self.assertAlmostEqual(eig, 1)
     
     question = Parser.parse("(size Red)")
     executor = Executor(question)
     eig = compute_eig(executor, context)
     # 4 valid hypothesis, 1 answers is 3, 3 are 2 for this question
     # entropy([0.25, 0.75]) = 2 - 0.75*log(3) = 0.81127812
     self.assertAlmostEqual(eig, 0.81127812)
     
     tmp_observation = self.observation1.copy()
     tmp_observation[1, 2] = 2
     context.observe(tmp_observation)
     eig = compute_eig(executor, context)
     # 1 valid hypothesis, answer is False
     # no IG for asking this question
     self.assertAlmostEqual(eig, 0)
     
     ## Simple example 2x2 grid with a single ship of lenth 2 => 4 hypotheses
     belief = Bayes(self.hs2)
     context = Context(self.hs2, belief)
     question = Parser.parse("(orient Blue)")
     executor = Executor(question)
     eig = compute_eig(executor, context)
     self.assertAlmostEqual(eig, 1)
     
     ## Full hypothesis space
     prior = EqualSizesDistribution(ship_labels=[1, 2, 3])
     belief = Bayes(self.hs3, prior)
     context = Context(self.hs3, belief)
     context.observe(self.observation2)
     question = Parser.parse("(bottomright (coloredTiles Purple))")
     executor = Executor(question)
     eig = compute_eig(executor, context)
     self.assertAlmostEqual(eig, 2.4275116)  # should pass once the prior is uniform over ship sizes
     
     question = Parser.parse("(size Purple)")
     executor = Executor(question)
     eig = compute_eig(executor, context)
     self.assertAlmostEqual(eig, 1.5653360)  # should pass once the prior is uniform over ship sizes
     
     question = Parser.parse("(color 3-1)")
     executor = Executor(question)
     eig = compute_eig(executor, context)
     self.assertAlmostEqual(eig, 0.9989968)
Exemple #10
0
    def test_parse_error_lambda(self):
        with self.assertRaises(ProgramSyntaxError) as cm:
            question = Parser.parse(
                "(map (lambda x0 (+ 1 2)) (set AllColors))")
        exception = cm.exception
        self.assertEqual(exception.error_msg,
                         "Top level type cannot be DataType.SET_N")

        with self.assertRaises(ProgramSyntaxError) as cm:
            question = Parser.parse(
                "(any (map (lambda x1 (== (color x2) Red)) (set AllColors)))")
        exception = cm.exception
        self.assertEqual(exception.error_msg,
                         "Lambda variable x2 should not exist here")

        with self.assertRaises(ProgramSyntaxError) as cm:
            question = Parser.parse(
                "(++ (map (lambda x1 (== (size x1) (++ (map (lambda x1 (== (orient x1) V)) (set AllColors))))) (set AllColors)))"
            )
        exception = cm.exception
        self.assertEqual(exception.error_msg,
                         "Lambda variable x1 has already been defined")

        with self.assertRaises(ProgramSyntaxError) as cm:
            question = Parser.parse(
                "(any (map (lambda x2 (== (orient x2) H)) (set AllTiles)))")
        exception = cm.exception
        self.assertEqual(
            exception.error_msg, "Parameter type mismatch. "
            "Expected one of\n  (DataType.LAMBDA_FXB, DataType.SET_S),\n  (DataType.LAMBDA_FYB, DataType.SET_L),\n"
            "  (DataType.LAMBDA_FXL, DataType.SET_S),\n  (DataType.LAMBDA_FXN, DataType.SET_S),\nget (DataType.LAMBDA_FXB, DataType.SET_L)"
        )

        with self.assertRaises(ProgramSyntaxError) as cm:
            question = Parser.parse(
                "(++ (lambda (lambda x0 (size x0)) (set AllColors)))")
        exception = cm.exception
        self.assertEqual(
            exception.error_msg, "Parameter type mismatch. "
            "The first child of lambda operator should be lambda\n"
            "variable (x0, x1, y2, etc.), get lambda_op")
Exemple #11
0
 def test_parse_basic(self):
     question = Parser.parse("(== (color 1-1) Blue)")
     reference = {
         'type':
         'equal',
         'children': [{
             'type': 'color_fn',
             'children': [{
                 'type': 'location',
                 'value': (0, 0)
             }]
         }, {
             'type': 'color',
             'value': 1
         }]
     }
     self.assertEqual(question.to_dict(), reference)
Exemple #12
0
 def test_parse_lambda(self):
     question = Parser.parse(
         "(any (map (lambda x0 (== (orient x0) H)) (set AllColors)))")
     reference = {
         'type':
         'any_op',
         'children': [{
             'type':
             'map_op',
             'children': [{
                 'type':
                 'lambda_op',
                 'children': [{
                     'type': 'lambda_x',
                     'value': 'x0'
                 }, {
                     'type':
                     'equal',
                     'children': [{
                         'type':
                         'orient_fn',
                         'children': [{
                             'type': 'lambda_x',
                             'value': 'x0'
                         }]
                     }, {
                         'type': 'orientation',
                         'value': 'H'
                     }]
                 }]
             }, {
                 'type':
                 'set_op',
                 'children': [{
                     'type': 'set_allcolors',
                     'value': 'AllColors'
                 }]
             }]
         }]
     }
     self.assertEqual(question.to_dict(), reference)
Exemple #13
0
def run_calculate(context):
    question = Parser.parse(PROG)
    executor = Executor(question)
    eig_s = eig.compute_eig(executor, context)
    return eig_s
Exemple #14
0
 def test_lambda_y(self):
     question = Parser.parse("(any (map (lambda y0 (and (== (color y0) Red) (== (rowL y0) 2))) (set AllTiles)))")
     executor = Executor(question)
     self.assertTrue(executor.execute(self.hypothesis4))
     self.assertFalse(executor.execute(self.hypothesis5))