// the.C #include #include #include #include #include "../Basics/pipeostream.h" #include "../Basics/basic.h" #include "../Basics/exception.h" #include "../Matrices/Matrix.h" #include "main_def.h" #include "pattern.h" #include "multiDS.h" static void PrintPosPatterns (const multiDS &training, const multiDS &testing, patterns &posPat, patterns &negPat, Matrix &one, Matrix &zero, ofstream &pat_file, int One) { Matrix train_stat(posPat.nbPatterns()+1, 4), test_stat (posPat.nbPatterns()+1, 4); training.printPatFile(posPat,negPat,One ? one : zero, One ? zero : one, pat_file,0, One ? 1 : 0,train_stat); testing.printPatFile (posPat,negPat,One ? one : zero, One ? zero : one, pat_file,1, One ? 1 : 0,test_stat); uint j; for (j = 0; j < 4; j++) pat_file << coefWidth(7) << train_stat (0, j); pat_file << " " ; for (j = 0; j < 4; j++) pat_file << coefWidth(7) << test_stat (0, j); pat_file << " Totals for points\n"; uint i; for (i=1, posPat.first(); !posPat.eol(); posPat.next(),i++) { for (j = 0; j < 4; j++) pat_file << coefWidth(7) << train_stat (i, j); pat_file << " " ; for (j = 0; j < 4; j++) pat_file << coefWidth(7) << test_stat (i, j); pat_file << " " << coefWidth(6) << posPat.weight() << " "; posPat.printPattern(pat_file); pat_file << endl; } } static void PrintPatterns (const multiDS &training, const multiDS &testing, patterns &posPat, patterns &negPat, Matrix &one, Matrix &zero, const char* outFile) { char fn [128]; strcat (strcpy (fn, outFile), ".patterns"); ofstream pat_file (fn); PrintPosPatterns (training, testing, posPat, negPat, one, zero, pat_file, 1); pat_file << endl << endl; PrintPosPatterns (training, testing, negPat, posPat, one, zero, pat_file, 0); } int main () { try { //------------------------------------------- //Collect general information for the batch //------------------------------------------- char inFile[80], patFile[80], outFile[80], choice[80]; // Declaration of all parameters, with default values float percFrom=10.0; float percTo=10.0; float percInter=1.0; int nbIter=1; int nbSizes=1; int initSeed=0; int iteration=0; int patReduce=-1; int weighting=1; boolean normalizeWP=true; boolean recenter=false; float errorTolerance=0.0; #if _batch_mode trace=0; #else cout<<"Trace level (1=normal, 2=debug): "; cin>>trace; #endif cout.setf(ios::fixed | ios::showpoint); cout<<"Prefix XXX of the files (.tra, .tes) containing the training data : "; cin>>inFile; cout<<"Prefix XXX of the files (.pos, .neg) with the patterns : "; cin>>patFile; cout<<"Prefix XXX for the result files : "; cin>>outFile; sprintf(choice,"%s-the.out",outFile); ofstream fout(choice); fout<<"Prefix XXX for the files (.tra) containing the training data : " <>percFrom; fout<>percTo; fout<>percInter; fout<>nbIter; fout<>iteration; fout<>initSeed; fout<>choice; patReduce=(choice[0]=='y'); fout<>patReduce; fout<>choice; patReduce = (choice[0]=='y' ? -1 : 0); fout<>weighting; fout<>choice; normalizeWP=(choice[0]=='y'); fout<>choice; recenter=(choice[0]=='y'); fout<>errorTolerance; fout< seed(nbSizes,nbIter); fixSeed(initSeed); if(singleExp) seed(0,0)= initSeed; else for (int size=0; size whole, training, testing; // Loop on the size int cSize; float perc; for (perc=percFrom, cSize=0; perc<=percTo; perc+=percInter, cSize++) { Matrix stat(1,14,0.0), statS(1,14,0.0), statSSQ(1,14,0.0), statMin(1,14,1E10), statMax(1,14,-1E10); flog<10 ? Iter%20==0 : (!Iter && (!cSize || (((cSize-1)*nbSizes)/20 != (cSize*nbSizes)/20))) ) { cout<<(fout<<= "Perc- |train-size--test-size| |#patterns| |-%-on-training-- --%-on-testing-- train-test|\n"); cout<<(fout<<= " -Iter |difP-difN---difP-difN| |-Pos-Neg-| |wrOn wrOff unkno wrOn wrOff unkno %tot %tot|\n"); } fixSeed(seed(cSize, Iter)); flog< training, testing; { char pref[80]; ostrstream buff(pref,80); buff< whole; sprintf(fn,"%s.tra",pref); fn>>whole; training.separate(whole); sprintf(fn,"%s.tes",pref); fn>>whole; testing.separate(whole); } stat(ii++)=training.ds[1].distinct(); stat(ii++)=training.ds[0].distinct(); stat(ii++)= testing.ds[1].distinct(); stat(ii++)= testing.ds[0].distinct(); // Read the patterns on file patterns posPat, negPat; { char pref[80]; ostrstream buff(pref,80); buff<>posPat; sprintf(fn,"%s.neg",pref); fn>>negPat; } // Reduction of patterns Matrix one (1,1,1); Matrix zero(1,1,0); if ( patReduce==-1 || patReduce==1 ) { training.cleanPatterns(posPat,one); training.cleanPatterns(negPat,zero); if ( trace>2 ) cout<< "\n*** positive patterns (after cleaning):\n" << posPat << "\n*** negative patterns (after cleaning):\n" << negPat; } if ( patReduce>0 ) { training.reducePatterns(posPat,one, patReduce); training.reducePatterns(negPat,zero,patReduce); if ( trace>2 ) cout<< "\n*** positive patterns (after reduction):\n" << posPat << "\n*** negative patterns (after reduction):\n" << negPat; } stat(ii++) = posPat.nbPatterns(); stat(ii++) = negPat.nbPatterns(); // Weight the patterns if ( trace>2 ) cout<< "\n*** weighting of positive patterns:\n"; training.weightPatterns(posPat,one,zero,weighting,normalizeWP); if ( trace>2 ) cout<< "\n*** weighting of negative patterns:\n"; training.weightPatterns(negPat,zero,one,weighting,normalizeWP); if ( recenter ) training.balanceWeightPatterns(posPat,negPat,one,zero); if ( trace>1 ) cout<< "\n*** positive patterns:\n" << posPat << "\n*** negative patterns:\n" << negPat; // evaluate performance of pseudo-Boolean function int totalTrain, undecidTrain, wronglyOnTrain, wronglyOffTrain; int totalTest, undecidTest, wronglyOnTest, wronglyOffTest; if ( !singleExp ) { totalTrain = training.nbErrors(posPat,negPat,errorTolerance, wronglyOnTrain,wronglyOffTrain, undecidTrain); totalTest = testing.nbErrors (posPat,negPat,errorTolerance, wronglyOnTest,wronglyOffTest, undecidTest); } else { char fn[80]; sprintf(fn,"%s.ptsTrain",outFile); totalTrain = training.nbErrors(posPat,negPat,errorTolerance, wronglyOnTrain,wronglyOffTrain, undecidTrain,fn); sprintf(fn,"%s.ptsTest",outFile); totalTest = testing.nbErrors (posPat,negPat,errorTolerance, wronglyOnTest, wronglyOffTest, undecidTest,fn); sprintf(fn,"%s.patterns",outFile); ofstream pat_file(fn); Matrix train_stat(posPat.nbPatterns()+1, 4), test_stat(posPat.nbPatterns()+1, 4); training.printPatFile(posPat,negPat,one,zero,pat_file,0,1,train_stat); testing.printPatFile(posPat,negPat,one,zero,pat_file,1,1,test_stat); int i=0,j; for(j=0;j<4;j++) pat_file << setw(6) << train_stat(0,j); pat_file << " "; for(j=0;j<4;j++) pat_file << setw(6) << test_stat(0,j); pat_file << " Totals for points\n"; for (posPat.first(); !posPat.eol(); posPat.next()) { ++i; for(j=0;j<4;j++) pat_file << setw(6) << train_stat(i,j); pat_file << " "; for(j=0;j<4;j++) pat_file << setw(6) << test_stat(i,j); pat_file << " " << setw(6) << posPat.weight() << " "; posPat.printPattern(pat_file); pat_file << "\n"; } pat_file << "\n\n"; train_stat.resize(negPat.nbPatterns()+1, 4); test_stat.resize(negPat.nbPatterns()+1, 4); training.printPatFile(negPat,posPat,zero,one,pat_file,0,0,train_stat); testing.printPatFile(negPat,posPat,zero,one,pat_file,1,0,test_stat); for(j=0;j<4;j++) pat_file << setw(6) << train_stat(0,j); pat_file << " "; for(j=0;j<4;j++) pat_file << setw(6) << test_stat(0,j); pat_file << " Totals for points\n"; i=0; for (negPat.first(); !negPat.eol(); negPat.next()) { ++i; for(j=0;j<4;j++) pat_file << setw(6) << train_stat(i,j); pat_file << " "; for(j=0;j<4;j++) pat_file << setw(6) << test_stat(i,j); pat_file << " " << setw(6) << negPat.weight() << " "; negPat.printPattern(pat_file); pat_file << "\n"; } } stat(ii++) = 100.0 * wronglyOnTrain / training.ds[0].card(); stat(ii++) = 100.0 * wronglyOffTrain / training.ds[1].card(); stat(ii++) = 100.0 * undecidTrain / training.card(); stat(ii++) = 100.0 * wronglyOnTest / testing.ds[0].card(); stat(ii++) = 100.0 * wronglyOffTest / testing.ds[1].card(); stat(ii++) = 100.0 * undecidTest / testing.card(); stat(ii++) = 100.0 * totalTrain / training.card(); stat(ii++) = 100.0 * totalTest / testing.card(); // Print the results cout<1 ) { cout<<" mean"<