예제 #1
0
def funcPrunBranch(dfOrig, tree_model):

    noPathCond = 0
    #data set to hold set of candidate counter examples, refer to cand-set of prunBranch algorithm
    with open('CandidateSetBranch.csv', 'w', newline='') as csvfile:
        fieldnames = dfOrig.columns.values
        writer = cv.writer(csvfile)
        writer.writerow(fieldnames)

    with open('param_dict.csv') as csv_file:
        reader = cv.reader(csv_file)
        paramDict = dict(reader)

    dfRead = pd.read_csv('TestDataSMTMain.csv')

    for row in range(0, dfRead.shape[0]):
        if (paramDict['multi_label'] == 'True'):
            funcgetPath4multiLbl(tree_model, dfOrig, row,
                                 int(paramDict['no_of_params']))
        else:
            funcgetPath(tree_model, dfOrig, row)
        fileCond = open('TreeOutput.txt', 'r')
        first = fileCond.read(1)

        if not first:
            print('No Branch')
        else:
            noPathCond = util.file_len('ConditionFile.txt')
            if (noPathCond == 'empty'):
                return
            for i in range(noPathCond):
                funcAddCond2File(i)
                os.system(r"z3 ToggleBranchSmt.smt2 > FinalOutput.txt")
                satFlag = ReadZ3Output.funcConvZ3OutToData(dfOrig)

                if (satFlag == True):
                    dfSmt = pd.read_csv('TestDataSMT.csv')
                    dataAppend = dfSmt.values
                    with open('CandidateSetBranch.csv', 'a',
                              newline='') as csvfile:
                        writer = cv.writer(csvfile)
                        writer.writerows(dataAppend)
예제 #2
0
def funcPrunInst(dfOrig, dnn_flag):
    #data set to hold set of candidate counter examples, refer to cand-set of prunInst algorithm
    with open('CandidateSetInst.csv', 'w', newline='') as csvfile:
        fieldnames = dfOrig.columns.values
        writer = cv.writer(csvfile)
        writer.writerow(fieldnames)

    with open('param_dict.csv') as csv_file:
        reader = cv.reader(csv_file)
        paramDict = dict(reader)

    if (paramDict['multi_label'] == 'True'):
        noClass = int(paramDict['no_of_class'])
    else:
        noClass = 1

    #Getting the counter example pair (x, x') and saving it to a permanent storage
    dfRead = pd.read_csv('TestDataSMTMain.csv')
    dataRead = dfRead.values

    #Combining loop in line 2 & 6 in a single loop
    for j in range(0, dfRead.shape[0]):
        for i in range(0, dfRead.columns.values.shape[0] - noClass):
            #writing content of DecSmt.smt2 to another file named ToggleFeatureSmt.smt2
            if (dnn_flag == True):
                funcWrite2File('DNNSmt.smt2')
            else:
                funcWrite2File('DecSmt.smt2')

            with open('ToggleFeatureSmt.smt2', 'r') as file:
                text = file.read()
                text = text.replace("(check-sat)", '')
                text = text.replace("(get-model)", '')
                with open('ToggleFeatureSmt.smt2', 'w') as file:
                    file.write(text)

            fileTogFe = open('ToggleFeatureSmt.smt2', 'a')
            name = str(dfRead.columns.values[i])

            data_type = str(dfOrig.dtypes[i])
            if ('int' in data_type):
                digit = int(dataRead[j][i])
            elif ('float' in data_type):
                digit = float(dataRead[j][i])

            digit = str(digit)
            if ((int(paramDict['no_of_params']) == 1)
                    and (paramDict['multi_label'] == 'True')
                    and (paramDict['white_box_model'] == 'Decision tree')):
                fileTogFe.write("(assert (not (= " + name + " " + digit +
                                "))) \n")
            else:
                fileTogFe.write("(assert (not (= " + name + str(j) + " " +
                                digit + "))) \n")
            fileTogFe.write("(check-sat) \n")
            fileTogFe.write("(get-model) \n")
            fileTogFe.close()

            os.system(r"z3 ToggleFeatureSmt.smt2 > FinalOutput.txt")

            satFlag = ReadZ3Output.funcConvZ3OutToData(dfOrig)

            #If sat then add the counter example to the candidate set, refer line 8,9 in prunInst algorithm
            if (satFlag == True):
                dfSmt = pd.read_csv('TestDataSMT.csv')
                dataAppend = dfSmt.values
                with open('CandidateSetInst.csv', 'a', newline='') as csvfile:
                    writer = cv.writer(csvfile)
                    writer.writerows(dataAppend)
예제 #3
0
    def runPropCheck(self):
        retrain_flag = False
        MAX_CAND_ZERO = 5
        count_cand_zero = 0
        count = 0
        satFlag = False
        self.max_samples = int(self.paramDict['max_samples'])
        self.no_of_params = int(self.paramDict['no_of_params'])
        self.mul_cex = self.paramDict['mul_cex_opt']
        self.deadline = int(self.paramDict['deadlines'])
        white_box = self.paramDict['white_box_model']
        start_time = time.time()

        if white_box == 'DNN':
            self.runWithDNN()
        else:
            while count < self.max_samples:
                print('count is:', count)
                tree = trainDecTree.functrainDecTree()
                tree2Logic.functree2LogicMain(tree, self.no_of_params)
                util.storeAssumeAssert('DecSmt.smt2')
                util.addSatOpt('DecSmt.smt2')
                os.system(r"z3 DecSmt.smt2 > FinalOutput.txt")
                satFlag = ReadZ3Output.funcConvZ3OutToData(self.df)
                if not satFlag:
                    if count == 0:
                        print(
                            'No CEX is found by the checker at the first trial'
                        )
                        return 0
                    elif (count != 0) and (self.mul_cex == 'True'):
                        dfCexSet = pd.read_csv('CexSet.csv')
                        if round(dfCexSet.shape[0] / self.no_of_params) == 0:
                            print('No CEX is found')
                            return 0
                        print('Total number of cex found is:',
                              round(dfCexSet.shape[0] / self.no_of_params))
                        self.addModelPred()
                        return round(dfCexSet.shape[0] / self.no_of_params)
                    elif (count != 0) and (self.mul_cex == 'False'):
                        print('No Cex is found after ' + str(count) +
                              ' no. of trials')
                        return 0
                else:
                    processCandCex.funcAddCex2CandidateSet()
                    processCandCex.funcAddCexPruneCandidateSet(tree)
                    processCandCex.funcCheckCex()
                    #Increase the count if no further candidate cex has been found
                    dfCand = pd.read_csv('Cand-set.csv')
                    if round(dfCand.shape[0] / self.no_of_params) == 0:
                        count_cand_zero += 1
                        if count_cand_zero == MAX_CAND_ZERO:
                            if self.mul_cex == 'True':
                                dfCexSet = pd.read_csv('CexSet.csv')
                                print(
                                    'Total number of cex found is:',
                                    round(dfCexSet.shape[0] /
                                          self.no_of_params))
                                if round(dfCexSet.shape[0] /
                                         self.no_of_params) > 0:
                                    self.addModelPred()
                                return round(
                                    dfCexSet.shape[0] / self.no_of_params) + 1
                            else:
                                print('No CEX is found by the checker')
                                return 0
                    else:
                        count = count + round(
                            dfCand.shape[0] / self.no_of_params)

                    data = dfCand.values
                    X = data[:, :-1]
                    y = data[:, -1]
                    if dfCand.shape[0] % self.no_of_params == 0:
                        arr_length = dfCand.shape[0]
                    else:
                        arr_length = dfCand.shape[0] - 1
                    testIndx = 0
                    while testIndx < arr_length:
                        temp_count = 0
                        temp_store = []
                        temp_add_oracle = []
                        for i in range(0, self.no_of_params):
                            if self.funcPrediction(X, dfCand,
                                                   testIndx) == y[testIndx]:
                                temp_store.append(X[testIndx])
                                temp_count += 1
                                testIndx += 1
                            else:
                                retrain_flag = True
                                temp_add_oracle.append(X[testIndx])
                                testIndx += 1
                        if temp_count == self.no_of_params:
                            if self.mul_cex == 'True':
                                with open('CexSet.csv', 'a',
                                          newline='') as csvfile:
                                    writer = cv.writer(csvfile)
                                    writer.writerows(temp_store)
                            else:
                                print(
                                    'A counter example is found, check it in CexSet.csv file: ',
                                    temp_store)
                                with open('CexSet.csv', 'a',
                                          newline='') as csvfile:
                                    writer = cv.writer(csvfile)
                                    writer.writerows(temp_store)
                                self.addModelPred()
                                return 1
                        else:
                            util.funcAdd2Oracle(temp_add_oracle)

                    if retrain_flag:
                        self.funcCreateOracle()

                    if (time.time() - start_time) > self.deadline:
                        print("Time out")
                        break

            dfCexSet = pd.read_csv('CexSet.csv')
            if (round(dfCexSet.shape[0] / self.no_of_params) >
                    0) and (count >= self.max_samples):
                self.addModelPred()
                print('Total number of cex found is:',
                      round(dfCexSet.shape[0] / self.no_of_params))
                print(
                    'No. of Samples looked for counter example has exceeded the max_samples limit'
                )
            else:
                print('No counter example has been found')