diff --git a/MyOrder.cpp b/MyOrder.cpp index b173a9d..9fe3047 100644 --- a/MyOrder.cpp +++ b/MyOrder.cpp @@ -10,15 +10,23 @@ #include #include #include +#include #include #include #include // std::find #include #include -using namespace std; +#include +#include +using namespace std; +//Function for calculating the binomial coefficient +boost::multiprecision::cpp_int BinomialCoefficient(unsigned int n, unsigned int k) { + if (k == 0) { return 1; } + else { return (n * BinomialCoefficient(n - 1, k - 1)) / k; } +} struct compare { bool operator()(const std::string& first, const std::string& second) { if(first.size() == second.size()) @@ -105,6 +113,13 @@ void findAndReplaceAll(std::string & data, std::string toSearch, std::string rep } } +string findMB(string structures, string variable){ + string MB; + + + return MB; +} + int main() { /* //TEMPORARY INPUT FILE @@ -225,15 +240,16 @@ int main() { for (int col = 0; col> DAT[row][col]; // fill the row with col elements } - row++; // Keep track of actual row + row++; // Keep track of actual row } } else cout << "Unable to open file" << endl; file.close(); + DAT.erase(DAT.end()); size_t totvars = DAT[1].size(); //column number size_t tottuples = DAT.size();//row number - + @@ -282,6 +298,11 @@ int main() { std::cin >> PERCENT; std::cout << endl; + string filepath; + std::cout << "What is the output file name?: "; + std::cin >> filepath; + std::cout << endl; + //Time that program begins time_t start = time(0); char* dt_start = ctime(&start); @@ -312,7 +333,7 @@ int main() { std::cout << endl; //Total Families Ui,alpha for a particular variable in the order - vector families;//is vector, first element is the number of parentset for the first variabel...... + vector families; for (unsigned int i = 0; i < totvars; ++i) { int numparents = i; if (numparents == 0) { @@ -320,7 +341,7 @@ int main() { } else { - unsigned long long numfams = 0; + boost::multiprecision::cpp_int numfams = 0; for (unsigned int j = 0; j <= i; ++j) { if (j <= maxparents) { unsigned long long jFactorial = 1; @@ -344,7 +365,7 @@ int main() { ijFactorial *= g; } } - numfams += (iFactorial) / (jFactorial * ijFactorial); + numfams += BinomialCoefficient(i,j); } else { break; @@ -354,7 +375,7 @@ int main() { } } - + //How many parent combinations for each step? As well as there counts vector< vector > ParentCombos; vector< vector > fullNijkvector; @@ -364,7 +385,8 @@ int main() { if (i == 0) { vector tmp,Nijkovercombos1; vector tempstring; - tempstring.push_back("[0]"); +// tempstring.push_back("[0]"); + tempstring.push_back("[" + int_to_str(order[0])+ "]"); tmp.push_back(1); ParentCombos.push_back(tmp); //counting the amount of times that a value of the first variable in the order occurs @@ -387,7 +409,8 @@ int main() { else { vector tmp,Nijkovercombos1; vector tempstring; - string tempa = "[" + int_to_str(i) +"]"; +// string tempa = "[" + int_to_str(i) +"]"; + string tempa = "[" + int_to_str(order[i]) +"]"; tempstring.push_back(tempa); tmp.push_back(1); int numparnts = i; @@ -405,11 +428,12 @@ int main() { Nijkovercombos1.push_back(Nijk1); } fullNijkvector.push_back(Nijkovercombos1); - + //j representing the number of parents for (int it = 1; it <= numparnts; ++it) { //(333)Creating a vector that uses the right combination - double Nloopy = 0, NcolFactorial = 1, iFactorial = 1, NiFactorial = 1; + boost::multiprecision::cpp_int Nloopy = 0; + //, NcolFactorial = 1, iFactorial = 1, NiFactorial = 1; /*std::cout << "This is for " << numparnts << " choose " << it << endl; std::cout << "The iteration number is: " << it << endl;*/ //Accounting for the limit of parent quantity @@ -421,7 +445,7 @@ int main() { for (int p = 0; p < it; ++p) { NewMat[p] = 1; } - for (int g = 2; g <= numparnts; ++g) { + /*for (int g = 2; g <= numparnts; ++g) { NcolFactorial *= g; } for (int g = 2; g <= it; ++g) { @@ -429,9 +453,9 @@ int main() { } for (int g = 2; g <= (numparnts - it); ++g) { NiFactorial *= g; - } + }*/ //Nloopy represents the result of numparnts choose i e.g. numparnts choose 1 equals numparnts - Nloopy = NcolFactorial / (iFactorial * NiFactorial); + Nloopy = BinomialCoefficient(numparnts,it); for (int iNloopy = 0; iNloopy < Nloopy; ++iNloopy) { int combsparents = 1; vector parsetv; @@ -445,13 +469,16 @@ int main() { for (int par2 = 0; par2 < parsetv.size(); ++par2){ if(par2+1==parsetv.size()){ - tempstring2 = tempstring2 + int_to_str(parsetv[par2]); + // tempstring2 = tempstring2 + int_to_str(parsetv[par2]); + tempstring2 = tempstring2 + int_to_str(order[parsetv[par2]]); //tempstring2 = tempstring2 +","+"|" + int_to_str(i); - tempstring2 = "[" + int_to_str(i)+"|"+tempstring2 +"]"; + // tempstring2 = "[" + int_to_str(i)+"|"+tempstring2 +"]"; + tempstring2 = "[" + int_to_str(order[i])+"|"+tempstring2 +"]"; } else{ //tempstring2 = tempstring2 + int_to_str(parsetv[par2])+","; - tempstring2 = tempstring2 + int_to_str(parsetv[par2])+":"; +// tempstring2 = tempstring2 + int_to_str(parsetv[par2])+":"; + tempstring2 = tempstring2 + int_to_str(order[parsetv[par2]])+":"; } @@ -512,7 +539,7 @@ int main() { vector hvect; //hvect tells us which variables are being considered always the last variable is being considered //e.g if ABC is our order and we are on i equals 1 then we are looking at relationships between A and B only - //continued: A is the only one that is either a parent or isn't a parent so hvect will be < 0 1 > + //continued: A is the only one that is either a parent or isn't a parent so hvect will be < 0 1 > //for A C hvect will be < 0 2 > for (int h = 0; h < i; ++h) { if (NewMat[h] == 1) { @@ -581,7 +608,7 @@ int main() { //Nijkovercombos displays data as follows //it starts with the smallest value for the last variable in hvect //and the largest values in the first n-1 variables in hvect - //max,max-1,max-2,max-3 e.g. 2, 1, 0, 2, 1, 0 + //max,max-1,max-2,max-3 e.g. 2, 1, 0, 2, 1, 0 //count,count,count,count e.g. 13, 2, 2, 3, 4, 10 Nijkovercombos.push_back(Nijk); //(666)Now that the values have been calculated find out what the next combination of variables should be @@ -619,7 +646,7 @@ int main() { //If all except one of the 1's are found in the last it - 1 columns for (int x = 0; x < numparnts; ++x) { if (((x <= (NxtOne + SumOnes3)) & (x > NxtOne)) | (x == (FrstOne + 1))) { - //If + //If NewMat[x] = 1; } else { @@ -630,7 +657,7 @@ int main() { else { for (int x = 0; x < numparnts; ++x) { if (((x <= (NxtOne + SumOnes3)) & (x > NxtOne)) | (x == FrstOne)) { - //If the position is that of the first 1 or it falls between the changed number one and the total + //If the position is that of the first 1 or it falls between the changed number one and the total //amount of ones that are on that side of the zero 10111 NewMat[x] = 1; } @@ -773,7 +800,7 @@ int main() { //Calculate sumovUialpha based on the logsumexp concept if (Uialpha + 1 == families[varinorder]) { - + for (size_t que = 0; que < vec2ndlastgamma.size(); ++que) { //change the value of maxseclastgamma if new value is larger than the previous value if (que == 0) { @@ -791,7 +818,7 @@ int main() { //add info on parent set scores for each variable to this vector of vectors vecvarparset.push_back(vec2ndlastgamma); } - + /*std::cout << endl; std::cout << seclastgamma; std::cout << endl;*/ @@ -872,85 +899,58 @@ int main() { sort(sortedStru.begin(),sortedStru.end(),compareDe); + vector strP; + double s = exp(bestScore); - -/* for (unsigned l = 1; l< parSetScoreSorted.size(); ++l){ //l means each variable - for (unsigned m = contl; m < parSetScoreSorted[l].size(); ++m){ //m means the number of parents sets - contl =m; - vector < pair > delta; // store the different of the highest score and the second highest score - for (unsigned i = l; i < parSetScoreSorted.size(); ++i){ - map :: iterator itr0, itr1; - double tempDeltaS; - string tempDeltaL; - itr0 = parSetScoreSorted[i].begin(); - itr1 = itr0; - for (int kr = 0; kr < contl; ++kr){ - itr1 = itr0++; - } - tempDeltaS = (itr1->first)-(itr0->first); - tempDeltaL = int_to_str(i); - delta.push_back(make_pair(tempDeltaL, tempDeltaS)); - } - - sort(delta.begin(),delta.end(),compareI); - - double const stopLimit = PERCENT; - double tempbeforeS = bestStrScore.first, temafterS; - - for (unsigned i = 0; i < delta.size(); ++i){ - pair temppair; - double tempLime; - int ind = str_to_int(delta[i].first); - string s = parSetScoreSorted[ind].begin()->second; - string sRep = (++parSetScoreSorted[ind].begin())->second; - temppair = bestStrScore; - findAndReplaceAll(temppair.second, s, sRep); - temppair.first = temppair.first - delta[i].second; - - temafterS = logAB(tempbeforeS, temppair.first); - - tempLime = persentageXofY(temafterS, tempbeforeS) ; - - tempbeforeS = temafterS; - - if(tempLime > stopLimit){ - goto finish; - } - - sortedStru.push_back(temppair); - - // std::cout << ind << " "<< s << " "<< sRep << " "<< temppair.first << " " << temppair.second <(finlogscore) << std::endl; std::cout << std::endl; + vector < pair > bestorders; + double struPer = 0; + cout << "Best several structures are:" << endl; cout << bestScore << " : " << bestLable << endl; + bestorders.push_back(make_pair(bestScore, bestLable)); + struPer = struPer + exp(bestScore); + for (unsigned i=0; i< sortedStru.size(); ++i ) { cout << sortedStru[i].first << " : " << sortedStru[i].second << endl; + bestorders.push_back(make_pair(sortedStru[i].first, sortedStru[i].second)); + struPer = struPer + exp(sortedStru[i].first); + if(strP[i+1] > PERCENT){ + break; + } + } + +/* cout << "Best several structures are:" << endl; + cout << bestScore << " : " << bestLable << " " << endl; + + for (unsigned i=0; i< sortedStru.size(); ++i ) + { + cout << sortedStru[i].first << " : " << sortedStru[i].second << " "<< endl; } std::cout << std::endl; - std::cout << std::endl; + std::cout << std::endl;*/ - for(unsigned i=0; i< parSetScoreSorted.size(); ++i){ +/* for(unsigned i=0; i< parSetScoreSorted.size(); ++i){ map :: iterator itr; - std::cout << "For variable "<< i <<":"<< endl; + std::cout << "For variable "<< order[i] <<":"<< endl; for (itr = parSetScoreSorted[i].begin(); itr != parSetScoreSorted[i].end(); ++itr) { cout << itr->second << " : " << itr->first << "; "; @@ -958,23 +958,50 @@ int main() { cout << endl; cout << endl; + }*/ + + + string orderstring; + for (unsigned i=0; i< order.size(); ++i){ + orderstring.append(int_to_str(order[i])+" "); } + ofstream myfile; + myfile.open( filepath.c_str()); -/* ofstream myfile; - myfile.open("/home/zgong001/Documents/SprinklerDataset/bestStructure.txt"); if (myfile.is_open()) { - myfile << bestLable << " : " << bestScore << sortedStru.size(); + myfile << "The order is:" << orderstring << " " << boost::lexical_cast(finlogscore); + myfile << "\n"; + myfile << bestLable << " " << bestScore << " " << exp(bestScore) << " "<< (exp(bestScore)/struPer)*100; myfile << "\n"; for(unsigned i=0; i< sortedStru.size(); ++i){ - myfile << sortedStru[i].second << " : " << sortedStru[i].first; + myfile << sortedStru[i].second << " " << sortedStru[i].first << " " << exp(sortedStru[i].first) << " "<< (exp(sortedStru[i].first)/struPer)*100; myfile << "\n"; + if(strP[i+1] > PERCENT){ + break; + } } } else cout << "Unable to open file"; - myfile.close();*/ + myfile.close(); + +/* ofstream myfile; + myfile.open("/home/zgong001/Documents/Alarm/D50S9v2/D50S9v2RO.txt"); + if (myfile.is_open()) + { + for(unsigned i=0; i< parSetScoreSorted.size(); ++i){ + map :: iterator itr; + for (itr = parSetScoreSorted[i].begin(); itr != parSetScoreSorted[i].end(); ++itr){ + myfile << itr->second << " : " << itr->first << "; " ; + } + myfile << "\n"; + + } + } + else cout << "Unable to open file"; + myfile.close();*/ //time after completion @@ -991,3 +1018,5 @@ int main() { std::cin.get(); return 0; } + +