Пример #1
0
def pl_resolve(ci, cj):
    """Return al clauses that can be obtained by resolving clauses ci and cj."""
    clauses = set()
    clauses = clauses.union(set(disjuncts(ci)))
    clauses = clauses.union(set(disjuncts(cj)))
    needBreak = False
    for di in disjuncts(ci):
        if needBreak:
            break
        for dj in disjuncts(cj):
            if di == ~dj or ~di == dj:
                clauses = removeall(di, clauses)
                clauses = removeall(dj, clauses)
                needBreak = True
                break
    result = False
    if not clauses:
        result = False
    else:
        #check if we resolve a TRUE clauses, which is useless
        for di in clauses:
            for dj in clauses:
                if di == ~dj or ~di == dj:
                    return True
        result = associate('|', clauses)
    print("pl_resolve called with {}, {}, and to return:{}".format(ci, cj, result))
    return result 
Пример #2
0
def pl_resolve(ci, cj):
    """Return all clauses that can be obtained by resolving clauses ci and cj."""
    clauses = []
    for di in disjuncts(ci):
        for dj in disjuncts(cj):
            if di == ~dj or ~di == dj:
                dnew = unique(removeall(di, disjuncts(ci)) +
                              removeall(dj, disjuncts(cj)))
                clauses.append(associate('|', dnew))
    return clauses
Пример #3
0
def pl_resolve(ci, cj):
    clauses = []
    for di in disjuncts(ci):
        for dj in disjuncts(cj):
            if di == ~dj or ~di == dj:
                dnew = unique(
                    removeall(di, disjuncts(ci)) +
                    removeall(dj, disjuncts(cj)))
                clauses.append(associate('|', dnew))
    return clauses
Пример #4
0
def pl_resolve(ci, cj):
    """Return all clauses that can be obtained by resolving clauses ci and cj."""
    clauses = []
    for di in disjuncts(ci):
        for dj in disjuncts(cj):
            if di == ~dj or ~di == dj:
                dnew = unique(removeall(di, disjuncts(ci)) +
                              removeall(dj, disjuncts(cj)))
                clauses.append(associate('|', dnew))
    return clauses
Пример #5
0
def resolve(ci, cj):
    #
    disjunction_ci = disjuncts(ci)
    disjunction_cj = disjuncts(cj)

    clauses = []
    for literal_i in disjunction_ci:
        for literal_j in disjunction_cj:
            if literal_i == ~literal_j or ~literal_i == literal_j:
                remaining = removeall(literal_i, disjunction_ci) + removeall(
                    literal_j, disjunction_cj)
                remaining = unique(remaining)
                new_clause = associate(Or, remaining)
                clauses.append(new_clause)

    return clauses
Пример #6
0
def fol_resolve(ci, cj):
    """Return all clauses that can be obtained by resolving clauses ci and cj.
    >>> for res in pl_resolve(to_cnf(A|B|C), to_cnf(~B|~C|F)):
    ...    ppset(disjuncts(res))
    set([A, C, F, ~C])
    set([A, B, F, ~B])
    """
    #print "TODO - fol_resolve: %s, %s" % (ci, cj)
    clauses = []
    for di in disjuncts(ci):
        for dj in disjuncts(cj):
            #print "TODO - di, dj: %s, %s" % (di, dj)
            dnew = None
            if di == ~dj or ~di == dj:
                dnew = unique(
                    removeall(di, disjuncts(ci)) +
                    removeall(dj, disjuncts(cj)))
            else:
                notDj = ~dj
                if (notDj.op == '~' and notDj.args[0].op == '~'):
                    notDj = notDj.args[0].args[0]
                unifySubst = unify(di, notDj, {})
                if (unifySubst != None):
                    #print "TODO - unifySubst is %s" % unifySubst
                    s = subst(unifySubst, di)
                    t = disjuncts(subst(unifySubst, ci))
                    #print "TODO - subst(unifySubst, di) is %s" % s
                    #print "TODO - disjuncts(subst(unifySubst, ci)) is %s" % t
                    #print "TODO - removeall(subst(unifySubst,di),disjuncts(subst(unifySubst,ci))) is %s" % removeall(s, t)
                    #print "TODO - subst(unifySubst, dj) is %s" % subst(unifySubst, dj)
                    #print "TODO - disjuncts(subst(unifySubst, cj)) is %s" % disjuncts(subst(unifySubst, cj))
                    dnew = unique(
                        removeall(subst(unifySubst, di),
                                  disjuncts(subst(unifySubst, ci))) +
                        removeall(subst(unifySubst, dj),
                                  disjuncts(subst(unifySubst, cj))))
                    #print "TODO -  unified! dnew is %s" % dnew
            if (dnew != None):
                clauses.append(NaryExpr('|', *dnew))
    #if len(clauses) > 0:
    #print "TODO - fol_resolve %s, %s is %s" % (ci, cj, clauses)
    return clauses
Пример #7
0
def dpll(clauses, symbols, model):
    "See if the clauses are true in a partial model."
    unknown_clauses = []  # clauses with an unknown truth value
    for c in clauses:
        val = pl_true(c, model)
        if val is False:
            return False
        if val is not True:
            unknown_clauses.append(c)
    if not unknown_clauses:
        return model
    P, value = find_pure_symbol(symbols, unknown_clauses)
    if P:
        return dpll(clauses, removeall(P, symbols), extend(model, P, value))
    P, value = find_unit_clause(clauses, model)
    if P:
        return dpll(clauses, removeall(P, symbols), extend(model, P, value))
    if not symbols:
        raise TypeError("Argument should be of the type Expr.")
    P, symbols = symbols[0], symbols[1:]
    return (dpll(clauses, symbols, extend(model, P, True)) or
            dpll(clauses, symbols, extend(model, P, False)))
Пример #8
0
def dpll(clauses, symbols, model):
    """See if the clauses are true in a partial model."""
    unknown_clauses = []  # clauses with an unknown truth value
    for c in clauses:
        val = pl_true(c, model)
        if val is False:
            return False
        if val is not True:
            unknown_clauses.append(c)
    if not unknown_clauses:
        return model
    P, value = find_pure_symbol(symbols, unknown_clauses)
    if P:
        return dpll(clauses, removeall(P, symbols), extend(model, P, value))
    P, value = find_unit_clause(clauses, model)
    if P:
        return dpll(clauses, removeall(P, symbols), extend(model, P, value))
    if not symbols:
        raise TypeError("Argument should be of the type Expr.")
    P, symbols = symbols[0], symbols[1:]
    return (dpll(clauses, symbols, extend(model, P, True))
            or dpll(clauses, symbols, extend(model, P, False)))
Пример #9
0
 def decision_tree_learning(examples, attrs, parent_examples=()):
     if len(examples) == 0:
         return plurality_value(parent_examples)
     elif all_same_class(examples):
         return DecisionLeaf(examples[0][target])
     elif len(attrs) == 0:
         return plurality_value(examples)
     else:
         A = choose_attribute(attrs, examples)
         tree = DecisionFork(A, dataset.attrnames[A])
         for (v_k, exs) in split_by(A, examples):
             subtree = decision_tree_learning(exs, removeall(A, attrs),
                                              examples)
             tree.add(v_k, subtree)
         return tree
Пример #10
0
 def decision_tree_learning(examples, attrs, parent_examples=()):
     if len(examples) == 0:
         return plurality_value(parent_examples)
     elif all_same_class(examples):
         return DecisionLeaf(examples[0][target])
     elif len(attrs) == 0:
         return plurality_value(examples)
     else:
         A = choose_attribute(attrs, examples)
         tree = DecisionFork(A, dataset.attrnames[A])
         for (v_k, exs) in split_by(A, examples):
             subtree = decision_tree_learning(
                 exs, removeall(A, attrs), examples)
             tree.add(v_k, subtree)
         return tree
Пример #11
0
def resolve(ci, cj):
    """
    Generate all clauses that can be obtained by applying
    the resolution rule on ci and cj.
    """

    clauses = []
    dci = disjuncts(ci)
    dcj = disjuncts(cj)

    for di in dci:
        for dj in dcj:
            # If di, dj are complementary
            if di == ~dj or ~di == dj:
                # Create list of all disjuncts except di and dj
                res = removeall(di, dci) + removeall(dj, dcj)
                # Remove duplicates
                res = unique(res)
                # Join into new clause
                dnew = associate(Or, res)

                clauses.append(dnew)

    return clauses
Пример #12
0
 def setproblem(self, target, inputs=None, exclude=()):
     """Set (or change) the target and/or inputs.
     This way, one DataSet can be used multiple ways. inputs, if specified,
     is a list of attributes, or specify exclude as a list of attributes
     to not use in inputs. Attributes can be -n .. n, or an attrname.
     Also computes the list of possible values, if that wasn't done yet."""
     self.target = self.attrnum(target)
     exclude = list(map(self.attrnum, exclude))
     if inputs:
         self.inputs = removeall(self.target, inputs)
     else:
         self.inputs = [a for a in self.attrs
                        if a != self.target and a not in exclude]
     if not self.values:
         self.values = list(map(unique, list(zip(*self.examples))))
     self.check_me()
Пример #13
0
 def setproblem(self, target, inputs=None, exclude=()):
     """Set (or change) the target and/or inputs.
     This way, one DataSet can be used multiple ways. inputs, if specified,
     is a list of attributes, or specify exclude as a list of attributes
     to not use in inputs. Attributes can be -n .. n, or an attrname.
     Also computes the list of possible values, if that wasn't done yet."""
     self.target = self.attrnum(target)
     exclude = list(map(self.attrnum, exclude))
     if inputs:
         self.inputs = removeall(self.target, inputs)
     else:
         self.inputs = [a for a in self.attrs
                        if a != self.target and a not in exclude]
     if not self.values:
         self.update_values()
     self.check_me()
Пример #14
0
def information_content(values):
    """Number of bits to represent the probability distribution in values."""
    probabilities = normalize(removeall(0, values))
    return sum(-p * math.log2(p) for p in probabilities)
Пример #15
0
def information_content(values):
    "Number of bits to represent the probability distribution in values."
    probabilities = normalize(removeall(0, values))
    return sum(-p * log2(p) for p in probabilities)
Пример #16
0
def main():
    actionDatabaseDir = config.actionDatabaseDir
    categories = config.actionCateogory
    
    ## Step.1 Data loading and features extraction 
    # Get features from training data over all categories
    print "Data loading and feature extraction ..."
    
    allFeatures = np.array([])
    trainActionSequence = []
    testActionSequence = []
    
    if os.path.exists('allFeatures.npy') \
        and os.path.exists('trainActionSequence.npy') \
        and os.path.exists('testActionSequence.npy'):
        
        allFeatures = np.load('allFeatures.npy')
        trainActionSequence = np.load('trainActionSequence.npy')
        testActionSequence = np.load('testActionSequence.npy')
    else:
        for category in categories:
            categoryPath = os.path.join(actionDatabaseDir, category)
            allData = os.listdir(categoryPath)
            
            
            # Train data and test data loading
            for data in allData:
                filePath = os.path.join(categoryPath, data)
                actionSequence = ActionSequence(filePath)
                actionSequence.extractStip()    # extract STIP
                
                subject = actionSequence.subject
                if subject in config.trainDataSubjects:
                    trainActionSequence.append(actionSequence)
                    
                    if allFeatures.size == 0:
                        allFeatures = actionSequence.stipFeatures
                    else:
                        allFeatures = np.vstack((allFeatures, 
                                                 actionSequence.stipFeatures))
                        
                elif subject in config.testDataSubjects:
                    testActionSequence.append(actionSequence)
            
        
        np.save('allFeatures', allFeatures)        
        np.save('trainActionSequence', trainActionSequence)
        np.save('testActionSequence', testActionSequence)
            
    ## Step.2 Codebook generation
    print "Codebook generation ..."
    bovw = BagOfWords(featureEncodingMethod = 'sparse-coding',
                      poolingMethod = 'max-pooling',
                      normalizationMethod = 'L2-norm')

#     bovw = BagOfWords(featureEncodingMethod = 'vector-quantization',
#                   poolingMethod = 'sum-pooling',
#                   normalizationMethod = 'L1-norm')
    
    if os.path.exists('codebook.npy'):
        codebook = np.load('codebook.npy')
        bovw.codebook = codebook
    else:
        bovw.generateCodebook(allFeatures)
        np.save('codebook', bovw.codebook)
        
    ## Step.3 Feature encoding for train data
    train_y = []
    train_X = []
    
    for actionSequence in trainActionSequence:
        # Feature encoding, pooling, normalization
        actionSequence.generateFinalFeatures(bovw)
        
        # Format train data
        train_y.append(actionSequence.categoryId)
        train_X.append(actionSequence.finalFeatures)
    
    # Cross validation    
    if config.is_cv:
        # cross validation
        crossValidate(train_y, train_X)
    
    ## Step.4 Classification
    # Learning using SVM
    svmTool = SvmTool()
    print "Model learning ..."
    svmTool.learnModel(train_y, train_X)
    
    # Feature encoding for test data and classify data using learned model
    print "Predicating ..."
    numCorrect = 0
    trueLabels = []
    testPredLabels = []
    removeall(config.predctDir)
    
    for actionSequence in testActionSequence:
        # Feature encoding, pooling, normalization
        actionSequence.generateFinalFeatures(bovw)
        
        # Format train data
        test_y = [actionSequence.categoryId]
        test_X = [actionSequence.finalFeatures]
        
        p_label, _ = svmTool.doPredication(test_y, test_X)
        predCagtegoryId = int(p_label[0])
        predCategoryName = categories[predCagtegoryId]
        
        # Write predicated action to predicated category
        predCategoryPath = os.path.join(config.predctDir, predCategoryName)
        if not os.path.exists(predCategoryPath):
            os.makedirs(predCategoryPath)
        
        predFilePath = os.path.join(predCategoryPath, actionSequence.filename)
        
        f = open(predFilePath, 'w')
        f.close()
        
        if predCagtegoryId == actionSequence.categoryId:
            numCorrect += 1 
            
        trueLabels.append(actionSequence.categoryName)
        testPredLabels.append(predCategoryName)
    
    # Calculate results
    accuracy = numCorrect / float(len(testActionSequence))
    print "accuracy: ", accuracy
    
    # Plot confusion matrix
    saveFilename = 'confusion_matrix.png'
    plotConfusionMatrix(trueLabels, testPredLabels, 
                        saveFilename, normalization = False)
    
    saveFilename = 'confusion_matrix_norm.png'
    plotConfusionMatrix(trueLabels, testPredLabels, 
                        saveFilename, normalization = True)