Exemplo n.º 1
0
 def test_get_ground_atoms(self):
     target_pred = u.str2pred('q/2')
     preds_ext = [u.str2pred('p/0')]
     preds_aux = [u.str2pred('t/1')]
     language_model = LanguageModel(target_pred, preds_ext,
                                    [Constant(x) for x in ['a', 'b', 'c']])
     program_template = ProgramTemplate(preds_aux, {}, 0)
     f = u.str2ground_atom
     expected_ground_atoms = [
         f('p()'),
         f('t(a)'),
         f('t(b)'),
         f('t(c)'),
         f('q(a,a)'),
         f('q(a,b)'),
         f('q(a,c)'),
         f('q(b,a)'),
         f('q(b,b)'),
         f('q(b,c)'),
         f('q(c,a)'),
         f('q(c,b)'),
         f('q(c,c)')
     ]
     self.assertEqual(l.get_ground_atoms(language_model, program_template),
                      expected_ground_atoms)
Exemplo n.º 2
0
 def test_GroundAtoms_all_ground_atom_generator(self):
     target_pred = u.str2pred('r/2')
     preds_ext = [u.str2pred('p/2'), u.str2pred('q/2')]
     preds_aux = []
     language_model = LanguageModel(
         target_pred, preds_ext,
         [Constant('a'), Constant('b')])
     program_template = ProgramTemplate(preds_aux, {}, 0)
     ground_atoms = GroundAtoms(language_model, program_template)
     f = u.str2ground_atom
     expected_ground_atoms = [
         f('p(a,a)'),
         f('p(a,b)'),
         f('p(b,a)'),
         f('p(b,b)'),
         f('q(a,a)'),
         f('q(a,b)'),
         f('q(b,a)'),
         f('q(b,b)'),
         f('r(a,a)'),
         f('r(a,b)'),
         f('r(b,a)'),
         f('r(b,b)')
     ]
     actual_ground_atoms = list(ground_atoms.all_ground_atom_generator())
     self.assertEqual(actual_ground_atoms, expected_ground_atoms)
Exemplo n.º 3
0
    def test_fc(self):
        target_pred = u.str2pred('r/2')
        preds_ext = [u.str2pred('p/2'), u.str2pred('q/2')]
        preds_aux = []
        language_model = LanguageModel(
            target_pred, preds_ext,
            [Constant('a'), Constant('b')])
        rules = {target_pred: (RuleTemplate(1, True), RuleTemplate(1, False))}
        program_template = ProgramTemplate(preds_aux, rules, 0)
        ground_atoms = GroundAtoms(language_model, program_template)

        a = tf.convert_to_tensor(
            np.array([0, 1, 0.9, 0, 0, 0.1, 0, 0.2, 0.8, 0, 0, 0, 0]))
        clause = c.Clause.from_str('r(X,Y)<-p(X,Z), q(Z,Y)')
        tau = RuleTemplate(1, False)
        expected_a_apostrophe = np.array(
            [0, 0, 0, 0, 0, 0, 0, 0, 0, 0.18, 0.72, 0, 0])
        with tf.device('/CPU:0'):
            xc = i.make_xc(clause, ground_atoms)
            xc_tensor = i.make_xc_tensor(xc, language_model.constants, tau,
                                         ground_atoms)
            a_apostrophe = i.fc(a, xc_tensor)
        try:
            np.testing.assert_array_almost_equal(a_apostrophe,
                                                 expected_a_apostrophe)
            self.assertTrue(True)
        except AssertionError:
            self.assertTrue(False)
Exemplo n.º 4
0
def setup_even():
    print('Setting up "Even" problem...')
    # Language model
    target_pred = str2pred('even/1')
    zero_pred = str2pred('zero/1')
    succ_pred = str2pred('succ/2')
    preds_ext = [zero_pred, succ_pred]
    constants = [Constant(str(i)) for i in range(11)]
    language_model = LanguageModel(target_pred, preds_ext, constants)

    # Program template
    aux_pred = str2pred('pred/2')
    aux_preds = [aux_pred]
    rules = {
        target_pred: (RuleTemplate(0, False), RuleTemplate(1, True)),
        aux_pred: (RuleTemplate(1, False), None)
    }
    forward_chaining_steps = 6
    program_template = ProgramTemplate(aux_preds, rules,
                                       forward_chaining_steps)

    # ILP problem
    ground_zero = str2ground_atom('zero(0)')
    background = [ground_zero] + [
        str2ground_atom(f'succ({i},{i + 1})') for i in range(10)
    ]
    positive = [str2ground_atom(f'even({i})') for i in range(0, 11, 2)]
    negative = [str2ground_atom(f'even({i})') for i in range(1, 10, 2)]
    ilp_problem = ILP('even', language_model, background, positive, negative)
    return ilp_problem, program_template
Exemplo n.º 5
0
 def test_GroundAtoms_ground_atom_generator(self):
     target_pred = u.str2pred('r/2')
     preds_ext = [u.str2pred('p/2'), u.str2pred('q/2')]
     preds_aux = []
     language_model = LanguageModel(
         target_pred, preds_ext,
         [Constant('a'), Constant('b')])
     program_template = ProgramTemplate(preds_aux, {}, 0)
     ground_atoms = GroundAtoms(language_model, program_template)
     maybe_ground_atom = MaybeGroundAtom.from_atom(
         Atom(target_pred, (Variable('C'), Variable('C'))))
     actual_ground_atoms = list(
         list(zip(
             *(ground_atoms.ground_atom_generator(maybe_ground_atom))))[0])
     f = u.str2ground_atom
     expected_ground_atoms = [f('r(a,a)'), f('r(b,b)')]
     self.assertEqual(actual_ground_atoms, expected_ground_atoms)
Exemplo n.º 6
0
    def test_make_xc(self):
        target_pred = u.str2pred('r/2')
        preds_ext = [u.str2pred('p/2'), u.str2pred('q/2')]
        preds_aux = []
        language_model = LanguageModel(
            target_pred, preds_ext,
            [Constant('a'), Constant('b')])
        program_template = ProgramTemplate(preds_aux, {}, 0)
        ground_atoms = GroundAtoms(language_model, program_template)

        clause = c.Clause.from_str('r(X,Y)<-p(X,Z), q(Z,Y)')
        f = u.str2ground_atom
        expected = [(f('r(a,a)'), [(1, 5), (2, 7)]),
                    (f('r(a,b)'), [(1, 6), (2, 8)]),
                    (f('r(b,a)'), [(3, 5), (4, 7)]),
                    (f('r(b,b)'), [(3, 6), (4, 8)])]
        self.assertEqual(i.make_xc(clause, ground_atoms), expected)
Exemplo n.º 7
0
def setup_predecessor():
    print('Setting up "Predecessor" problem...')

    # Language model
    target_pred = str2pred('predecessor/2')
    zero_pred = str2pred('zero/1')
    succ_pred = str2pred('succ/2')
    preds_ext = [zero_pred, succ_pred]
    constants = [Constant(str(i)) for i in range(10)]
    language_model = LanguageModel(target_pred, preds_ext, constants)

    # Program template
    preds_aux = []
    rules = {target_pred: (RuleTemplate(0, False), None)}
    forward_chaining_steps = 1
    program_template = ProgramTemplate(preds_aux, rules,
                                       forward_chaining_steps)

    # ILP problem
    ground_zero = str2ground_atom('zero(0)')
    background_axioms = [ground_zero] + [
        str2ground_atom(f'succ({i},{i + 1})') for i in range(9)
    ]
    positive_examples = [
        str2ground_atom(f'predecessor({i + 1},{i})') for i in range(9)
    ]

    ground_atoms = GroundAtoms(language_model, program_template)
    negative_examples = []
    for ground_atom, _ in ground_atoms.ground_atom_generator(
            MaybeGroundAtom.from_pred(target_pred)):
        if ground_atom not in positive_examples:
            negative_examples.append(ground_atom)

    ilp_problem = ILP('predecessor', language_model, background_axioms,
                      positive_examples, negative_examples)
    return ilp_problem, program_template
Exemplo n.º 8
0
 def test_make_xc_tensor(self):
     f = u.str2ground_atom
     xc = [(f('p(a,a)'), []), (f('p(a,b)'), []), (f('p(b,a)'), []),
           (f('p(b,b)'), []), (f('q(a,a)'), []), (f('q(a,b)'), []),
           (f('q(b,a)'), []), (f('q(b,b)'), []),
           (f('r(a,a)'), [(1, 5), (2, 7)]), (f('r(a,b)'), [(1, 6), (2, 8)]),
           (f('r(b,a)'), [(3, 5), (4, 7)]), (f('r(b,b)'), [(3, 6), (4, 8)])]
     constants = [Constant('a'), Constant('b')]
     tau = RuleTemplate(1, False)
     target_pred = u.str2pred('r/2')
     preds_ext = [u.str2pred('p/2'), u.str2pred('q/2')]
     preds_aux = []
     language_model = LanguageModel(target_pred, preds_ext, constants)
     program_template = ProgramTemplate(preds_aux, {}, 0)
     ground_atoms = GroundAtoms(language_model, program_template)
     expected_tensor = np.array([[(0, 0), (0, 0)], [(0, 0), (0, 0)],
                                 [(0, 0), (0, 0)], [(0, 0), (0, 0)],
                                 [(0, 0), (0, 0)], [(0, 0), (0, 0)],
                                 [(0, 0), (0, 0)], [(0, 0), (0, 0)],
                                 [(0, 0), (0, 0)], [(1, 5), (2, 7)],
                                 [(1, 6), (2, 8)], [(3, 5), (4, 7)],
                                 [(3, 6), (4, 8)]])
     result = i.make_xc_tensor(xc, constants, tau, ground_atoms)
     self.assertEqual(result.numpy().tolist(), expected_tensor.tolist())