Ejemplo n.º 1
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"
Ejemplo n.º 2
0
def random_pdkb(depth, numAg, fluents, numRMLs, output=True):

    agents = list(range(1, numAg + 1))
    kb = PDKB(depth, agents, fluents)
    count = 0
    new_rmls = set()

    while count < numRMLs:
        rml = random_rml(depth, agents, fluents)
        if rml not in kb.rmls and neg(rml) not in kb.rmls:
            # Take advantage of the fact that we know the closure
            #  is just the union of the closure of each RML
            test_rmls = list(kd_closure(rml)) + [rml]
            cons = True
            for trml in test_rmls:
                if neg(trml) in kb.rmls:
                    cons = False

            if cons:
                if output:
                    print "Added %s" % rml
                for trml in test_rmls:
                    kb.add_rml(trml)
                new_rmls.add(rml)
                count += 1

    kb.rmls = new_rmls
    return kb
Ejemplo n.º 3
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"
Ejemplo n.º 4
0
def close_literal(condeff, act):
    to_ret = []
    to_close = [condeff.eff]
    seen = set()
    while to_close:
        rml = to_close.pop(0)
        if rml not in seen:
            seen.add(rml)
            to_ret.append(
                (True,
                 CondEff(condeff.condp.copy(), condeff.condn.copy(), rml,
                         condeff.ma_cond, ('closure', 'pos', condeff))))
            to_close.extend(kd_closure(rml))
    return to_ret
Ejemplo n.º 5
0
def unclose_literal(condeff, act):
    # unclose just follows the above process but uses the contrapositive
    to_ret = []
    to_close = [neg(condeff.eff)]
    seen = set()
    while to_close:
        rml = to_close.pop(0)
        if rml not in seen:
            seen.add(rml)
            to_ret.append((False,
                           CondEff(condeff.condp.copy(), condeff.condn.copy(),
                                   neg(rml), condeff.ma_cond,
                                   ('unclosure', 'neg', condeff))))
            to_close.extend(kd_closure(rml))
    return to_ret