#include "tsp.h" /////////////////////////////////////////////////////////////////////////// // Spracovanie riadka vstupneho suboru v tvare: // pocet_opakovani // metoda_zihania -- 0 = zakladne, 1 = elitarske, 2 = paralelne // metoda_reprezentacie -- 0 = permutacia, 1 = vektor // perioda_reportu -- 0 = len na konci // typ_reportu -- 1 = minimalna hodnota, 10 = hodnoty makroskopickych // velicin, 100 = grafy makroskopickych velicin, // 1000 = graf riesenia, 10000 = & separated, // 100000 = medzi makroskopickymi hodnotami nevypisuj teplotu // velkost_mriezky // pociatocna_teplota // koncova_teplota // pokles_teploty // pocet_iteracii_metropolisa // pravdepodobnost_elementarnej_mutacie // pre paralelne zihanie: // pocet_paralene_zihanych_vektorov // pravdepodobnost_krizenia // void doReport(int repType, GenerReport &rep); //forward #define rtMIN_VAL 0x01 #define rtMACRO_VAL 0x02 #define rtMACRO_GRAPH 0x04 #define rtMIN_GRAPH 0x08 #define rtAMPER_SEP 0x10 #define rtNO_TEMP 0x20 #define IFAR(th,el) (((repType & rtAMPER_SEP) != 0)? cout<< th: cout<< el) #define IFARTHENA(el) IFAR(" & ",el) #define IFARTHENNIL(el) IFAR("",el) bool processInput() { int repeatCnt, meth, repres, gridSize; double tMin, tMax, pMut, anF; int repPeriod, repType, rt, i, kMax; int poolSize; double crossProb; cin >> repeatCnt >> meth >> repres >> repPeriod >> repType >> gridSize >> tMax >> tMin >> anF >> kMax >> pMut; if(meth == 2) cin >> poolSize >> crossProb; if(cin.fail() || cin.bad()) { cout << "!!! FAILED TO READ INPUT !!!"; return false; } rt = 0; i = 1; while(repType > 0) { if(repType % 10 == 1) rt |= i; i <<= 1; repType /= 10; } repType = rt; IFAR("\\hline%","-------") << "----------------------------------------------------------------------" << endl; IFARTHENNIL("Method: ") << meth; IFARTHENA(" Representation: ") << repres; IFARTHENNIL(endl); IFARTHENA("Grid size: ") << gridSize; IFARTHENNIL(endl); IFARTHENA("Temperatures: maximal ") << tMax; IFARTHENA(", minimal ") << tMin; IFARTHENNIL(endl); IFARTHENA("Annealing factor: ") << anF; IFARTHENNIL(endl); IFARTHENA("Metropolis iterations: ") << kMax; IFARTHENA(" Probability of mutation: ") << pMut; IFARTHENNIL(endl); if(meth == 2) { IFARTHENA("Vector-pool size: ") << poolSize; IFARTHENA(" Probability of crossing: ") << crossProb; IFARTHENNIL(endl); } while (repeatCnt > 0) { switch(meth) { case 0://Zakladne zihanie switch(repres) { case 0: { TSPperm tmplt(gridSize, pMut); SATSPperm sa(tMin,tMax,kMax,anF,repPeriod); sa.anneal(tmplt); doReport(repType, *(sa.getReport())); } break; case 1: { TSPvect tmplt(gridSize, pMut); SATSPvect sa(tMin,tMax,kMax,anF,repPeriod); sa.anneal(tmplt); doReport(repType, *(sa.getReport())); } break; default: cout << "!!! Unknown representation!" << endl; } break;//meth=0 case 1://Elitarske zihanie switch(repres) { case 0: { TSPperm tmplt(gridSize, pMut); ESATSPperm sa(tMin,tMax,kMax,anF,repPeriod); sa.anneal(tmplt); doReport(repType, *(sa.getReport())); } break; case 1: { TSPvect tmplt(gridSize, pMut); ESATSPvect sa(tMin,tMax,kMax,anF,repPeriod); sa.anneal(tmplt); doReport(repType, *(sa.getReport())); } break; default: cout << "!!! Unknown representation!" << endl; } break;//meth=1 case 2://Paralelne zihanie switch(repres) { case 0: { TSPperm tmplt(gridSize, pMut); PSATSPperm sa(tMin,tMax,kMax,anF,repPeriod,crossProb,poolSize); sa.anneal(tmplt); doReport(repType, *(sa.getReport())); } break; case 1: { TSPvect tmplt(gridSize, pMut); PSATSPvect sa(tMin,tMax,kMax,anF,repPeriod,crossProb,poolSize); sa.anneal(tmplt); doReport(repType, *(sa.getReport())); } break; default: cout << "!!! Unknown representation !!!" << endl; } break;//meth=2 default: cout << "!!! Unknown annealing method !!!" << endl; }//switch(meth) repeatCnt--; }//while(repeatCnt>0) IFAR("\\\\",endl)<< endl; return true; } /////////////////////////////////////////////////////////////////////////// // typ_reportu -- 1 = minimalna hodnota, 10 = hodnoty makroskopickych // velicin, 100 = grafy makroskopickych velicin, // 1000 = graf riesenia, 10000 = & separated, // 100000 = medzi makroskopickymi hodnotami nevypisuj teplotu //#define rtMIN_VAL 0x01 //#define rtMACRO_VAL 0x02 //#define rtMACRO_GRAPH 0x04 //#define rtMIN_GRAPH 0x08 //#define rtAMPER_SEP 0x10 //#define rtNO_TEMP 0x20 //#define IFARTHENA(x) (((repType & rtAMPER_SEP) == 1)?x:" & ") void doReport(int repType, GenerReport &rep) { int cnt = rep.numOfRecords(); int i, v; double minV; if((repType & (rtMIN_VAL | rtMACRO_VAL | rtMIN_GRAPH)) != 0) { for(i = 0; i < cnt; i++) { if((repType & rtMIN_VAL) != 0) { minV = rep.getPurpFVal(i); IFARTHENA("Minimal value: ") << minV; IFARTHENNIL(endl); }//rtMIN_VAL if((repType & rtMACRO_VAL) != 0) { cout << setprecision(15) << resetiosflags(ios::fixed); if((repType & rtNO_TEMP) == 0) IFARTHENA("Temperature: ") << rep.getTemp(i); for(v = 0; v < mvCNT; v++) { IFARTHENA(mvName[v] << ": ") << rep.getMacroVal(i,v); IFARTHENNIL(endl); } cout << endl; // bez ohladu na &-separated format vystupu }//rtMACRO_VAL if((repType & rtMIN_GRAPH) != 0) { rep.printVector(i,cout); cout << endl; }//rtMIN_GRAPH }//for(i=0...) }//if(rtMIN_VAL||rtMACRO_VAL||rtMIN_GRAPH) if((repType & rtMACRO_GRAPH) != 0) { rep.printGraph(cout,6,4); } }//doReport /////////////////////////////////////////////////////////////////////////// // Hlavna slucka citania vstupneho suboru void main() { char c; bool ok; int cnt = 0; srand(time(NULL)); ok = true; cerr<<"Processed input lines: "<