Exemple #1
0
def test13():
    print "-- START TEST 13 --"

    AxiomSystem.SYSTEM = KD
    l1 = Belief(1, Belief(2, Literal('p')))
    l2 = Belief(1, Possible(2, Literal('p')))
    l3 = Possible(2, Literal('p'))
    l4 = Belief(1, Literal('p'))
    assert l1.kd_entails_rml(l2)
    assert not l1.kd_entails_rml(l3)
    assert not l1.kd_entails_rml(l4)

    assert l1.kt_entails_rml(l2)
    assert l1.kt_entails_rml(l3)
    assert l1.kt_entails_rml(l4)

    l5 = Belief(1, Possible(3, Literal('p')))
    assert not l1.kt_entails_rml(l5)

    l6 = Possible(1, Belief(2, Literal('p')))
    assert not l6.kt_entails_rml(l1)

    l7 = Possible(1, Literal('p'))
    assert l6.kt_entails_rml(l7)

    AxiomSystem.SYSTEM = KT
    l1 = Belief(2, Belief(1, Possible(2, neg(Literal('r')))))
    l2 = Possible(2, neg(Literal('r')))
    assert l1.kt_entails_rml(l2)

    print "-- END TEST 13 --"
Exemple #2
0
def test_expand(kb1, kb2):

    l1 = Literal('p')
    l2 = Belief(2, Belief(1, l1))
    l3 = Belief(2, neg(Belief(1, neg(l1))))
    l4 = neg(l3)

    kb1.expand(set([l1, l2, l3, l4]))
    kb2.add_rml(l1)
    kb2.add_rml(l2)
    kb2.add_rml(l3)
    kb2.add_rml(l4)

    assert kb1.size() == kb2.size()

    l5 = Possible(2, Possible(1, l1))
    assert kb1.query(l5)
    assert kb2.query(l5)

    l6 = Possible(1, l1)
    assert kb1.query(l6) == (AxiomSystem.SYSTEM == KT), "KB = %s\nSystem = %s" % (kb1, AxiomSystem.SYSTEM)

    kb2.remove_rml(l1)
    kb2.remove_rml(l2)
    kb2.remove_rml(l3)
    kb2.remove_rml(l4)
    kb1.contract(set([l1, l2, l3, l4]))

    assert not kb1.query(l1)
    assert not kb2.query(l1)
Exemple #3
0
def test12():
    print "-- START TEST 12 --"

    #kb1 = PDKB(3, [1,2,3], map(Literal, ['p', 'q', 'r']))
    kb2 = IndexedPDKB(3, [1,2,3], map(Literal, ['p', 'q', 'r']))
    kb3 = IndexedPDKB(3, [1,2,3], map(Literal, ['p', 'q', 'r']))

    l1 = Belief(1, Belief(2, Literal('p')))
    l2 = Possible(1, Belief(2, Literal('p')))
    kb2.expand(set([l1]))
    kb3.expand(set([l2]))

    assert kb2.query_t(Literal('p'))
    assert not kb3.query_t(Literal('p'))

    l1_closed = kt_closure(l1)
    assert l1 in l1_closed
    assert Belief(1, Literal('p')) in l1_closed
    assert Belief(2, Literal('p')) in l1_closed
    assert Literal('p') in l1_closed

    l2_closed = kt_closure(l2)
    fully_closed = []
    for rml in l2_closed:
        fully_closed += kd_closure(rml)
    assert l2 in fully_closed
    assert Possible(1, Possible(2, Literal('p'))) in fully_closed
    assert Possible(1, Literal('p')) in fully_closed

    print "-- END TEST 12 --\n"
Exemple #4
0
def test6():
    print "-- START TEST 6 --"

    for axiom_system in [KD, KT]:
        AxiomSystem.SYSTEM = axiom_system
        kb1 = PDKB(1, [1,2], map(Literal, ['p', 'q']))
        kb2 = PDKB(1, [1,2], map(Literal, ['p', 'q']))
        test_expand(kb1, kb2)

        # Test that adding an RML already implied does nothing
        kb1 = IndexedPDKB(1, [1,2], map(Literal, ['p', 'q']))
        kb1.expand(set([Belief(1, Literal('p'))]))
        kb2 = kb1.copy()
        kb1.expand(set([Possible(1, Literal('p'))]))
        assert kb1 == kb2

        # Test that adding an RML that implies an existing RML removes that RML
        kb1 = IndexedPDKB(1, [1,2], map(Literal, ['p', 'q']))
        kb1.expand(set([Possible(1, Literal('p'))]))
        old_size = kb1.size()
        kb1.expand(set([Belief(1, Literal('p'))]))
        assert kb1.size() == old_size, "kb1 = %s, old = %s" % (kb1.size(), old_size)

        l1 = Belief(1, Literal('p'))
        l2 = Belief(2, Literal('p'))
        assert not l1.entails_rml(l2)

    for axiom_system in [KD, KT]:
        AxiomSystem.SYSTEM = axiom_system
        kb1 = IndexedPDKB(1, [1,2], map(Literal, ['p', 'q']))
        kb2 = IndexedPDKB(1, [1,2], map(Literal, ['p', 'q']))
        test_expand(kb1, kb2)       

    print "-- END TEST 6 --\n"
Exemple #5
0
def kd_closure(rml):
    assert isinstance(rml, RML)

    if isinstance(rml, Literal):
        return [rml]
    elif isinstance(rml, Belief):
        return [Possible(rml.agent, newrml) for newrml in kd_closure(rml.rml)] + \
               [Belief(rml.agent, newrml) for newrml in kd_closure(rml.rml)]
    else:
        return [Possible(rml.agent, newrml) for newrml in kd_closure(rml.rml)]
Exemple #6
0
def kd_closure(rml):
    """Closure of the D axiom: Belief(phi) ==> Diamond(phi)"""
    assert isinstance(rml, RML)

    if isinstance(rml, Literal):
        return [rml]
    elif isinstance(rml, Belief):
        return [Possible(rml.agent, newrml) for newrml in kd_closure(rml.rml)] + \
               [Belief(rml.agent, newrml) for newrml in kd_closure(rml.rml)]
    else:
        return [Possible(rml.agent, newrml) for newrml in kd_closure(rml.rml)]
def kd_lattice_children(rml):
    assert isinstance(rml, RML)

    to_ret = set()
    if isinstance(rml, Belief):
        to_ret = set([Possible(rml.agent, rml.rml)])
        to_ret |= set([
            Belief(rml.agent, newrml)
            for newrml in kd_lattice_children(rml.rml)
        ])
    elif isinstance(rml, Possible):
        to_ret |= set([
            Possible(rml.agent, newrml)
            for newrml in kd_lattice_children(rml.rml)
        ])

    return to_ret
Exemple #8
0
def test_remove(kb):

    l1 = Belief(2, Belief(1, Literal('p')))
    l2 = Possible(2, Possible(1, Literal('p')))
    l3 = Possible(1, Belief(2, Literal('q')))
    l4 = Belief(1, Possible(2, Literal('q')))

    kb.expand(set([l1, l2, l3, l4]))

    q1 = Possible(2, Belief(1, Literal('p')))
    q2 = Belief(1, Belief(2, Literal('q')))
    
    assert kb.query(q1)
    assert not kb.query(q2)

    kb.remove(set([q1]))

    assert not kb.query(q1)
    assert not kb.query(l1)
    assert kb.query(l2)

    l5 = Possible(1, Possible(2, Literal('q')))
    kb.remove(set([l2, l5]))

    assert kb.size() == 0, "kb.size() == %s" % str(kb.size())
Exemple #9
0
def test_consistency(pdkb, kb):
    assert isinstance(pdkb, PDKB)

    l1 = Literal('p')
    l2 = Belief(1, Belief(2, l1))
    l3 = Possible(3, Belief(2, Belief(1, neg(l1))))
    l4 = Belief(2, Possible(1, Belief(3, Belief(1, Literal('p')))))

    pdkb.expand(set([l1, l2, l3, l4]))
    kb.expand(set([l1, l2, l3, l4]))

    compare(pdkb, kb)

    l5 = neg(l3)
    pdkb.update(set([l5]))
    kb.update(set([l5]))

    compare(pdkb, kb)
Exemple #10
0
 def parse_rml(s):
     if '!' == s[0]:
         return neg(parse_rml(s[1:]))
     elif 'B' == s[0]:
         return Belief(int(s[1]), parse_rml(s[2:]))
     elif 'P' == s[0]:
         return Possible(int(s[1]), parse_rml(s[2:]))
     else:
         return Literal(s)
Exemple #11
0
    def relevant_output(self):
        newRMLs = set()
        for rml in self.rmls:
            if not isinstance(rml, Possible):
                newRMLs.add(rml)
            elif (Possible(rml.agent, rml.rml.negate()) not in self.rmls) and \
                   (Belief(rml.agent, rml.rml) not in self.rmls):
                newRMLs.add(rml)

        return "  " + "\n  ".join(sorted(map(str, newRMLs)))
Exemple #12
0
def kt_closure(rml):
    assert isinstance(rml, RML)

    if isinstance(rml, Belief):
        kt_subclosure = [newrml for newrml in kt_closure(rml.rml)]
        return kt_subclosure + [
            Belief(rml.agent, newrml) for newrml in kt_subclosure
        ]
    elif isinstance(rml, Possible):
        kt_subclosure = [newrml for newrml in kt_closure(rml.rml)]
        return [Possible(rml.agent, newrml) for newrml in kt_subclosure]
    else:
        return [rml]
Exemple #13
0
def kt_closure(rml):
    """Closure of the T axiom: Belief(phi) ==> phi"""
    assert isinstance(rml, RML)

    if isinstance(rml, Belief):
        kt_subclosure = [newrml for newrml in kt_closure(rml.rml)]
        return kt_subclosure + [
            Belief(rml.agent, newrml) for newrml in kt_subclosure
        ]
    elif isinstance(rml, Possible):
        kt_subclosure = [newrml for newrml in kt_closure(rml.rml)]
        return [Possible(rml.agent, newrml) for newrml in kt_subclosure]
    else:
        return [rml]
Exemple #14
0
    def relevant_output(self):
        """
        Computes the unique information of the KB.
        
        Ignores disjunctive trivial belief like every agent either believes something or they don't.
        """
        newRMLs = set()
        for rml in self.rmls:
            if not isinstance(rml, Possible):
                newRMLs.add(rml)
            elif (Possible(rml.agent, rml.rml.negate()) not in self.rmls) and \
                   (Belief(rml.agent, rml.rml) not in self.rmls):
                newRMLs.add(rml)

        return "  " + "\n  ".join(sorted(map(str, newRMLs)))
Exemple #15
0
def test1():
    print "\n----\n"

    l1 = Literal('p')
    l2 = Belief(2, Belief(1, l1))
    l3 = Belief(2, neg(Belief(1, neg(l1))))
    l4 = neg(l3)

    l5 = neg(Belief(1, neg(Belief(2, l1))))
    l6 = neg(Belief(1, neg(Belief(2, Possible(3, Belief(4, l1))))))

    print "KD closure operation:"
    print "%s -> %s" % (str(l2), str(map(str, kd_closure(l2))))
    print "%s -> %s" % (str(l3), str(map(str, kd_closure(l3))))

    print "\n----\n"

    kb = PDKB(2, [1,2], map(Literal, ['p', 'q']))
    kb.add_rml(l2)
    print "Initial KB:"
    print kb
    print "\nClosing..."
    kb.logically_close()
    print kb

    print "\n----\n"

    kb.add_rml(l4)
    print "Initial Closed KB:"
    kb.logically_close()
    print kb
    print "\nConsistent: %s" % str(kb.is_consistent())

    print "\n----\n"

    kb = PDKB(2, [1,2], map(Literal, ['p', 'q']))
    kb.add_rml(Belief(1, Literal('p')))
    kb.add_rml(Belief(2, Literal('q')))
    kb.add_rml(Belief(1, Belief(2, Literal('q'))))
    kb.add_rml(neg(Belief(1, Literal('q'))))
    print "Initial KB:"
    print kb
    print "\n...projecting on agent 1\n"
    print project(kb.rmls, 1)

    print "\n----\n"
Exemple #16
0
    def all_rmls(self):
        if not self._all_rmls:

            self._all_rmls = set()
            to_add = set(self.props) | set(map(neg, self.props))

            self._all_rmls.update(to_add)

            for i in range(1, self.depth + 1):

                prev_added = to_add.copy()
                to_add = set()

                for ag in self.agents:
                    to_add.update(set([Belief(ag, rml) for rml in prev_added] + \
                                      [Possible(ag, rml) for rml in prev_added]))

                self._all_rmls.update(to_add)

        return self._all_rmls
Exemple #17
0
    def perspectival_view(self, allow_repeat=False):
        def no_repeats(rml):
            if rml.is_lit():
                return True
            elif rml.agent == rml.rml.agent:
                return False
            else:
                return no_repeats(rml.rml)

        if allow_repeat:
            all_rmls = self.all_rmls
        else:
            all_rmls = filter(no_repeats, self.all_rmls)

        CW = set()
        for rml in all_rmls:
            if rml in self.rmls:
                CW.add(Belief(0, rml))
            else:
                CW.add(Possible(0, neg(rml)))
        return CW
Exemple #18
0
    def perspectival_view(self, allow_repeat=False, root_agent=0):
        """Returns the set of RMLs from the perspective of the root agent."""
        def no_repeats(rml):
            if rml.is_lit():
                return True
            elif rml.agent == rml.rml.agent:
                return False
            else:
                return no_repeats(rml.rml)

        if allow_repeat:
            all_rmls = self.all_rmls
        else:
            all_rmls = list(filter(no_repeats, self.all_rmls))

        CW = set()
        for rml in all_rmls:
            if rml in self.rmls:
                CW.add(Belief(root_agent, rml))
            else:
                CW.add(Possible(root_agent, neg(rml)))
        return CW
Exemple #19
0
def test_update(kb):

    l1 = Belief(1, Belief(2, Literal('p')))
    l2 = Belief(1, Belief(2, Literal('q')))

    kb.update(set([l1, l2]))
    assert kb.is_consistent(), str(kb)

    assert kb.query(l1)
    assert kb.query(l2)

    l3 = Possible(1, Possible(2, neg(Literal('p'))))
    l4 = Belief(1, Possible(2, Literal('p')))

    kb.update(set([l3]))
    assert kb.is_consistent(), str(kb)

    assert not kb.query(l1)
    assert kb.query(l2)
    assert kb.query(l3)
    assert kb.query(l4)

    assert kb.is_consistent(), str(kb)

    l5 = Belief(3, neg(Literal('r')))
    kb.update(set([l5]))

    l6 = Belief(3, Literal('r'))
    kb.update(set([l6]))

    assert not kb.query(l5)
    assert kb.query(l6)
    assert kb.query(Possible(3, Literal('r')))

    AxiomSystem.SYSTEM = KT
    kb.reset()
    kb.update(set([Belief(1, Belief(2, Literal('p')))]))
    p1 = Possible(2, Belief(1, neg(Literal('p'))))
    p2 = Possible(3, neg(Literal('p')))
    kb.update(set([p1, p2]))
    q = Literal('p')
    assert kb.query(q) == (AxiomSystem.SYSTEM in [KT, S5])
    assert kb.is_consistent(), str(kb)
Exemple #20
0
def test_remove_kt(kb):

    l1 = Belief(2, Belief(1, Literal('p')))
    l2 = Possible(2, Possible(1, Literal('p')))
    l3 = Possible(1, Belief(2, Literal('q')))
    l4 = Belief(1, Possible(2, Literal('q')))

    kb.expand(set([l1, l2, l3, l4]))

    q1 = Possible(2, Belief(1, Literal('p')))
    q2 = Belief(1, Belief(2, Literal('q')))
    q3 = Possible(2, Literal('p'))

    assert kb.query(q1)
    assert not kb.query(q2)
    assert kb.query(q3)

    kb.remove(set([q3]))

    assert not kb.query(q1)
    assert not kb.query(l1)
    assert not kb.query(q3)
    assert kb.query(l2)
Exemple #21
0
        'q',
    ])))
    p3 = PDKB(2, [1, 2], list(map(Literal, [
        'p',
        'q',
    ])))
    p4 = PDKB(2, [1, 2], list(map(Literal, [
        'p',
        'q',
    ])))
    pempty = PDKB(2, [1, 2], list(map(Literal, [
        'p',
        'q',
    ])))
    p1.add_rml(Belief(1, Literal('p')))
    p1.add_rml(Belief(1, Possible(2, neg(Literal('p')))))
    lit = Belief(2, Literal('q'))
    p2.add_rml(Belief(2, Literal('q')))
    p3.add_rml(Literal('p'))
    p4.add_rml(Literal('q'))

    a.add_pos_effect(p3, pempty, Literal('q'))
    a.add_pos_effect(p1, pempty, lit)
    a.new_nondet_effect()
    a.add_neg_effect(pempty, p1, lit)
    a.add_pos_effect(p1, pempty, lit)

    print(a.pddl())
    print()

    a.expand()
Exemple #22
0
def test14():
    print "-- START TEST 14 --"

    for axiom_system in [KD, KT]:
        AxiomSystem.SYSTEM = axiom_system        

        p = Literal('p')
        q = Literal('q')
        assert p.inconsistent(neg(p))
        assert not p.inconsistent(neg(q))
        assert not p.inconsistent(q)
        
        b1p = Belief(1, p)
        b2p = Belief(2, p)
        b2np = Belief(2, neg(p))
        p1np = Possible(1, neg(p))
        p2np = Possible(2, neg(p))
        p2p = Possible(2, p)
        assert b1p.inconsistent(p1np)
        assert p1np.inconsistent(b1p)
        assert not b1p.inconsistent(b2p)
        assert not b2p.inconsistent(b1p)
        assert b2p.inconsistent(b2np)
        assert b2np.inconsistent(b2p)
        assert (b1p.inconsistent(b2np) == (axiom_system in [KT, S5])), ax_system()
        assert (b2np.inconsistent(b1p) == (axiom_system in [KT, S5])), ax_system()
        assert not b1p.inconsistent(p2np)
        assert not p2np.inconsistent(b1p)
        assert not p1np.inconsistent(p2np) 
        assert not p2np.inconsistent(p1np)
        assert not p2np.inconsistent(p2p)
        
        assert (b1p.inconsistent(neg(p)) == (axiom_system in [KT, S5])), ax_system()      
        assert (neg(p).inconsistent(b1p) == (axiom_system in [KT, S5])), ax_system()
        assert not p1np.inconsistent(p)
        assert not p.inconsistent(p1np)

        b2b1p = Belief(2, b1p)
        b2p1np = Belief(2, p1np)
        assert b2b1p.inconsistent(b2p1np), ax_system()
        assert b2p1np.inconsistent(b2b1p), ax_system()

        assert (b1p.inconsistent(b2p1np) == (axiom_system in [KT, S5])), ax_system()
        assert (b2p1np.inconsistent(b1p) == (axiom_system in [KT, S5])), ax_system()

        assert not b2p.inconsistent(Possible(3, neg(p))), ax_system()
        assert b2p.inconsistent(Possible(2, Belief(1, neg(p)))) == (axiom_system in [KT, S5]), ax_system()

    print "-- END TEST 14 --"
Exemple #23
0
def test11():
    print "-- START TEST 11 --"
    # Test helper functions
    AxiomSystem.SYSTEM = KD
    p = Belief(2, Belief(1, Literal('p')))
    q = Possible(2, Possible(1, Literal('p')))

    sub = sub_lattice(p, q)
    assert len(sub) == 4, "len(%s) == %d" % (str(sub), len(sub))
    assert p in sub
    assert q in sub
    assert not any(r for r in sub if r.entails_rml(p) and r != p)
    assert all(r for r in sub if r.entails_rml(q))
    assert Belief(2, Possible(1, Literal('p'))) in sub
    assert Possible(2, Belief(1, Literal('p'))) in sub

    p = Belief(5, Possible(4, Belief(3, Belief(2, Belief(1, neg(Literal('p')))))))
    q = Possible(5, Possible(4, Belief(3, Possible(2, Possible(1, neg(Literal('p')))))))
    sub = sub_lattice(p, q)
    assert len(sub) == 8, "len(%s) == %d" % (str(sub), len(sub))
    assert p in sub
    assert q in sub
    assert not any(r for r in sub if r.entails_rml(p) and r != p)
    assert all(r for r in sub if r.entails_rml(q))
    assert Possible(5, Possible(4, Belief(3, Possible(2, Belief(1, neg(Literal('p'))))))) in sub
    assert Belief(5, Belief(4, Belief(3, Belief(2, Belief(1, neg(Literal('p'))))))) not in sub

    # Switch to axiom system KT
    AxiomSystem.SYSTEM = KT
    p = Belief(1, Belief(2, Literal('p')))
    q = Possible(2, Literal('p'))
    sub = sub_lattice(p, q)
    assert len(sub) == 4, "len(%s) == %d" % (str(sub), len(sub))
    assert p in sub
    assert q in sub
    assert not any(r for r in sub if r.entails_rml(p) and r != p)
    assert all(r for r in sub if r.entails_rml(q))
    assert Belief(1, Possible(2, Literal('p'))) in sub
    assert Belief(2, Literal('p')) in sub
    
    p = Belief(1, Possible(2, Belief(3, Belief(4, neg(Literal('p'))))))
    q = Possible(2, neg(Literal('p')))
    sub = sub_lattice(p, q)
    assert len(sub) == 8, "len(%s) == %d" % (str(sub), len(sub))
    assert p in sub
    assert q in sub
    assert not any(r for r in sub if r.entails_rml(p) and r != p)
    assert all(r for r in sub if r.entails_rml(q))
    assert Possible(2, Belief(3, Belief(4, neg(Literal('p'))))) in sub
    assert Possible(2, Belief(4, neg(Literal('p')))) in sub
    assert Possible(2, Possible(3, neg(Literal('p')))) not in sub

    print "-- END TEST 11 --\n"