⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 assrule.cpp

📁 用data miming技术进行false prediction
💻 CPP
📖 第 1 页 / 共 2 页
字号:
            if (applicable) {
                vector<string> thenVec = r->getThenVector();
                /* As the thenVec will have only one failure event so we check
                 * if it is present in the test case
                 */
                for (i = thenVec.begin(); i != thenVec.end(); i++) {
                    string thenEvent = *i;
                    if (thenEvent.find("Failure") == string::npos) {
                        continue;
                    }
                    size_t h = string_hash(thenEvent);
                    curr = strMap.find(h);
                    if (curr == strMap.end()) {
                        //fp++;
                        correctPrediction = false;
                        applicable = false;
                        break;
                    }
                }
                if (applicable) {
                    tp++;
                    correctPrediction = true;
                    break;
                }
            }
        }

        if (! correctPrediction) {
            fp++;
        }
    }

    totRec++;
}

void AssRule::testAssociationRules() throw (AppException) {
    string sName;
    string line;
    int recNum = 0;

    try {
        //ifstream inFile(dataFileName, ios::in);
        ifstream inFile(testFileName, ios::in);

        if (inFile.is_open()) {
            while (!inFile.eof()) {
                getline(inFile, line);
                recNum++;
                if ((recNum < testStartRec) || (recNum > testEndRec)) {
                    continue;
                }
                //cout << "testing Range (" << testStartRec << "," << testEndRec << ") " << recNum << endl;

                trim(line);
                this->checkPrediction(line);
            }
            inFile.close();
        } else {
            throw AppException(9, "unable to open Input file", __FILE__, __LINE__);
        }
    } catch (exception &e) {
        throw AppException(9, string("abnormal error in processing Input file - ") + string(e.what()), __FILE__, __LINE__);
    }
    /*
    this->checkPrediction("2 3 7 51 24");
    this->checkPrediction("4 51 6 24");
    this->checkPrediction("1 2 4");
    this->checkPrediction("31 10 4 40 45");*/
}

void AssRule::computeStatistics() {
    float precision = (float)tp/(tp+fp);
    float recall = (float)tp/totRec;

    cout << setiosflags(ios::fixed | ios::showpoint) << setprecision(4);
    cout << setw(7) << Config::getInstance()->getSupport() << "  " << setw(10) << Config::getInstance()->getConfidence();
    cout << "  " << setw(4) << tp << "  " << setw(4) << fp << "  " << setw(6) << totRec << "  " << setw(6) << sinRec;
    cout << "  " << setw(9) << precision << "  " << setw(6) << recall << endl;
    /*
    cout << "Confidence: " << CONFIDENCE << endl;
    cout << "tp: " << tp << endl;
    cout << "fp: " << fp << endl;
    cout << "totRec: " << totRec << endl;
    cout << "sinRec: " << sinRec << endl;
    cout << "precision: " << precision << endl;
    cout << "recall: " << recall << endl;*/
}

AssRule::AssRule(const char* fileName, int testStartRec, int testEndRec) {
    this->dataFileName = fileName;
    this->testFileName = fileName;
    //this->itemHeaderTable = ItemHeaderTable::getInstance();
    this->fiMap = new fiHashMap;
    this->itemHeaderTable = new ItemHeaderTable(fiMap);
    this->rootNode = new Node("root", 0, 1);
    this->ruleVec = new vector<Rule*>;
    tp = 0;
    fp = 0;
    totRec = 0;
    sinRec = 0;
    this->testStartRec = testStartRec;
    this->testEndRec = testEndRec;
};

AssRule::AssRule(const char* dataFileName, const char* testFileName, int testStartRec, int testEndRec) {
    this->dataFileName = dataFileName;
    this->testFileName = testFileName;
    //this->itemHeaderTable = ItemHeaderTable::getInstance();
    this->fiMap = new fiHashMap;
    this->itemHeaderTable = new ItemHeaderTable(fiMap);
    this->rootNode = new Node("root", 0, 1);
    this->ruleVec = new vector<Rule*>;
    tp = 0;
    fp = 0;
    totRec = 0;
    sinRec = 0;
    this->testStartRec = testStartRec;
    this->testEndRec = testEndRec;
};

AssRule::~AssRule() {
    //cout << "deleting assRule" << endl;
    if (itemHeaderTable != 0) {
        delete itemHeaderTable;
    }

    //cout << "deleting rootNode" << endl;
    if (rootNode != 0) {
        delete rootNode;
    }

    //cout << "deleting fiMap" << endl;
    if (fiMap != 0) {
        fiHashMap::iterator iter;
        for (iter = fiMap->begin(); iter != fiMap->end(); iter++) {
            delete (*iter).second;
        }
        delete fiMap;
    }

    //cout << "deleting ruleVec" << endl;
    if (ruleVec != 0) {
        vector<Rule*>::iterator itr;
        for (itr = ruleVec->begin(); itr != ruleVec->end(); itr++) {
            delete *itr;
        }
        delete ruleVec;
    }
};

void AssRule::learnRules() throw (AppException) {
    //cout << "starting firstScan" << endl;
    this->processFirstScan();
    //cout << "starting secondScan" << endl;
    this->processSecondScan();
    //cout << "starting frequentItemset" << endl;
    this->extractFrequentItemsets();
    //cout << "starting ARules" << endl;
    this->extractAssociationRules();
}

void AssRule::runEngine() throw (AppException) {
    this->learnRules();
    //cout << "starting testingRules" << endl;
    this->testAssociationRules();
    //cout << "computing stats" << endl;
    this->computeStatistics();
    //cout << "stopped engine" << endl;
};

bool AssRule::predictFailure(vector<Event*>* eventList) const {
    string str;
    bool applicable = true;

    map<const size_t, string> strMap;
    map<const size_t, string>::iterator curr;

    vector<Event*>::iterator iter;
    for (iter = eventList->begin(); iter != eventList->end(); iter++) {
        Event* event = *iter;
        str = event->getCategory();
        size_t h = string_hash(str);
        strMap[h] = str;
    }

    vector<Rule*>::iterator itr;
    vector<string>::iterator i;
    for (itr = ruleVec->begin(); itr != ruleVec->end(); itr++) {
        applicable = true;
        Rule* r = *itr;
        vector<string> ifVec = r->getIfVector();

        for (i = ifVec.begin(); i != ifVec.end(); i++) {
            size_t h = string_hash(*i);
            curr = strMap.find(h);
            if (curr == strMap.end()) {
                applicable = false;
                break;
            }
        }

        if (applicable) {
            return true;
        }
    }

    return false;
}

string AssRule::getPredictionResult(string inputStr) {
    string str;
    bool applicable = true;
    bool correctPrediction = true;
    map<const size_t, string> strMap;
    map<const size_t, string>::iterator curr;

    tokenizer<> tok(inputStr);
    for (tokenizer<>::iterator beg = tok.begin(); beg != tok.end(); ++beg) {
        str = *beg;
        size_t h = string_hash(str);
        strMap[h] = str;
    }

    /*
     *  If it is a failure event without any precursor events then
     *  we cannot apply any of the association rules.
     */

    if (strMap.size() == 1) {
        return ("Fn");
    } else {
        vector<Rule*>::iterator itr;
        vector<string>::iterator i;
        for (itr = ruleVec->begin(); itr != ruleVec->end(); itr++) {
            applicable = true;
            Rule* r = *itr;
            vector<string> ifVec = r->getIfVector();

            for (i = ifVec.begin(); i != ifVec.end(); i++) {
                size_t h = string_hash(*i);
                curr = strMap.find(h);
                if (curr == strMap.end()) {
                    applicable = false;
                    break;
                }
            }

            if (applicable) {
                vector<string> thenVec = r->getThenVector();
                /* As the thenVec will have only one failure event so we check
                 * if it is present in the test case
                 */
                for (i = thenVec.begin(); i != thenVec.end(); i++) {
                    string thenEvent = *i;
                    if (thenEvent.find("Failure") == string::npos) {
                        continue;
                    }
                    size_t h = string_hash(thenEvent);
                    curr = strMap.find(h);
                    if (curr == strMap.end()) {
                        correctPrediction = false;
                        applicable = false;
                        break;
                    }
                }
                if (applicable) {
                    correctPrediction = true;
                    return ("Tp");
                }
            }
        }

        if (! correctPrediction) {
            return ("Fp");
        }

        if (! applicable) {
            return ("Fn");
        }
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -