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 test_consistency_random_kb(pdkb, kb): assert isinstance(pdkb, PDKB) NUM_RUNS = 10000 fluents = map(Literal, 'pqrst') agents = list(range(1, 4)) added = [] for i in range(0, NUM_RUNS): print >> sys.stderr, str(i) + " ", kb.reset() pdkb.reset() rmls = [ random_rml(4, agents, fluents), random_rml(3, agents, fluents), random_rml(2, agents, fluents), ] # Only add the RMLs if they are consistent check_consistency = PDKB(4, agents, fluents) check_consistency.update(set(rmls)) if check_consistency.is_consistent(): assert pdkb.is_consistent() kb_copy = kb.copy() pdkb_copy = pdkb.copy() pdkb.update(set(rmls)) kb.update(set(rmls)) rml = compare(pdkb, kb) if isinstance(rml, RML): kb_copy.restrict(rml) pdkb_copy.restrict(rml) kb.restrict(rml) pdkb.restrict(rml) print "before = " + str(kb_copy) print "before = " + str(pdkb_copy) print "\t add " + str(rmls) print "\t after = " + str(kb) print "\t after = " + str(pdkb) sys.exit()
class Problem(object): def __init__(self): self.domain = None def parse_parts(self, prob): self.parse_projection(prob) self.parse_init(prob) self.parse_goal(prob) def parse_projection(self, prob): assert 'projection' in dir(prob) self.agent_projection = list(reversed(prob.projection)) def parse_init(self, prob): self.init = PDKB(self.domain.depth, self.domain.agents, self.domain.props) assume_closed = False if prob.init_type == 'complete': assume_closed = True assert isinstance(prob.init, pdkb.pddl.formula.And) for prim in prob.init.args: init_rmls = [prim2rml(prim, False)] for t in prob.types: going = True while going: new_init_rmls = [] going = False for old_init_rml in init_rmls: if "$%s$" % t not in old_init_rml: new_init_rmls.append(old_init_rml) else: going = True for v in prob.type_to_obj[t]: # Only do a single split so we can reuse the same variable type lside = old_init_rml[:old_init_rml. index("$%s$" % t)] rside = old_init_rml[old_init_rml. index("$%s$" % t) + len("$%s$" % t):] new_init_rmls.append(lside + v + rside) init_rmls = new_init_rmls for rml_line in init_rmls: self.init.add_rml(parse_rml(rml_line)) # Close the initial state self.init.logically_close() if assume_closed: self.init.close_omniscience() assert self.init.is_consistent(), "Error: Inconsistent initial state" def parse_goal(self, prob): self.goal = PDKB(self.domain.depth, self.domain.agents, self.domain.props) assert isinstance(prob.goal, pdkb.pddl.formula.And) for g in prob.goal.args: self.goal.add_rml(prim2rml(g))