예제 #1
0
def create_si_contra():
    #define the initial atomic name in the WCS: one for each observed card
    #these atoms have value None
    D = basicLogic.atom('D')
    K = basicLogic.atom('K')
    three = basicLogic.atom('3')
    seven = basicLogic.atom('7')
    #this is the atom which means NOT (D)
    Dprime = basicLogic.atom("D'")

    #create an epistemic state containing the known facts and conditionals
    basePointContra = epistemicState.epistemicState('WST')
    #the set of conditionals with the contraposition rule
    delta_contra = ["( 3 | D )", " ( D' | 7 ) "]
    #the set of conditionals as a set of <basicLogic> clauses
    deltaAsLogic = scpNotationParser.stringListToBasicLogic(delta_contra)
    #the set of facts known, this prevents negative heads in rules
    S_contra = ["( ( D ) <- ( !  D'  ) )"]
    #the set of known facts as <basicLogic>
    SAsLogic = scpNotationParser.stringListToBasicLogic(S_contra)
    #the set of abducibles, any subset of which might be an explanation
    abducibs = [
        '( D <- T )', '( D <- F )', '( K <- T )', '( K <- F )', '( 3 <- T )',
        '( 3 <- F )', '( 7 <- T )', '( 7 <- F )'
    ]
    abducibs = ['( D <- T )', '( K <- T )', '( 3 <- T )', '( 7 <- T )']
    #transform the set of abducibles to a set of <basicLogic> clauses
    logAbducibs = scpNotationParser.stringListToBasicLogic(abducibs)

    #set the structural variables of the only epistemic state in the intial state point
    basePointContra['S'] = SAsLogic
    basePointContra['Delta'] = deltaAsLogic
    basePointContra['V'] = [D, K, three, seven, Dprime]
    basePointContra['R'] = {'abducibles': logAbducibs}
    return [basePointContra]
    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
예제 #3
0
    import scpNotationParser
    import CTM
    import CognitiveOperation
    import StatePointOperations

print("=================THE SUPPRESSION TASK=========================")
print(
    ">>> 1) If she has an essay to write she will study late in the library (l|e)."
)
print(
    ">>> 2) If the library is open she will study late in the library (l|o).")
print(">>> 3) She has an essay to write (e <- T).")

#STARTING VARIABLES
# e: she has an essay to write
e = basicLogic.atom('e', setValue=False)
# l: she will study late in the library
l = basicLogic.atom('l', setValue=False)
# o: the library is open
o = basicLogic.atom('o', setValue=False)

# THE SET OF COGNITIVE OPERATIONS APPROPRIATE TO THE SUPPRESSION TASK
ADDAB = CognitiveOperation.m_addAB()
WC = CognitiveOperation.m_wc()
SEMANTIC = CognitiveOperation.m_semantic()
ABDUCIBLES = CognitiveOperation.m_addAbducibles(maxLength=4)
DELETE = CognitiveOperation.m_deleteo()
"""
==================================================================================================
================================EXTERNAL EVALUATION FUNCTIONS=====================================
"""
def polishNotationParser(task, logicType="P"):
    monotonicOps = {
        'Not': basicLogic.operator_monotonic_negation,
        'not': basicLogic.operator_monotonic_negation,
        'Holds': basicLogic.operator_monotonic_holds,
        'Mostly': basicLogic.operator_monotonic_mostly,
        'Rarely': basicLogic.operator_monotonic_rarely
    }
    bitonicOps = {
        'and': basicLogic.operator_bitonic_and,
        'or': basicLogic.operator_bitonic_or,
        'if': basicLogic.operator_bitonic_implication,
        'iff': basicLogic.operator_bitonic_bijection,
        'Implies': basicLogic.operator_bitonic_implication
    }
    specialMappings = {}
    ignoredWords = []
    #This removes all words in ignoredWords from the list completely
    #Holds is in this list because its functionality is trivialized by my implementation
    task = ignoreWords(task, ignoredWords)

    if len(task) == 1 and not isinstance(task[0], basicLogic.operator):
        #set true if epistemic state only has kb and no v
        return [basicLogic.atom(task[0], False)]
    #@TODO still needs to handle negative initialisations
    #PROCEDURE
    # find any case with monotonic operator, base/node
    # find any case with bitonic operator, base/node, number
    # find any case with bitonic operator, number, base/node
    # replace these cases with small node
    # repeat
    changeMade = False
    for i in range(0, len(task)):
        #Currently empty
        if task[i] in specialMappings:
            specialName = task[i]
            task[i] = specialMappings[specialName]()
            break
        #Monotonic Operations
        elif task[i] in monotonicOps and i < len(task) - 1:
            if task[i + 1] not in bitonicOps and task[i +
                                                      1] not in monotonicOps:
                clause = task[i + 1]
                if not isinstance(clause, basicLogic.operator):
                    clause = basicLogic.atom(clause, None)
                monOp = monotonicOps[task[i]](clause, logicType=logicType)
                del task[i:i + 2]
                task.insert(i, monOp)
                changeMade = True
                break
        #Bitonic Operations
        elif task[i] in bitonicOps and i < len(task) - 2:
            if task[i + 1] not in bitonicOps and task[i + 2] not in bitonicOps:
                if task[i +
                        1] not in monotonicOps and task[i +
                                                        2] not in monotonicOps:
                    left = task[i + 1]
                    right = task[i + 2]
                    if not isinstance(left, basicLogic.operator):
                        left = basicLogic.atom(left, None)
                        #x changeMade=True
                    if not isinstance(right, basicLogic.operator):
                        right = basicLogic.atom(right, None)
                        #x changeMade=True
                    bitOp = bitonicOps[task[i]](left,
                                                right,
                                                logicType=logicType)
                    #does +3 because delete is up to but not including
                    del task[i:i + 3]
                    task.insert(i, bitOp)
                    #x I changed
                    changeMade = True
                    break
    if changeMade:
        return polishNotationParser(task)
    return task
def specialSplit(sp):
    #find last index
    def rindex(mylist, myvalue):
        return len(mylist) - mylist[::-1].index(myvalue) - 1

    #must be a literal or a monotonic operator
    #length 1 can only be an atom or truth value
    truthValues = {
        'T': basicLogic.TRUE,
        'u': basicLogic.UNKNOWN,
        'F': basicLogic.FALSE
    }

    monotonicOps = {
        '!': basicLogic.operator_monotonic_negation,
        'h': basicLogic.operator_monotonic_holds,
        'm': basicLogic.operator_monotonic_mostly,
        'r': basicLogic.operator_monotonic_rarely
    }
    bitonicOps = {
        '&': basicLogic.operator_bitonic_and,
        'or': basicLogic.operator_bitonic_or,
        '<-': basicLogic.operator_bitonic_implication,
        '<->': basicLogic.operator_bitonic_bijection,
        '|': basicLogic.operator_bitonic_conditional
    }
    """
    newS = []
    firstBracket=-1
    lastBracket=-1
    #if this is a single rule encapsulated by brackets
    
    if sp[0]=='(':
        firstBracket=0
    if firstBracket == 0:
        for s in range( 0, len(sp)):
            if sp[s]==')':
                lastBracket=s
        return specialSplit(sp[firstBracket+1:lastBracket])
    else:
        #find the first operator when there are an equal number of '(' and ')'
        leftBracketCount = 0
        rightBracketCount = 0
        if sp[0] in monotonicOps:
            if sp[1] == '(':     
                return monotonicOps[sp[0]](specialSplit(sp[1:len(sp)]))
            else:
                return monotonicOps[sp[0]](specialSplit(sp[1]))
        
        for s in range (0, len(sp)):
            if sp[s]=='(':
                leftBracketCount=leftBracketCount+1
            if sp[s]==')':
                rightBracketCount=rightBracketCount+1
            if leftBracketCount == rightBracketCount:
                if sp[s] in bitonicOps:
                    clause1 = specialSplit(sp[0:s])
                    clause2 = specialSplit(sp[s+1:len(sp)])
                    return bitonicOps[sp[s]](clause1,clause2)

    """
    if len(sp) == 1:
        if sp[0] in truthValues:
            return truthValues[sp[0]]
        return basicLogic.atom(name=sp[0], value=None)
    if len(sp) == 3:
        return specialSplit(sp[1])

    countleftBracket = 0
    counterRightBracket = 0
    for i in range(0, len(sp)):
        if sp[i] == '(':
            countleftBracket = countleftBracket + 1
        if sp[i] == ')':
            counterRightBracket = counterRightBracket + 1
        if countleftBracket == counterRightBracket + 1:
            if sp[i] in bitonicOps:
                left = specialSplit(sp[1:i])
                right = specialSplit(sp[i + 1:len(sp) - 1])
                return bitonicOps[sp[i]](left, right)
    #if we are here it must be a long clause (monotonicOp, a, b, ..., Z)
    #is not Atom or (Atom)
    if sp[1] in monotonicOps:
        clause = sp[2:len(sp) - 1]
        return monotonicOps[sp[1]](specialSplit(clause))