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"
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)
def test2(): kb = PDKB(1, [1], map(Literal, ['p', 'q'])) l1 = neg(Literal('p')) l2 = Belief(1, Literal('p')) l3 = Belief(1, neg(Literal('q'))) kb.add_rml(l1) kb.add_rml(l2) kb.add_rml(l3) test_kripke(kb, [l1, l2, l3])
def test5(): kb = PDKB(2, [1,2], map(Literal, ['p', 'q'])) rmls = [ Literal('p'), Belief(1, Literal('q')), Belief(1, Belief(2, neg(Literal('q')))) ] for rml in rmls: kb.add_rml(rml) test_kripke(kb, rmls)
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())
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)
def query_t_bound(self, rml, depth): if self.query(rml): return True elif depth == 0: return False else: return any( self.query_t_bound(Belief(a, rml), depth - 1) for a in self.agents)
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)]
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)
def test3(): kb = PDKB(1, [1], map(Literal, ['p', 'q'])) l1 = Literal('p') l2 = Belief(1, Literal('q')) kb.add_rml(l1) kb.add_rml(l2) test_kripke(kb, [l1, l2])
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)))
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 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"
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]
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]
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)))
def kt_lattice_children(rml): assert isinstance(rml, RML) to_ret = set() if isinstance(rml, Belief): to_ret = set([ Belief(rml.agent, newrml) for newrml in kt_lattice_children(rml.rml) ]) to_ret.add(rml.rml) elif isinstance(rml, Possible): to_ret = set([ Possible(rml.agent, newrml) for newrml in kt_lattice_children(rml.rml) ]) return to_ret
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)
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
def random_rml(depth, agents, fluents): b = random.choice(fluents) d = random.randint(0, depth) def coin(): return random.choice([True, False]) if coin(): b = neg(b) for i in range(d): if b.is_lit(): options = agents else: options = list(set(agents) - set([b.agent])) b = Belief(random.choice(options), b) if coin(): b = neg(b) return b
def test_kripke(kb, rmls): print print kb print "\nClosing...\n" kb.logically_close() print kb print "Consistent: %s" % str(kb.is_consistent()) print "\nGenerating compressed kripke..." M = kb.generate_kripke(True) print print "Generating dot export..." M.generate_dot("graph.dot", True) lines = read_file("graph.dot") lines = [lines[0], " rankdir=LR;"] + lines[1:] write_file("graph.dot", lines) os.system("dot -Tpng graph.dot > graph.png") print "\nGenerating full kripke..." M = kb.generate_kripke(False) print print M.get_stats() print for rml in rmls: print "Assessing %s: %s" % (str(rml), M.assess_rml(Belief(0, rml))) print all_match = all([M.assess_rml(rml) for rml in kb.perspectival_view()]) print "All rmls match: %s" % str(all_match) if not all_match: print "Non-matching:" for rml in kb.construct_closed_world(): if not M.assess_rml(rml): print " %s" % str(rml) print
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
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
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)
condition, spacing, self.eff.pddl()) else: if EPDDL: return reason + spacing + "<{%s}\n%s {(%s)}>" % ( condition, spacing, self.eff.pddl()) else: return reason + spacing + "(when (and %s)\n%s (%s))" % ( condition, spacing, self.eff.pddl()) if __name__ == '__main__': a = Action('foo', 2, [1, 2], list(map(Literal, [ 'p', 'q', ])), True) a.add_pre(Belief(1, Literal('p'))) a.add_pre(Belief(2, Literal('p'))) a.new_nondet_effect() p1 = PDKB(2, [1, 2], list(map(Literal, [ 'p', 'q', ]))) p2 = PDKB(2, [1, 2], list(map(Literal, [ 'p', 'q', ]))) p3 = PDKB(2, [1, 2], list(map(Literal, [ 'p', 'q', ]))) p4 = PDKB(2, [1, 2], list(map(Literal, [
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 --"
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"
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 --"
def commonly_known_effect(condeff, agents, depth, props, akprops, posneg, schema, negated=False): if (schema is False) or (condeff.depth >= depth): return [] condeffs = [] if schema is True: schema = [[], []] if condeff.ma_cond: schema[0].extend(condeff.ma_cond[0]) schema[1].extend(condeff.ma_cond[1]) for ag in agents: # We can't remove belief we don't have. # E.g., (-) p -> B1 q and (-) p -> !B1 q together state that # when p holds, we're not sure if 1 believes q or not. # When introspecting for agent 1, it doesn't make sense # to try and consider (+) B1 p -> !B1 !B1 q as that would # make (+) B1 p -> B1 q, which when projected would be # (+) p -> q. Definitely an undesired result. #if ag == condeff.eff.agent and isinstance(condeff.eff, Not) and negated: if ag == condeff.eff.agent and negated: #print "Warning: Ignoring the commonly known extension for the following conditional effect:\n%s" % str(condeff) continue condp = PDKB(depth, agents, props) condn = PDKB(depth, agents, props) for rml in condeff.condp: if rml.is_lit() and rml.is_ak(akprops): condp.add_rml(rml) else: condp.add_rml(Belief(ag, rml).merge_modalities()) for rml in condeff.condn: if rml.is_lit() and rml.is_ak(akprops): condp.add_rml(neg(rml)) else: condp.add_rml(neg(Belief(ag, rml)).merge_modalities()) for rmlscheme in schema[0]: rml = parse_rml(ag.join(rmlscheme.split('?'))) if rml.is_lit() and rml.is_ak(akprops): condp.add_rml(rml) else: condp.add_rml(Belief(ag, rml).merge_modalities()) for rmlscheme in schema[1]: rml = parse_rml(ag.join(rmlscheme.split('?'))) if rml.is_lit() and rml.is_ak(akprops): condp.add_rml(rml) else: condp.add_rml(neg(Belief(ag, rml)).merge_modalities()) if condeff.eff.is_ak(akprops): new_eff = condeff.eff else: new_eff = Belief(ag, condeff.eff).merge_modalities() if negated: new_eff = neg(new_eff) # Only add the new conditional effect if it is different (this may not # be the case if there was some introspection removed) new_cond = CondEff(condp, condn, new_eff, condeff.ma_cond, ('commonly_known', posneg, condeff)) if new_cond != condeff: condeffs.append((True, new_cond)) return condeffs
def project(rmls, ag): belief_rmls = [x for x in rmls if isinstance(x, Belief)] return set([rml.rml for rml in filter_agent(belief_rmls, ag)]) CLOSURE = dict([(KD, [kd_closure]), (KT, [kt_closure, kd_closure]), (KD45, [kd_closure]), (S5, [kt_closure, kd_closure])]) if __name__ == '__main__': print("\n----\n") l1 = Literal('p') l2 = Belief(2, Belief(1, l1)) l3 = Belief(2, neg(Belief(1, neg(l1)))) l4 = neg(l3) print("%s -> %s" % (str(l2), str(list(map(str, kd_closure(l2)))))) print("%s -> %s" % (str(l3), str(list(map(str, kd_closure(l3)))))) print("\n----\n") kb = PDKB(2, [1, 2], list(map(Literal, ['p', 'q']))) kb.add_rml(l2) print(kb) kb.logically_close() print(kb) print("\n----\n")