示例#1
0
def createRarelyRule(rule):
    rarelyIsRule = False
    rarelyIsHead = False
    isImplication = isinstance(rule, basicLogic.operator_bitonic_implication)
    rarelyIsRule = isinstance(rule, basicLogic.operator_monotonic_rarely)

    if isImplication:
        rarelyIsHead = isinstance(rule.clause2,
                                  basicLogic.operator_monotonic_rarely)

    rarelyInRightPlace = rarelyIsRule or rarelyIsHead
    if rarelyIsHead:
        c1 = rule.clause1
        c2 = basicLogic.operator_monotonic_negation(rule.clause1)
        #known to be mostly
        c3 = basicLogic.operator_monotonic_negation(rule.clause2.clause)
        r = basicLogic.operator_tritonic_defaultRule(c1, c2, c3)

        return r
    elif rarelyIsRule:
        c1 = basicLogic.TRUE
        c2 = rule.clause
        c3 = basicLogic.operator_monotonic_negation(rule.clause)
        r = basicLogic.operator_tritonic_defaultRule(c1, c2, c3)
        return r
    else:
        print("Requirement that 'mostly' is the second clause in a->m(b)")
        raise Exception()
示例#2
0
def unit_yuvalOnions():
    yuval = scp.scp(epistemicStateType="dl")
    eatonionSoup = basicLogic.atom("eatOnionSoup")
    loveEating= basicLogic.atom("loveEating")
    eatOnions=basicLogic.atom("eatOnions")
    brushTeeth=basicLogic.atom("brushTeeth")
    careForHygiene=basicLogic.atom("careForHygiene")
    
    dontEatOnions=basicLogic.operator_monotonic_negation(eatOnions)
    
    rule1=basicLogic.operator_tritonic_defaultRule(eatonionSoup,[eatOnions],eatOnions)
    rule2=basicLogic.operator_tritonic_defaultRule(eatonionSoup,[loveEating],loveEating)
    rule3=basicLogic.operator_tritonic_defaultRule(loveEating,[brushTeeth],brushTeeth)
    rule4=basicLogic.operator_tritonic_defaultRule(brushTeeth,[careForHygiene],careForHygiene)
    rule5=basicLogic.operator_tritonic_defaultRule(careForHygiene,[dontEatOnions],dontEatOnions)
    
    factEatsOnionSoup = basicLogic.operator_bitonic_implication(basicLogic.TRUE,eatonionSoup)
    
    D = [rule1,rule2,rule3,rule4,rule5]
    W = [factEatsOnionSoup]
    V =  [eatonionSoup, loveEating, eatOnions, brushTeeth, careForHygiene]
    
    yuval.addDList(D)
    yuval.addVList(V)
    yuval.addWList(W)
    
    yuval.addNext(comp_def_eval)
    
    print ("<<<<<<<<YUVAL>>>>>>>>>>")
    
    print (yuval.evaluate())    
示例#3
0
def unit_quakersRepublicans():
    dick = scp.scp(epistemicStateType="dl")
    
    republican = basicLogic.atom('republican')
    quaker = basicLogic.atom('quaker')
    pacifist = basicLogic.atom('pacifist')
    
    factRepublican = basicLogic.operator_bitonic_implication(basicLogic.TRUE, republican)
    factQuaker = basicLogic.operator_bitonic_implication(basicLogic.TRUE, quaker)
    
    notPacifist = basicLogic.operator_monotonic_negation(pacifist)
    #republicans are usually not pacifists
    rule1 = basicLogic.operator_tritonic_defaultRule(republican,[notPacifist],notPacifist)
    #quakers are usually pacifists
    rule2 = basicLogic.operator_tritonic_defaultRule(quaker,[pacifist],pacifist)
    
    D = [rule1,rule2]
    W = [factRepublican,factQuaker]
    V = [republican,quaker,pacifist]
    
    dick.addDList(D)
    dick.addWList(W)
    dick.addVList(V)
    
    dick.addNext(comp_def_eval)
    
    print ("<<<<<<<<DICK>>>>>>>>>>")
    print (dick.evaluate())
    def evaluateEpistemicState(self, epi):
        #set of conditional rules
        delta = epi['Delta']
        S = epi['S']
        V = epi['V']

        resolvedDependencies = []
        for conditional in delta:
            consequence = conditional.clause1
            precondition = conditional.clause2
            if consequence not in resolvedDependencies:
                lowestk = m_addAB.findLowestK(epi)
                allDependencies = m_addAB.findAllConditionalDependencyPreconditions(
                    consequence, delta)
                abBody = None
                for dep in allDependencies:
                    negateDep = basicLogic.operator_monotonic_negation(dep)
                    if dep != precondition:
                        if abBody == None:
                            abBody = negateDep
                        else:
                            abBody = basicLogic.operator_bitonic_or(
                                abBody, negateDep)
                if abBody == None:
                    abBody = basicLogic.FALSE
                abName = 'ab_' + str(lowestk)
                abAtom = basicLogic.atom(abName, None)
                ab = basicLogic.operator_bitonic_implication(abAtom, abBody)

                negABAtom = basicLogic.operator_monotonic_negation(abAtom)

                newBody = basicLogic.operator_bitonic_and(
                    precondition, negABAtom)
                newRule = basicLogic.operator_bitonic_implication(
                    consequence, newBody)
                #add the abnormality and its assignment to the list of rules
                S.append(newRule)
                S.append(ab)
                V.append(abAtom)
            resolvedDependencies = resolvedDependencies + allDependencies
        #all conditionals have now been interpreted
        epi['Delta'] = []

        return epi
示例#5
0
 def createBodyFromRulesThatAffectHead(self, head, body, li_abs):
     #if basicLogic.isGroundAtom(body):
     if basicLogic.isGroundAtom(body) or basicLogic.isGroundAtom(head):
         return basicLogic.operator_bitonic_implication(body,
                                                        head), None, li_abs
     newAbnormality = basicLogic.atom('ab{}'.format(len(li_abs) + 1))
     negAbnormality = basicLogic.operator_monotonic_negation(newAbnormality)
     li_abs.append(newAbnormality)
     newBody = basicLogic.operator_bitonic_and(body, negAbnormality)
     newRule = basicLogic.operator_bitonic_implication(newBody, head)
     return newRule, newAbnormality, li_abs
示例#6
0
 def getVariablesFromThW(thW):
     v = []
     for x in thW:
         if isinstance(x[0], basicLogic.atom):
             v.append(copy.deepcopy(x[0]))
             v[-1].setValue(x[1])
         elif isinstance(x[0], basicLogic.operator_monotonic_negation):
             v.append(copy.deepcopy(x[0].clause))
             doubleNeg = basicLogic.operator_monotonic_negation(
                 basicLogic.atom('', x[1]))
             v[-1].setValue(doubleNeg.evaluate())
     return v
示例#7
0
    def evaluate(self):
        prev_epi = self.prev.evaluate()
        current_epi = complexOperation.createEmptyNextEpi(prev_epi)
        oldkb = prev_epi.getKB()
        oldV = prev_epi.getV()

        current_epi.addVariableList(oldV)
        current_epi.addKnowledgeList(oldkb)

        for rule in oldkb:
            if not rule.immutable:
                if isinstance(rule, basicLogic.atom) and not self.isGroundAtom(
                        rule.clause2):
                    negateClause1 = basicLogic.operator_monotonic_negation(
                        rule.clause1)
                    negateClause2 = basicLogic.operator_monotonic_negation(
                        rule.clause2)
                    contraRule = basicLogic.operator_bitonic_implication(
                        negateClause1, negateClause2)
                    current_epi.addKnowledge(contraRule)
        return current_epi
示例#8
0
 def createNewAbnormalityInstant(self, otherRulesThatAffectHead):
     # case: KB={D->3, T->3} without this (T -> ab) is created
     otherRulesThatAffectHead = self.removeGroundFromRules(
         otherRulesThatAffectHead)
     #if nothing else affects the head, assume the abnormality is false by default
     if otherRulesThatAffectHead == []:
         rule = basicLogic.FALSE
         return rule
     rule = otherRulesThatAffectHead[0]
     #added a negated disjunction of all the rules
     for other in range(1, len(otherRulesThatAffectHead)):
         neg = basicLogic.operator_monotonic_negation(rule)
         rule = basicLogic.operator_bitonic_or(rule, neg)
     return rule
示例#9
0
def unit_tweetyAndChilly ():
    #create the two birds as individual scps
    tweety = scp.scp(epistemicStateType="dl")
    chilly = scp.scp(epistemicStateType="dl")
    
    #variables
    flies = basicLogic.atom('flies')
    bird = basicLogic.atom('bird')  
    
    notflies = basicLogic.operator_monotonic_negation(flies)
    #the only inference rule
    rule1 = basicLogic.operator_tritonic_defaultRule(bird,[flies],flies)
    
    fact_bird = basicLogic.operator_bitonic_implication(basicLogic.TRUE, bird)
    factNotFlies = basicLogic.operator_bitonic_implication(basicLogic.TRUE, notflies)
    #the set of concrete rules
    W_tweety=[fact_bird]
    W_chilly=[fact_bird,factNotFlies]
    #in this case both tweety and chilly share the same inference rules
    D = [rule1]
    V = [flies,bird]
    #create wteety
    tweety.addDList(D)
    tweety.addVList(V)
    tweety.addWList(W_tweety)
    #create chilly
    chilly.addDList(D)
    chilly.addVList(V)
    chilly.addWList(W_chilly)
    #add the complex operator for evaluating default rules
    tweety.addNext(comp_def_eval)
    chilly.addNext(comp_def_eval)
    print ("<<<<<<<<TWEETY>>>>>>>>>>")
    print (tweety.evaluate())
    
    
    print (chilly.evaluate())
示例#10
0
    def evaluate(self):
        #get the previous epistemic state
        epi_prev = self.prev.evaluate()
        #generate an empy epistemic state of the same type as the previous one
        epi_next = complexOperation.createEmptyNextEpi(epi_prev)

        #all variables in prevkb will be present in the next one (plus abnormalities)
        prev_v = epi_prev.getV()
        epi_next.addVariableList(prev_v)

        #the list of created abnormalities
        ABs = []
        prev_kb = epi_prev.getKB()

        for rule in prev_kb:
            #we do this because there can be 1 or 2 heads/bodies depending on -> or <->
            body = self.getBodies(rule)
            head = self.getHeads(rule)
            if rule.immutable:
                epi_next.addKnowledge(rule)
            else:
                for h in head:
                    for b in body:
                        #truth values don't need abnormalities
                        if basicLogic.isGroundAtom(b):
                            newRule = basicLogic.operator_bitonic_implication(
                                b, h)
                            epi_next.addKnowledge(newRule)
                        else:
                            #find every clause x, with (x->head)
                            rulesThatAffectHead = self.getRulesThatAffectHead(
                                h, prev_kb)
                            #remove this body from the list of rules that affect head
                            otherRulesThatAffectHead = self.removeRuleFromList(
                                b, rulesThatAffectHead)
                            #create the new abnormality
                            newAb = basicLogic.atom(
                                "ab{}".format(len(ABs) + 1))
                            ABs.append(newAb)
                            negAb = basicLogic.operator_monotonic_negation(
                                newAb)
                            newBody = basicLogic.operator_bitonic_and(b, negAb)
                            newRule = basicLogic.operator_bitonic_implication(
                                newBody, h)
                            epi_next.addKnowledge(newRule)

                            #create a valuation for the new abnormality
                            abInstHead = newAb
                            abInstBody = self.createNewAbnormalityInstant(
                                otherRulesThatAffectHead)
                            abInstBodyIsGroundValue = basicLogic.isGroundAtom(
                                abInstBody)
                            newAbInstBody = abInstBody if abInstBodyIsGroundValue else basicLogic.operator_monotonic_negation(
                                abInstBody)
                            abInstRule = basicLogic.operator_bitonic_implication(
                                newAbInstBody, abInstHead)
                            epi_next.addKnowledge(abInstRule)

                            #add the new abnormality to the variable list
                            epi_next.addVariable(newAb)
        return epi_next
示例#11
0
knowledge_3 = basicLogic.operator_bitonic_implication(basicLogic.TRUE, card_3)
knowledge_k = basicLogic.operator_bitonic_implication(basicLogic.TRUE, card_k)
knowledge_7 = basicLogic.operator_bitonic_implication(basicLogic.TRUE, card_7)
# the extra fact that 7->not(3)
#CHANGED pPrime = basicLogic.operator_monotonic_negation(card_3)
pPrime = basicLogic.atom("D'", None)

knowledge_primeRelationp = basicLogic.operator_bitonic_implication(
    card_7, pPrime, immutable=True)
# the extra fact that K->not(D)
#CHANGED qPrime = basicLogic.operator_monotonic_negation(card_k)
qPrime = basicLogic.atom("3'", None)

knowledge_primeRelationq = basicLogic.operator_bitonic_implication(
    card_d, qPrime, immutable=True)
notPPrime = basicLogic.operator_monotonic_negation(pPrime, immutable=True)
knowledgeNotPtoP = basicLogic.operator_bitonic_implication(notPPrime,
                                                           card_d,
                                                           immutable=True)

#ALL POSSIBLE VARIABLES (USED IN ABDUCTION)
allVariables = (card_3, card_7, card_d, card_k, pPrime, qPrime)

#INITIALISE THE SET OF COMPLEX OPERATORS M
# create the complex operation to add abnormalities
comp_addAB = complexOperation.complexOperation_addAB()
# create the complex operation to delete a named variable
comp_deleteo = complexOperation.complexOperation_deleteVariable('o')
# create the complex operation to fix a named variable to a specified value
comp_fixab1 = complexOperation.complexOperation_fixVariable('ab1', False)
# Create the complex operation to weakly complete the logic program