📄 predictor.cpp
字号:
#include "Predictor.h"
bool failLesser(Event* e1, Event* e2) {
return *e1 < *e2;
}
Predictor::Predictor(const char* fileName) {
statCount = 0;
assCount = 0;
predictionWindowSize = 3600;
ruleTimeWindowSize = 2100;
historyWindowSize = 2100;
slideWindowSize = 600;
this->failureList = new vector<Event*>;
const char* failureFileName = "..\\logs\\anl_compress5\\allFailures.dat";
//const char* dataFileName = "..\\logs\\anl_compress5\\allFailures.dat";
this->initializeList(failureFileName);
//this->printFailureList();
ar = new AssRule(fileName, 4000, 5000);
ar->learnRules();
statsAnalyzer = new StatsAnalyzer();
eventHistory = new EventHistory();
};
Predictor::~Predictor() {
//cout << "deleting failureList" << endl;
if (failureList != 0) {
vector<Event*>::iterator itr;
for (itr = failureList->begin(); itr != failureList->end(); itr++) {
delete *itr;
}
delete failureList;
}
//cout << "deleting ar" << endl;
if (ar != 0) {
delete ar;
}
//cout << "deleting statsAnalyzer" << endl;
if (statsAnalyzer != 0) {
delete statsAnalyzer;
}
//cout << "deleting eventHistory" << endl;
if (eventHistory != 0) {
delete eventHistory;
}
};
void Predictor::printFailureList() {
if (failureList != 0) {
vector<Event*>::iterator itr;
for (itr = failureList->begin(); itr != failureList->end(); itr++) {
cout << *(*itr) << endl;
}
}
}
void Predictor::initializeList(const char* fileName) {
int mRecId;
int mEventTime;
string mCategory;
int count = 0;
try {
ifstream inFile(fileName, ios::in);
if (inFile.is_open()) {
//cout << "opened file" << endl;
while (!inFile.eof()) {
if (inFile >> mEventTime) {
inFile >> mRecId >> mCategory;
Event* event = new Event(mRecId, mEventTime, mCategory);
failureList->push_back(event);
++count;
}
}
inFile.close();
} else {
throw AppException(9, "unable to open Input file", __FILE__, __LINE__);
}
sort(failureList->begin(), failureList->end(), failLesser);
//cout << "Total Failure Records: " << count << endl;
} catch (exception &e) {
throw AppException(9, string("abnormal error in processing Input file - ") + string(e.what()), __FILE__, __LINE__);
}
}
void Predictor::streamEvents(const char* fileName) {
int mRecId;
int mEventTime;
string mCategory;
string mSeverity;
string mFacility;
int count = 0;
int startTime = 1102368971;
//int predictTime = startTime + (ruleTimeWindowSize - predictionWindowSize);
int predictTime = startTime + historyWindowSize;
//int historyStartTime = predictTime - (ruleTimeWindowSize - predictionWindowSize);
int historyStartTime = predictTime - historyWindowSize;
int Tp = 0;
int Fp = 0;
int Fn = 0;
int Tn = 0;
try {
ifstream inFile(fileName, ios::in);
if (inFile.is_open()) {
cout << "opened file" << endl;
while (!inFile.eof()) {
if (inFile >> mEventTime) {
inFile >> mRecId >> mCategory >> mSeverity >> mFacility;
while (mEventTime > predictTime) {
// flush records from eventHistory before historyStartTime
//cout << "before flush predictTime: " << predictTime << " historyTime: " << historyStartTime << endl;
eventHistory->flushEvents(historyStartTime);
//cout << "after flush" << endl;
// predict for failures now (when time = predictTime)
int toTime = predictTime + predictionWindowSize;
bool willFailureOccur = predictFailure(predictTime, toTime);
// validate the prediction
bool hasFailureOccurred = isFailurePresent(predictTime, toTime);
// update counters
if (willFailureOccur) {
if (hasFailureOccurred) {
cout << "predicted correctly" << endl;
Tp++;
} else {
Fp++;
}
} else {
if (hasFailureOccurred) {
Fn++;
cout << "***** predicted incorrectly *****" << endl;
//cout << "count: " << count << " Tp: " << Tp << " Fp: " << Fp << " Tn: " << Tn;
//cout << " Fn: " << Fn << " assCount: " << assCount << " statCount: " << statCount;
//cout << " will: " << willFailureOccur << " has: " << hasFailureOccurred << " predictTime: " << predictTime << " toTime: " << toTime << endl;
} else {
Tn++;
}
}
cout << "count: " << count << " Tp: " << Tp << " Fp: " << Fp << " Tn: " << Tn;
cout << " Fn: " << Fn << " assCount: " << assCount << " statCount: " << statCount;
cout << " will: " << willFailureOccur << " has: " << hasFailureOccurred << " time: " << predictTime << endl;
// slide prediction timeWindow
//predictTime += predictionWindowSize;
//historyStartTime += predictionWindowSize;
predictTime += slideWindowSize;
historyStartTime += slideWindowSize;
}
Event* event = new Event(mRecId, mFacility, mSeverity, mEventTime, mCategory);
++count;
//cout << *event << endl;
eventHistory->addEvent(event);
}
}
inFile.close();
} else {
throw AppException(9, "unable to open Input file", __FILE__, __LINE__);
}
cout << "Total Events Processed: " << count << endl;
cout << "Tp: " << Tp << " Fp: " << Fp << " Tn: " << Tn << " Fn: " << Fn << endl;
float precision = (float)Tp / (Tp + Fp);
float recall = (float)Tp / (Tp + Fn);
cout << "Precision: " << precision << " Recall: " << recall << endl;
} catch (exception &e) {
throw AppException(9, string("abnormal error in processing Input file - ") + string(e.what()), __FILE__, __LINE__);
}
}
bool Predictor::isFailurePresent(int fromTime, int toTime) {
vector<Event*>::iterator itr;
for (itr = failureList->begin(); itr != failureList->end(); itr++) {
Event* event = *itr;
if (event->getEventTime() < fromTime) {
continue;
} else if (event->getEventTime() > toTime) {
return false;
} else {
cout << *event << endl;
return true;
}
}
return false;
}
bool Predictor::predictFailure(int fromTime, int toTime) {
int size = eventHistory->getEventListSize();
int lastFailTime = eventHistory->getLastFailTime();
int secondLastFailTime = eventHistory->getSecondLastFailTime();
if (size > 0) {
vector<Event*>* eventList = eventHistory->getEventList();
assCount++;
return (ar->predictFailure(eventList));
} else {
statCount++;
return (statsAnalyzer->predictFailure(lastFailTime, secondLastFailTime, toTime));
}
}
void Predictor::streamFailures(const char* fileName) {
int mEventTime;
string mMessage;
int count = 0;
int lastFailTime, secondLastFailTime, statCount, assCount;
secondLastFailTime = 0;
lastFailTime = 0;
int Tp = 0;
int Fp = 0;
int Fn = 0;
int Tn = 0;
try {
ifstream inFile(fileName, ios::in);
if (inFile.is_open()) {
//cout << "opened file" << endl;
while (!inFile.eof()) {
if (inFile >> mEventTime) {
getline(inFile, mMessage);
trim(mMessage);
if (mMessage.find(" ") == string::npos) {
//if (1 == 1) {
//call stats predictor
statCount++;
bool predictedFailure = statsAnalyzer->predictFailure(lastFailTime, secondLastFailTime, mEventTime);
if (predictedFailure) {
Tp++;
} else {
Fn++;
}
} else {
//call ar
assCount++;
string retVal = ar->getPredictionResult(mMessage);
if (retVal == "Tp") {
Tp++;
} else if (retVal == "Fp") {
Fp++;
} else if (retVal == "Fn") {
Fn++;
}
}
secondLastFailTime = lastFailTime;
lastFailTime = mEventTime;
//cout << "Tp: " << Tp << " Fp: " << Fp << " Tn: " << Tn << " Fn: " << Fn << endl;
}
count++;
}
inFile.close();
} else {
throw AppException(9, "unable to open Input file", __FILE__, __LINE__);
}
cout << "Total Events Processed: " << count << endl;
cout << "Tp: " << Tp << " Fp: " << Fp << " Tn: " << Tn << " Fn: " << Fn << endl;
float precision = (float)Tp / (Tp + Fp);
float recall = (float)Tp / (Tp + Fn);
//cout << "Precision: " << precision << " Recall: " << recall << endl;
cout << precision << " " << recall << endl;
} catch (exception &e) {
throw AppException(9, string("abnormal error in processing Input file - ") + string(e.what()), __FILE__, __LINE__);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -