def testSetMinus(p, q): premisse = () outputatoms = () input = dlvhex.getInputAtoms() for x in input: tup = x.tuple() if tup[0].value() == p.value(): # keep true monotonic input atoms if dlvhex.isTrue(x): premisse = (x, ) + premisse if not dlvhex.isTrue(dlvhex.storeAtom((q, tup[1]))): outputatoms = (dlvhex.storeOutputAtom((tup[1], )), ) + outputatoms dlvhex.output((tup[1], )) if tup[0].value() == q.value(): # keep false antimonotonic input atoms if not dlvhex.isTrue(x): premisse = (x.negate(), ) + premisse # learn one nogood for each output atom for x in outputatoms: dlvhex.learn((x.negate(), ) + premisse)
def testSetMinus(p, q): premisse = () outputatoms = () input = dlvhex.getInputAtoms() for x in input: tup = x.tuple() if tup[0].value() == p.value(): # keep true monotonic input atoms if dlvhex.isTrue(x): premisse = (x, ) + premisse if x.isTrue() and not dlvhex.isTrue(dlvhex.storeAtom((q, tup[1]))): outputatoms = (dlvhex.storeOutputAtom((tup[1], )), ) + outputatoms dlvhex.output((tup[1], )) if tup[0].value() == q.value(): # keep false antimonotonic input atoms if not dlvhex.isTrue(x): premisse = (x.negate(), ) + premisse # learn one nogood for each output atom for x in outputatoms: dlvhex.learn((x.negate(), ) + premisse)
def testSetMinus2(p, q): for x in p.extension(): if not x in q.extension(): dlvhex.learn((dlvhex.storeAtom((p, ) + x), dlvhex.storeAtom((q, ) + x).negate(), dlvhex.storeOutputAtom(x).negate())) dlvhex.output(x)
def testSetMinus2(p, q): for x in p.extension(): if not x in q.extension(): dlvhex.learn(( dlvhex.storeAtom((p, ) + x), dlvhex.storeAtom((q, ) + x).negate(), dlvhex.storeOutputAtom(x).negate() )) dlvhex.output(x)
def neg(p): if dlvhex.learnSupportSets(): dlvhex.learn(( dlvhex.storeAtom((p, )).negate(), # if p is true dlvhex.storeOutputAtom(()).negate() # then () is in the output )); else: for x in dlvhex.getTrueInputAtoms(): return dlvhex.output(())
def id(p): if dlvhex.learnSupportSets(): dlvhex.learn(( dlvhex.storeAtom((p, )), # if p is true for some X dlvhex.storeOutputAtom(()).negate() # then () is in the output )); else: for x in dlvhex.getTrueInputAtoms(): dlvhex.output(()) return
def parity(p): if dlvhex.learnSupportSets(): pos = () for x in dlvhex.getInputAtoms(): pos = pos + (False, ) # special case: no input if pos == (): # always true dlvhex.learn(dlvhex.storeOutputAtom(()).negate(), ) else: pos = pos[:-1] pos = list(pos) overflow = False while not overflow: ng = () # enumerate all combinations except for the last element (which is then definite) last = False for i in range(0, len(pos)): if pos[i] == True: ng = ng + (dlvhex.getInputAtoms()[i], ) last = not last else: ng = ng + (dlvhex.getInputAtoms()[i].negate(), ) # add last element with a sign such that the partiy is even if last: ng = ng + (dlvhex.getInputAtoms()[-1], ) else: ng = ng + (dlvhex.getInputAtoms()[-1].negate(), ) # generate nogood which implies that the external atom is true supset = ng + (dlvhex.storeOutputAtom(()).negate(), ) dlvhex.learn(supset) # go to next combination and check if we have an overflow, i.e., all combinations have been enumerated inc=0 pos[inc] = not pos[inc] while not overflow and not pos[inc]: inc = inc + 1 if inc >= len(pos): overflow = True else: pos[inc] = not pos[inc] even = True for atom in dlvhex.getInputAtoms(): if atom.isTrue(): even = not even if even: dlvhex.output(())
def aOrNotB(a,b): if dlvhex.learnSupportSets(): dlvhex.learn(( dlvhex.storeAtom((a, )), dlvhex.storeOutputAtom(()).negate() # then () is in the output )); dlvhex.learn(( dlvhex.storeAtom((b, )).negate(), dlvhex.storeOutputAtom(()).negate() # then () is in the output )); else: aIsTrue = dlvhex.isTrue(dlvhex.storeAtom((a, ))) bIsFalse = dlvhex.isFalse(dlvhex.storeAtom((b, ))) if aIsTrue or bIsFalse: dlvhex.output(())
def testSetMinusLearn(p, q): # is true for all constants in extension of p but not in extension of q # (same as testSetMinus) # uses learning pe = p.extension() #logging.error("ATOM got pe {}".format(repr(pe))) #for y in pe: # logging.error("ATOM y in pe {} {} {}".format(str(y), repr(y[0].symlit.lit), hash(y))) qe = q.extension() #logging.error("ATOM got qe {}".format(repr(qe))) #for y in qe: # logging.error("ATOM y in qe {} {} {}".format(str(y), repr(y[0].symlit.lit), hash(y))) for x in pe: #logging.error("ATOM x = {} {}".format(repr(x), x.__class__)) if x not in qe: #logging.error("ATOM {} not in qe".format(x)) # learn that it is not allowed that p(x) and -q(x) and this atom is false for x nogood = (dlvhex.storeAtom((p, ) + x), dlvhex.storeAtom((q, ) + x).negate(), dlvhex.storeOutputAtom(x).negate()) #logging.error("ATOM nogood {}".format(repr(nogood))) dlvhex.learn(nogood) #logging.error("ATOM output {}".format(repr(x))) dlvhex.output(x)
def someSelectedLearning(selected): for x in dlvhex.getInputAtoms(): if x.tuple()[0] == selected and x.isTrue(): dlvhex.output(()) nogood = [x, dlvhex.storeOutputAtom(()).negate()] dlvhex.learn(nogood)
def learn(self, nogood): #logging.info("java learns nogood %s", [ str(x) for x in nogood ]) dlvhex.learn([x.hid for x in nogood])