def p_literal(p): '''literal : NEGATION LPAREN atom RPAREN | NEGATION atom | atom''' if len(p) == 5: p[0] = Literal.negative(p[3]) elif len(p) == 3: p[0] = Literal.negative(p[2]) else: p[0] = Literal.positive(p[1])
def setUp(cls): cls.r1 = 'running(c1,1)' cls.r2 = 'running(c2,1)' cls.r3 = 'running(c3,1)' cls.lp1 = Literal.positive(cls.r1) cls.ln1 = Literal.negative(cls.r1) cls.lp2 = Literal.positive(cls.r2) cls.ln2 = Literal.negative(cls.r2) cls.lp3 = Literal.positive(cls.r3) cls.ln3 = Literal.negative(cls.r3)
def p_literal(p): '''literal : LPAREN NOT_KEY predicate RPAREN | predicate''' if len(p) == 2: p[0] = Literal.positive(p[1]) elif len(p) == 5: p[0] = Literal.negative(p[3])
def __init__(self, atoms): self._atoms = atoms self._terms = [] self._weights = {} self.__index = {} for atom in self._atoms: self.__index[Literal.positive(atom)] = set() self.__index[Literal.negative(atom)] = set()
def test_combine(self): parser = Parser() transition_model = ''' 1.000::running(c1, 1) :- running(c1, 0). ''' atoms1, rules1 = parser.parse(transition_model) wdnf1 = WeightedDNF(set(atoms1.keys())) for index, rule in enumerate(rules1): pos_term = tuple( sorted([Literal.positive(rule.head)] + list(rule.body))) wdnf1.add_term(pos_term, rule.probability) neg_term = tuple( sorted([Literal.negative(rule.head)] + list(rule.body))) wdnf1.add_term(neg_term, 1 - rule.probability) value_model = ''' V(0) :- not(running(c1, 1)), not(running(c2, 1)), not(running(c3, 1)). V(1) :- running(c1, 1), not(running(c2, 1)), not(running(c3, 1)). V(1) :- not(running(c1, 1)), running(c2, 1), not(running(c3, 1)). V(1) :- not(running(c1, 1)), not(running(c2, 1)), running(c3, 1). V(2) :- running(c1, 1), running(c2, 1), not(running(c3, 1)). V(2) :- running(c1, 1), not(running(c2, 1)), running(c3, 1). V(2) :- not(running(c1, 1)), running(c2, 1), running(c3, 1). V(3) :- running(c1, 1), running(c2, 1), running(c3, 1). ''' atoms2, rules2 = parser.parse(value_model) wdnf2 = WeightedDNF(set(atoms2.keys())) for index, rule in enumerate(rules2): wdnf2.add_term(sorted(rule.body), float(rule.head[2:-1])) prod = combine(wdnf1, wdnf2, float.__mul__) self.assertEqual(len(prod), len(wdnf2)) pos = Literal.positive('running(c1,1)') neg = Literal.negative('running(c1,1)') r1 = Literal.positive('running(c1,0)') for weight, term in prod: self.assertEqual(len(term), 4) if neg in term: self.assertEqual(weight, 0.0) if pos in term: count = sum([ literal.is_positive() for literal in term if literal != r1 ]) self.assertEqual(weight, float(count))
def __update_index(self, term, index): term = set(term) for atom in self._atoms: positive_literal = Literal.positive(atom) negative_literal = Literal.negative(atom) if positive_literal in term: self.__index[positive_literal].add(index) continue if negative_literal in term: self.__index[negative_literal].add(index) continue self.__index[positive_literal].add(index) self.__index[negative_literal].add(index)
def test_add_term(self): # value function wdnf1 = wdnf.WeightedDNF(self.atoms1) self.assertEqual(len(wdnf1), 0) for index, rule in enumerate(self.rules1): wdnf1.add_term(rule.body, int(rule.head[2:-1])) self.assertEqual(len(wdnf1), index + 1) fluents = ['running(c1,1)', 'running(c2,1)', 'running(c3,1)'] for fluent in fluents: pos = Literal.positive(fluent) neg = Literal.negative(fluent) t1 = set(wdnf1.terms_by_literals([pos])) t2 = set(wdnf1.terms_by_literals([neg])) self.assertEqual(len(t1), len(wdnf1) / 2) self.assertEqual(len(t2), len(wdnf1) / 2) self.assertEqual(t1 & t2, set()) self.assertEqual(t1 | t2, set(wdnf1.terms)) values = [0, 1, 2, 3] terms_sets = [set(wdnf1.terms_by_weight(v)) for v in values] self.assertEqual(sum([len(s) for s in terms_sets]), len(wdnf1)) for i in range(len(terms_sets[-1])): for j in range(i + 1, len(terms_sets)): self.assertEqual(terms_sets[i] & terms_sets[j], set()) # transition function wdnf2 = wdnf.WeightedDNF(self.atoms2) self.assertEqual(len(wdnf2), 0) for index, rule in enumerate(self.rules2): pos_term = tuple([Literal.positive(rule.head)] + list(rule.body)) wdnf2.add_term(pos_term, rule.probability) neg_term = tuple([Literal.negative(rule.head)] + list(rule.body)) wdnf2.add_term(neg_term, 1 - rule.probability) self.assertEqual(len(wdnf2), 2 * len(self.rules2)) next_state_fluents = [ 'running(c1,1)', 'running(c2,1)', 'running(c3,1)' ] for fluent in next_state_fluents: pos = Literal.positive(fluent) neg = Literal.negative(fluent) t1 = set(wdnf2.terms_by_literals([pos])) t2 = set(wdnf2.terms_by_literals([neg])) self.assertEqual(len(t1), len(t2)) inter = set(term for _, term in t1 & t2) self.assertTrue(pos not in inter) self.assertTrue(neg not in inter) actions = ['reboot(c1)', 'reboot(c2)', 'reboot(c3)'] for action in actions: pos = Literal.positive(action) neg = Literal.negative(action) t1 = set(wdnf2.terms_by_literals([pos])) t2 = set(wdnf2.terms_by_literals([neg])) inter = set(term for _, term in t1 & t2) self.assertTrue(pos not in inter) self.assertTrue(neg not in inter) if action == actions[0]: self.assertEqual(len(t1), len(wdnf2) - 6) self.assertEqual(len(t2), len(wdnf2) - 2) if action == actions[2]: self.assertEqual(len(t1), len(wdnf2) - 6) self.assertEqual(len(t2), len(wdnf2) - 2)