Commit 7f982ea62aaac9de6a72455eb7e43ad484908594

Authored by Zhenghua Gong
1 parent c643e466ce
Exists in master

Updated MyOrder.cpp. So it can calculate factorial of 100.

Showing 1 changed file with 115 additions and 86 deletions   Show diff stats
... ... @@ -10,15 +10,23 @@
10 10 #include <boost/math/special_functions.hpp>
11 11 #include <boost/lexical_cast.hpp>
12 12 #include <boost/algorithm/string/replace.hpp>
  13 +#include <boost/multiprecision/cpp_int.hpp>
13 14  
14 15 #include <stdio.h>
15 16 #include <math.h>
16 17 #include <algorithm> // std::find
17 18 #include <map>
18 19 #include <iterator>
19   -using namespace std;
  20 +#include <bits/stdc++.h>
  21 +#include <tuple>
20 22  
  23 +using namespace std;
21 24  
  25 +//Function for calculating the binomial coefficient
  26 +boost::multiprecision::cpp_int BinomialCoefficient(unsigned int n, unsigned int k) {
  27 + if (k == 0) { return 1; }
  28 + else { return (n * BinomialCoefficient(n - 1, k - 1)) / k; }
  29 +}
22 30 struct compare {
23 31 bool operator()(const std::string& first, const std::string& second) {
24 32 if(first.size() == second.size())
... ... @@ -105,6 +113,13 @@ void findAndReplaceAll(std::string &amp; data, std::string toSearch, std::string rep
105 113 }
106 114 }
107 115  
  116 +string findMB(string structures, string variable){
  117 + string MB;
  118 +
  119 +
  120 + return MB;
  121 +}
  122 +
108 123 int main() {
109 124 /*
110 125 //TEMPORARY INPUT FILE
... ... @@ -225,15 +240,16 @@ int main() {
225 240 for (int col = 0; col<COLS; col++) {
226 241 file >> DAT[row][col]; // fill the row with col elements
227 242 }
228   - row++; // Keep track of actual row
  243 + row++; // Keep track of actual row
229 244 }
230 245 }
231 246 else cout << "Unable to open file" << endl;
232 247 file.close();
  248 + DAT.erase(DAT.end());
233 249  
234 250 size_t totvars = DAT[1].size(); //column number
235 251 size_t tottuples = DAT.size();//row number
236   -
  252 +
237 253  
238 254  
239 255  
... ... @@ -282,6 +298,11 @@ int main() {
282 298 std::cin >> PERCENT;
283 299 std::cout << endl;
284 300  
  301 + string filepath;
  302 + std::cout << "What is the output file name?: ";
  303 + std::cin >> filepath;
  304 + std::cout << endl;
  305 +
285 306 //Time that program begins
286 307 time_t start = time(0);
287 308 char* dt_start = ctime(&start);
... ... @@ -312,7 +333,7 @@ int main() {
312 333 std::cout << endl;
313 334  
314 335 //Total Families Ui,alpha for a particular variable in the order
315   - vector <unsigned long long> families;//is vector, first element is the number of parentset for the first variabel......
  336 + vector <boost::multiprecision::cpp_int> families;
316 337 for (unsigned int i = 0; i < totvars; ++i) {
317 338 int numparents = i;
318 339 if (numparents == 0) {
... ... @@ -320,7 +341,7 @@ int main() {
320 341 }
321 342 else {
322 343  
323   - unsigned long long numfams = 0;
  344 + boost::multiprecision::cpp_int numfams = 0;
324 345 for (unsigned int j = 0; j <= i; ++j) {
325 346 if (j <= maxparents) {
326 347 unsigned long long jFactorial = 1;
... ... @@ -344,7 +365,7 @@ int main() {
344 365 ijFactorial *= g;
345 366 }
346 367 }
347   - numfams += (iFactorial) / (jFactorial * ijFactorial);
  368 + numfams += BinomialCoefficient(i,j);
348 369 }
349 370 else {
350 371 break;
... ... @@ -354,7 +375,7 @@ int main() {
354 375 }
355 376 }
356 377  
357   -
  378 +
358 379 //How many parent combinations for each step? As well as there counts
359 380 vector< vector <int> > ParentCombos;
360 381 vector< vector <int> > fullNijkvector;
... ... @@ -364,7 +385,8 @@ int main() {
364 385 if (i == 0) {
365 386 vector <int> tmp,Nijkovercombos1;
366 387 vector <string> tempstring;
367   - tempstring.push_back("[0]");
  388 +// tempstring.push_back("[0]");
  389 + tempstring.push_back("[" + int_to_str(order[0])+ "]");
368 390 tmp.push_back(1);
369 391 ParentCombos.push_back(tmp);
370 392 //counting the amount of times that a value of the first variable in the order occurs
... ... @@ -387,7 +409,8 @@ int main() {
387 409 else {
388 410 vector <int> tmp,Nijkovercombos1;
389 411 vector <string> tempstring;
390   - string tempa = "[" + int_to_str(i) +"]";
  412 +// string tempa = "[" + int_to_str(i) +"]";
  413 + string tempa = "[" + int_to_str(order[i]) +"]";
391 414 tempstring.push_back(tempa);
392 415 tmp.push_back(1);
393 416 int numparnts = i;
... ... @@ -405,11 +428,12 @@ int main() {
405 428 Nijkovercombos1.push_back(Nijk1);
406 429 }
407 430 fullNijkvector.push_back(Nijkovercombos1);
408   -
  431 +
409 432 //j representing the number of parents
410 433 for (int it = 1; it <= numparnts; ++it) {
411 434 //(333)Creating a vector that uses the right combination
412   - double Nloopy = 0, NcolFactorial = 1, iFactorial = 1, NiFactorial = 1;
  435 + boost::multiprecision::cpp_int Nloopy = 0;
  436 + //, NcolFactorial = 1, iFactorial = 1, NiFactorial = 1;
413 437 /*std::cout << "This is for " << numparnts << " choose " << it << endl;
414 438 std::cout << "The iteration number is: " << it << endl;*/
415 439 //Accounting for the limit of parent quantity
... ... @@ -421,7 +445,7 @@ int main() {
421 445 for (int p = 0; p < it; ++p) {
422 446 NewMat[p] = 1;
423 447 }
424   - for (int g = 2; g <= numparnts; ++g) {
  448 + /*for (int g = 2; g <= numparnts; ++g) {
425 449 NcolFactorial *= g;
426 450 }
427 451 for (int g = 2; g <= it; ++g) {
... ... @@ -429,9 +453,9 @@ int main() {
429 453 }
430 454 for (int g = 2; g <= (numparnts - it); ++g) {
431 455 NiFactorial *= g;
432   - }
  456 + }*/
433 457 //Nloopy represents the result of numparnts choose i e.g. numparnts choose 1 equals numparnts
434   - Nloopy = NcolFactorial / (iFactorial * NiFactorial);
  458 + Nloopy = BinomialCoefficient(numparnts,it);
435 459 for (int iNloopy = 0; iNloopy < Nloopy; ++iNloopy) {
436 460 int combsparents = 1;
437 461 vector <int> parsetv;
... ... @@ -445,13 +469,16 @@ int main() {
445 469  
446 470 for (int par2 = 0; par2 < parsetv.size(); ++par2){
447 471 if(par2+1==parsetv.size()){
448   - tempstring2 = tempstring2 + int_to_str(parsetv[par2]);
  472 + // tempstring2 = tempstring2 + int_to_str(parsetv[par2]);
  473 + tempstring2 = tempstring2 + int_to_str(order[parsetv[par2]]);
449 474 //tempstring2 = tempstring2 +","+"|" + int_to_str(i);
450   - tempstring2 = "[" + int_to_str(i)+"|"+tempstring2 +"]";
  475 + // tempstring2 = "[" + int_to_str(i)+"|"+tempstring2 +"]";
  476 + tempstring2 = "[" + int_to_str(order[i])+"|"+tempstring2 +"]";
451 477 }
452 478 else{
453 479 //tempstring2 = tempstring2 + int_to_str(parsetv[par2])+",";
454   - tempstring2 = tempstring2 + int_to_str(parsetv[par2])+":";
  480 +// tempstring2 = tempstring2 + int_to_str(parsetv[par2])+":";
  481 + tempstring2 = tempstring2 + int_to_str(order[parsetv[par2]])+":";
455 482 }
456 483  
457 484  
... ... @@ -512,7 +539,7 @@ int main() {
512 539 vector <int> hvect;
513 540 //hvect tells us which variables are being considered always the last variable is being considered
514 541 //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
515   - //continued: A is the only one that is either a parent or isn't a parent so hvect will be < 0 1 >
  542 + //continued: A is the only one that is either a parent or isn't a parent so hvect will be < 0 1 >
516 543 //for A C hvect will be < 0 2 >
517 544 for (int h = 0; h < i; ++h) {
518 545 if (NewMat[h] == 1) {
... ... @@ -581,7 +608,7 @@ int main() {
581 608 //Nijkovercombos displays data as follows
582 609 //it starts with the smallest value for the last variable in hvect
583 610 //and the largest values in the first n-1 variables in hvect
584   - //max,max-1,max-2,max-3 e.g. 2, 1, 0, 2, 1, 0
  611 + //max,max-1,max-2,max-3 e.g. 2, 1, 0, 2, 1, 0
585 612 //count,count,count,count e.g. 13, 2, 2, 3, 4, 10
586 613 Nijkovercombos.push_back(Nijk);
587 614 //(666)Now that the values have been calculated find out what the next combination of variables should be
... ... @@ -619,7 +646,7 @@ int main() {
619 646 //If all except one of the 1's are found in the last it - 1 columns
620 647 for (int x = 0; x < numparnts; ++x) {
621 648 if (((x <= (NxtOne + SumOnes3)) & (x > NxtOne)) | (x == (FrstOne + 1))) {
622   - //If
  649 + //If
623 650 NewMat[x] = 1;
624 651 }
625 652 else {
... ... @@ -630,7 +657,7 @@ int main() {
630 657 else {
631 658 for (int x = 0; x < numparnts; ++x) {
632 659 if (((x <= (NxtOne + SumOnes3)) & (x > NxtOne)) | (x == FrstOne)) {
633   - //If the position is that of the first 1 or it falls between the changed number one and the total
  660 + //If the position is that of the first 1 or it falls between the changed number one and the total
634 661 //amount of ones that are on that side of the zero 10111
635 662 NewMat[x] = 1;
636 663 }
... ... @@ -773,7 +800,7 @@ int main() {
773 800  
774 801 //Calculate sumovUialpha based on the logsumexp concept
775 802 if (Uialpha + 1 == families[varinorder]) {
776   -
  803 +
777 804 for (size_t que = 0; que < vec2ndlastgamma.size(); ++que) {
778 805 //change the value of maxseclastgamma if new value is larger than the previous value
779 806 if (que == 0) {
... ... @@ -791,7 +818,7 @@ int main() {
791 818 //add info on parent set scores for each variable to this vector of vectors
792 819 vecvarparset.push_back(vec2ndlastgamma);
793 820 }
794   -
  821 +
795 822 /*std::cout << endl;
796 823 std::cout << seclastgamma;
797 824 std::cout << endl;*/
... ... @@ -872,85 +899,58 @@ int main() {
872 899  
873 900 sort(sortedStru.begin(),sortedStru.end(),compareDe);
874 901  
  902 + vector <double> strP;
  903 + double s = exp(bestScore);
875 904  
876   -
877   -/* for (unsigned l = 1; l< parSetScoreSorted.size(); ++l){ //l means each variable
878   - for (unsigned m = contl; m < parSetScoreSorted[l].size(); ++m){ //m means the number of parents sets
879   - contl =m;
880   - vector < pair <string, double > > delta; // store the different of the highest score and the second highest score
881   - for (unsigned i = l; i < parSetScoreSorted.size(); ++i){
882   - map <double, string> :: iterator itr0, itr1;
883   - double tempDeltaS;
884   - string tempDeltaL;
885   - itr0 = parSetScoreSorted[i].begin();
886   - itr1 = itr0;
887   - for (int kr = 0; kr < contl; ++kr){
888   - itr1 = itr0++;
889   - }
890   - tempDeltaS = (itr1->first)-(itr0->first);
891   - tempDeltaL = int_to_str(i);
892   - delta.push_back(make_pair(tempDeltaL, tempDeltaS));
893   - }
894   -
895   - sort(delta.begin(),delta.end(),compareI);
896   -
897   - double const stopLimit = PERCENT;
898   - double tempbeforeS = bestStrScore.first, temafterS;
899   -
900   - for (unsigned i = 0; i < delta.size(); ++i){
901   - pair <double, string > temppair;
902   - double tempLime;
903   - int ind = str_to_int(delta[i].first);
904   - string s = parSetScoreSorted[ind].begin()->second;
905   - string sRep = (++parSetScoreSorted[ind].begin())->second;
906   - temppair = bestStrScore;
907   - findAndReplaceAll(temppair.second, s, sRep);
908   - temppair.first = temppair.first - delta[i].second;
909   -
910   - temafterS = logAB(tempbeforeS, temppair.first);
911   -
912   - tempLime = persentageXofY(temafterS, tempbeforeS) ;
913   -
914   - tempbeforeS = temafterS;
915   -
916   - if(tempLime > stopLimit){
917   - goto finish;
918   - }
919   -
920   - sortedStru.push_back(temppair);
921   -
922   - // std::cout << ind << " "<< s << " "<< sRep << " "<< temppair.first << " " << temppair.second <<endl;
923   - // std::cout << temppair.first << " " << temppair.second <<endl;
924   - // std::cout << std::endl;
925   -
926   - }
927   -
928   - }
  905 + for(unsigned i=0; i< sortedStru.size(); ++i ){
  906 + double s0 = s;
  907 + s = s + exp(sortedStru[i].first);
  908 + double te = s0/s*100;
  909 + strP.push_back(te);
929 910  
930 911 }
931 912  
932   - finish:*/
933   -
  913 +/* double fi = s/exp(finlogscore)*100;
  914 + strP.push_back(fi);*/
934 915  
935 916 std::cout << std::endl;
936 917 std::cout << "Total Score: "<< boost::lexical_cast<string>(finlogscore) << std::endl;
937 918 std::cout << std::endl;
938 919  
939 920  
  921 + vector < pair <double, string> > bestorders;
  922 + double struPer = 0;
  923 +
940 924 cout << "Best several structures are:" << endl;
941 925 cout << bestScore << " : " << bestLable << endl;
942 926  
  927 + bestorders.push_back(make_pair(bestScore, bestLable));
  928 + struPer = struPer + exp(bestScore);
  929 +
943 930 for (unsigned i=0; i< sortedStru.size(); ++i )
944 931 {
945 932 cout << sortedStru[i].first << " : " << sortedStru[i].second << endl;
  933 + bestorders.push_back(make_pair(sortedStru[i].first, sortedStru[i].second));
  934 + struPer = struPer + exp(sortedStru[i].first);
  935 + if(strP[i+1] > PERCENT){
  936 + break;
  937 + }
  938 + }
  939 +
  940 +/* cout << "Best several structures are:" << endl;
  941 + cout << bestScore << " : " << bestLable << " " << endl;
  942 +
  943 + for (unsigned i=0; i< sortedStru.size(); ++i )
  944 + {
  945 + cout << sortedStru[i].first << " : " << sortedStru[i].second << " "<< endl;
946 946 }
947 947  
948 948 std::cout << std::endl;
949   - std::cout << std::endl;
  949 + std::cout << std::endl;*/
950 950  
951   - for(unsigned i=0; i< parSetScoreSorted.size(); ++i){
  951 +/* for(unsigned i=0; i< parSetScoreSorted.size(); ++i){
952 952 map <double, string> :: iterator itr;
953   - std::cout << "For variable "<< i <<":"<< endl;
  953 + std::cout << "For variable "<< order[i] <<":"<< endl;
954 954 for (itr = parSetScoreSorted[i].begin(); itr != parSetScoreSorted[i].end(); ++itr)
955 955 {
956 956 cout << itr->second << " : " << itr->first << "; ";
... ... @@ -958,23 +958,50 @@ int main() {
958 958 cout << endl;
959 959 cout << endl;
960 960  
  961 + }*/
  962 +
  963 +
  964 + string orderstring;
  965 + for (unsigned i=0; i< order.size(); ++i){
  966 + orderstring.append(int_to_str(order[i])+" ");
961 967 }
962 968  
  969 + ofstream myfile;
  970 + myfile.open( filepath.c_str());
963 971  
964   -/* ofstream myfile;
965   - myfile.open("/home/zgong001/Documents/SprinklerDataset/bestStructure.txt");
966 972 if (myfile.is_open())
967 973 {
968   - myfile << bestLable << " : " << bestScore << sortedStru.size();
  974 + myfile << "The order is:" << orderstring << " " << boost::lexical_cast<string>(finlogscore);
  975 + myfile << "\n";
  976 + myfile << bestLable << " " << bestScore << " " << exp(bestScore) << " "<< (exp(bestScore)/struPer)*100;
969 977 myfile << "\n";
970 978  
971 979 for(unsigned i=0; i< sortedStru.size(); ++i){
972   - myfile << sortedStru[i].second << " : " << sortedStru[i].first;
  980 + myfile << sortedStru[i].second << " " << sortedStru[i].first << " " << exp(sortedStru[i].first) << " "<< (exp(sortedStru[i].first)/struPer)*100;
973 981 myfile << "\n";
  982 + if(strP[i+1] > PERCENT){
  983 + break;
  984 + }
974 985 }
975 986 }
976 987 else cout << "Unable to open file";
977   - myfile.close();*/
  988 + myfile.close();
  989 +
  990 +/* ofstream myfile;
  991 + myfile.open("/home/zgong001/Documents/Alarm/D50S9v2/D50S9v2RO.txt");
  992 + if (myfile.is_open())
  993 + {
  994 + for(unsigned i=0; i< parSetScoreSorted.size(); ++i){
  995 + map <double, string> :: iterator itr;
  996 + for (itr = parSetScoreSorted[i].begin(); itr != parSetScoreSorted[i].end(); ++itr){
  997 + myfile << itr->second << " : " << itr->first << "; " ;
  998 + }
  999 + myfile << "\n";
  1000 +
  1001 + }
  1002 + }
  1003 + else cout << "Unable to open file";
  1004 + myfile.close();*/
978 1005  
979 1006  
980 1007 //time after completion
... ... @@ -991,3 +1018,5 @@ int main() {
991 1018 std::cin.get();
992 1019 return 0;
993 1020 }
  1021 +
  1022 +