00001 #include "main.h"
00002
00003 #ifdef NSGAII
00004 string emoa = "NSGA-II";
00005 #else
00006 string emoa = "SMS-EMOA";
00007 #endif
00008
00009 int nreal;
00010 int nbin;
00011 int nnom;
00012 int nobj;
00013 int* obj_direction;
00014 int ncon;
00015 int popsize;
00016 double pcross_real;
00017 double pcross_bin;
00018 double pcross_nom;
00019 double pmut_real;
00020 double pmut_bin;
00021 double pmut_nom;
00022 double eta_c;
00023 double eta_m;
00024 int ngen;
00025 int nbinmut;
00026 int nrealmut;
00027 int nnommut;
00028 int nbincross;
00029 int nrealcross;
00030 int nnomcross;
00031 int* nbits;
00032 double* min_realvar;
00033 double* max_realvar;
00034 double* min_binvar;
00035 double* max_binvar;
00036 int* min_nomvar;
00037 int* max_nomvar;
00038 int bitlength;
00039 double *nadir;
00040 double *ideal;
00041 int fixedNadirIdeal;
00042
00043 unsigned evaluations, sampleSetSize = 1;
00044 bool snapShots;
00045
00046 string ES, MOproblem;
00047 MOTestFunctions* testFunctions = NULL;
00048 WDN* network = NULL;
00049
00050 bool fileExists(string filename)
00051 {
00052 struct stat stFileInfo;
00053
00054 if (stat(filename.c_str(), &stFileInfo) == 0)
00055 return true;
00056
00057 return false;
00058 }
00059
00060
00061 void simulatorWrapper(double* xreal, double* xbin, int** gene, int* xnom, double* obj, double* constr)
00062 {
00063 if (ES == "TestFunctions_MIES")
00064 {
00065 if (MOproblem == "DTLZ1")
00066 testFunctions->DTLZ1(xreal, nreal);
00067 else if (MOproblem == "DTLZ2")
00068 testFunctions->DTLZ2(xreal, nreal);
00069 }
00070 else if (ES == "WDN_MIES")
00071 {
00072 #ifdef ROBUST
00073 network->robustSimulate(xnom, nnom);
00074 #else
00075 network->simulate(xnom, nnom);
00076 #endif
00077 }
00078
00079 vector<double>* F = (ES == "TestFunctions_MIES") ? testFunctions->getF() : network->getF();
00080 for (int i = 0; i < nobj; i++)
00081 obj[i] = F->at(i);
00082 }
00083
00084
00085 void allocateMemoryPop(population* P, int size)
00086 {
00087 P->ind = new individual[size];
00088 for (int i = 0; i < size; i++)
00089 allocateMemoryInd(&(P->ind[i]));
00090 }
00091
00092
00093 void allocateMemoryInd(individual* ind)
00094 {
00095 ind->xreal = (nreal != 0) ? new double[nreal] : NULL;
00096
00097 if (nbin != 0)
00098 {
00099 ind->xbin = new double[nbin];
00100 ind->gene = new int* [nbin];
00101 for (int i = 0; i < nbin; i++)
00102 ind->gene[i] = new int[nbits[i]];
00103 }
00104 else
00105 {
00106 ind->xbin = NULL;
00107 ind->gene = NULL;
00108 }
00109
00110 ind->xnom = (nnom != 0) ? new int[nnom] : NULL;
00111 ind->obj = new double[nobj];
00112 ind->constr = (ncon != 0) ? new double[ncon] : NULL;
00113 }
00114
00115
00116 void deallocateMemoryPop(population* P, int size)
00117 {
00118 for (int i = 0; i < size; i++)
00119 deallocateMemoryInd(&(P->ind[i]));
00120
00121 delete [] P->ind;
00122 }
00123
00124
00125 void deallocateMemoryInd(individual* ind)
00126 {
00127 delete [] ind->xreal;
00128 delete [] ind->xbin;
00129
00130 for (int i = 0; i < nbin; i++)
00131 delete [] ind->gene[i];
00132 delete [] ind->gene;
00133
00134 delete [] ind->xnom;
00135 delete [] ind->obj;
00136 delete [] ind->constr;
00137 }
00138
00139
00140 void writePop(double id, population* P, int currGen)
00141 {
00142 string filename;
00143 fstream outfile;
00144 if (snapShots)
00145 {
00146 unsigned evalsUsed;
00147
00148 #ifdef NSGAII
00149 evalsUsed = popsize + currGen * popsize;
00150 #else
00151 evalsUsed = popsize + currGen;
00152 #endif
00153
00154 #ifdef ROBUST
00155 # ifdef LHSR
00156 evalsUsed = network->getEvalsUsed();
00157 # else
00158 evalsUsed *= sampleSetSize;
00159 # endif
00160 #endif
00161
00162 if (evalsUsed % popsize != 0)
00163 return;
00164
00165 filename = "RunID_" + boost::lexical_cast<string>(id) + "_P_" + itos(evalsUsed) + ".txt";
00166 }
00167 else
00168 {
00169 if (currGen == 0)
00170 filename = "RunID_" + boost::lexical_cast<string>(id) + "_P_0.txt";
00171 else if (currGen == ngen)
00172 filename = "RunID_" + boost::lexical_cast<string>(id) + "_P.txt";
00173 else
00174 return;
00175 }
00176
00177 outfile.open(filename.c_str(), ios::out);
00178 for (int i = 0; i < popsize; i++)
00179 {
00180 outfile << ((P->ind[i].constr_violation == 0.0) ? "F" : "P" ) << "\t";
00181
00182 for (int j = 0; j < nobj; j++)
00183 outfile << P->ind[i].obj[j] << "\t";
00184
00185 outfile << "\t\t\t\t";
00186
00187 for (int j = 0; j < nreal; j++)
00188 outfile << P->ind[i].xreal[j] << "\t";
00189
00190 for (int j = 0; j < nbin; j++)
00191 {
00192 for (int m = 0; m < nbits[j]; m++)
00193 outfile << P->ind[i].gene[j][m] << "\t";
00194 }
00195
00196 for (int j = 0; j < nnom; j++)
00197 outfile << P->ind[i].xnom[j] << "\t";
00198
00199 outfile << endl << endl << endl;
00200 }
00201 outfile.close();
00202 }
00203
00204
00205 void writeLog(double id, time_t elapsed, int currGen, bool feedback)
00206 {
00207 if (snapShots)
00208 return;
00209
00210 string filename = "RunID_" + boost::lexical_cast<string>(id) + "_log.txt";
00211 fstream outfile(filename.c_str(), ios::out | ios::ate | ios::app);
00212
00213 if (currGen <= ngen)
00214 {
00215 outfile << emoa << "Generation " << currGen << ": \t" << elapsed << "s" << endl;
00216 }
00217 else
00218 {
00219 #ifdef LHSR
00220 if (ES == "WDN_MIES")
00221 cout << emoa << "Total number of evaluations used: " << network.getEvalsUsed() << endl;
00222 #endif
00223 cout << emoa << "Optimization took " << elapsed << "s" << endl << endl;
00224
00225 outfile << endl << emoa << "Total optimization: \t";
00226 #ifdef LHSR
00227 if (ES == "WDN_MIES")
00228 outfile << network.getEvalsUsed() << " evaluations, ";
00229 #endif
00230 outfile << elapsed << "s" << endl;
00231 }
00232
00233 outfile.close();
00234 }
00235
00236 int main(int argc, char* argv[])
00237 {
00238 if (argc < 2)
00239 {
00240 cerr << "No input file supplied! Exiting." << endl;
00241 return 1;
00242 }
00243
00244 #ifdef ROBUSTNESS_EVAL
00245 if (argc < 5)
00246 {
00247 cerr << "No pop file, RunID, and/or sample set size supplied for MC robustness evaluation! Exiting." << endl;
00248 return 1;
00249 }
00250 #endif
00251
00252
00253 #ifdef DP
00254 emoa += " dp";
00255 #endif
00256
00257 emoa += ": ";
00258
00259 char line[LEN];
00260 ifstream infile(argv[1]);
00261
00262
00263 vector<string> constSimParams;
00264 skipLinesUntil(infile, HEADER_CONSTANTS);
00265
00266 infile.getline(line, LEN);
00267 while (line[0] != '\0')
00268 {
00269 constSimParams.push_back(strtok(line, " ;"));
00270 findAndReplace(constSimParams.back(), SPACE, " ");
00271 infile.getline(line, LEN);
00272 }
00273
00274
00275 vector<string> varSimParamNames;
00276 skipLinesUntil(infile, HEADER_NAMES);
00277
00278 infile.getline(line, LEN);
00279 while (line[0] != '\0')
00280 {
00281 varSimParamNames.push_back(strtok(line, " ;"));
00282 infile.getline(line, LEN);
00283 }
00284
00285
00286 skipLinesUntil(infile, HEADER_MIES);
00287
00288 unsigned n_r = atoi(getValue(infile)),
00289 n_z = atoi(getValue(infile)),
00290 n_d = atoi(getValue(infile)),
00291 n_sigma_r = atoi(getValue(infile)),
00292 n_sigma_z = atoi(getValue(infile)),
00293 n_prob = atoi(getValue(infile)),
00294 n_f = atoi(getValue(infile)),
00295 mu = atoi(getValue(infile)),
00296 rho = atoi(getValue(infile)),
00297 kappa = atoi(getValue(infile)),
00298 lambda = atoi(getValue(infile));
00299
00300 vector<double> lBound_r, uBound_r;
00301 vector<int> lBound_z, uBound_z, lBound_d, uBound_d;
00302
00303 getBound(infile, &lBound_r, NULL, n_r);
00304 getBound(infile, &uBound_r, NULL, n_r);
00305
00306 getBound(infile, NULL, &lBound_z, n_z);
00307 getBound(infile, NULL, &uBound_z, n_z);
00308
00309 getBound(infile, NULL, &lBound_d, n_d);
00310 getBound(infile, NULL, &uBound_d, n_d);
00311
00312 double prob_min = atof(getValue(infile)),
00313 prob_max = atof(getValue(infile));
00314
00315 bool disRec_r = (atoi(getValue(infile)) == 0) ? false : true,
00316 disRec_z = (atoi(getValue(infile)) == 0) ? false : true,
00317 disRec_sigma_r = (atoi(getValue(infile)) == 0) ? false : true,
00318 disRec_sigma_z = (atoi(getValue(infile)) == 0) ? false : true,
00319 disRec_prob = (atoi(getValue(infile)) == 0) ? false : true,
00320 selfAdaptation = (atoi(getValue(infile)) == 0) ? false : true;
00321
00322 vector<int> direction;
00323 for (unsigned i = 0; i < n_f; i++)
00324 direction.push_back(atoi(getValue(infile)));
00325
00326 unsigned selectDimension = atoi(getValue(infile));
00327
00328 vector<unsigned> selectFunction;
00329 for (unsigned i = 0; i < selectDimension; i++)
00330 selectFunction.push_back(atoi(getValue(infile)));
00331
00332 evaluations = atoi(getValue(infile));
00333
00334 vector<double> optimalF;
00335 for (unsigned i = 0; i < n_f; i++)
00336 optimalF.push_back(atof(getValue(infile)));
00337
00338 double initialSigma_r = atof(getValue(infile)),
00339 initialSigma_z = atof(getValue(infile)),
00340 initialProb = atof(getValue(infile));
00341
00342 string initialPopFileName = getStringValue(infile);
00343 #ifdef ROBUSTNESS_EVAL
00344 initialPopFileName = argv[2];
00345
00346 optimalF[0] = atof(argv[3]);
00347 #endif
00348
00349 int randomSeed = atoi(getValue(infile));
00350
00351 #ifdef MIES
00352 int feedback = (atoi(getValue(infile)) == 1) ? true : false;
00353 #else
00354 int feedback = (atoi(getValue(infile)) == 0) ? false : true;
00355 #endif
00356
00357 ES = getStringValue(infile);
00358
00359 if (ES == "TestFunctions_MIES")
00360 MOproblem = getStringValue(infile);
00361
00362 bool excludeParent = (atoi(getValue(infile)) == 0) ? false : true;
00363
00364 double cvDemand, cvRoughness;
00365 if (ES == "WDN_MIES")
00366 {
00367 cvDemand = atof(getValue(infile));
00368 cvRoughness = atof(getValue(infile));
00369 sampleSetSize = atoi(getValue(infile));
00370
00371 #ifdef ROBUSTNESS_EVAL
00372 sampleSetSize = atoi(argv[4]);
00373 #endif
00374 }
00375
00376
00377 skipLinesUntil(infile, HEADER_SIMULATOR);
00378 string simulator = getStringValue(infile),
00379 path = getStringValue(infile),
00380 simInputFileName = getStringValue(infile),
00381 simOutputFileName = getStringValue(infile);
00382
00383 unsigned timeOutValue = 0;
00384
00385 infile.close();
00386
00387 findAndReplace(initialPopFileName, SPACE, " ");
00388 findAndReplace(MOproblem, SPACE, " ");
00389 findAndReplace(simulator, SPACE, " ");
00390 findAndReplace(path, SPACE, " ");
00391 findAndReplace(simInputFileName, SPACE, " ");
00392 findAndReplace(simOutputFileName, SPACE, " ");
00393
00394 ifstream* initialPopFile = (initialPopFileName == " ") ? NULL : new ifstream(initialPopFileName.c_str());
00395
00396 simulator = "\"\"\"" + simulator + "\" \"" + path + "\"\"\"";
00397
00398 snapShots = fileExists(SNAPSHOTS);
00399
00400 #ifdef MIES
00401 if (ES == "TestFunctions_MIES")
00402 {
00403 TestFunctions_MIES testMies(n_r, n_z, n_d, n_sigma_r, n_sigma_z, n_prob, n_f,
00404 mu, rho, kappa, lambda,
00405 lBound_r, uBound_r, lBound_z, uBound_z,
00406 lBound_d, uBound_d, prob_min, prob_max,
00407 disRec_r, disRec_z, disRec_sigma_r, disRec_sigma_z, disRec_prob,
00408 selfAdaptation, direction,
00409 evaluations, optimalF, initialSigma_r, initialSigma_z, initialProb, initialPopFile,
00410 randomSeed, feedback, timeOutValue,
00411 selectDimension, selectFunction,
00412 MOproblem, excludeParent);
00413 testMies.run();
00414 }
00415 else if (ES == "WDN_MIES")
00416 {
00417 WDN_MIES wdnMies(n_r, n_z, n_d, n_sigma_r, n_sigma_z, n_prob, n_f, mu, rho, kappa, lambda,
00418 lBound_r, uBound_r, lBound_z, uBound_z, lBound_d, uBound_d, prob_min, prob_max,
00419 disRec_r, disRec_z, disRec_sigma_r, disRec_sigma_z, disRec_prob,
00420 selfAdaptation, direction, evaluations, optimalF,
00421 initialSigma_r, initialSigma_z, initialProb,
00422 initialPopFile, randomSeed, feedback, snapShots,
00423 timeOutValue,
00424 simInputFileName, simOutputFileName, selectDimension, selectFunction,
00425 excludeParent,
00426 cvDemand, cvRoughness, sampleSetSize
00427 #ifdef ROBUSTNESS_EVAL
00428 , false
00429 #endif
00430 );
00431 wdnMies.run();
00432 }
00433 #else
00434 if (ES == "TestFunctions_MIES")
00435 testFunctions = new MOTestFunctions;
00436 else if (ES == "WDN_MIES")
00437 {
00438 network = new WDN(simInputFileName, simOutputFileName, cvDemand, cvRoughness, sampleSetSize);
00439
00440 if (network->isParallelExp())
00441 {
00442
00443 for (unsigned i = 0; i < n_d; i++)
00444 uBound_d[i]++;
00445 }
00446 }
00447
00448
00449 nreal = n_r;
00450 nbin = n_z;
00451 nnom = n_d;
00452 nobj = selectDimension;
00453 ncon = 0;
00454 popsize = mu;
00455 pcross_real = P_CROSS_R;
00456 pcross_bin = P_CROSS_B;
00457 pcross_nom = P_CROSS_N;
00458 pmut_real = 1/(double) nreal;
00459 pmut_bin = 1/(double) nbin;
00460 pmut_nom = 1/(double) nnom;
00461 eta_c = DIST_IDX_C;
00462 eta_m = DIST_IDX_M;
00463 nbinmut = 0;
00464 nrealmut = 0;
00465 nnommut = 0;
00466 nbincross = 0;
00467 nrealcross = 0;
00468 nnomcross = 0;
00469 fixedNadirIdeal = 0;
00470
00471 population* P = new population,
00472 * O = new population,
00473 * Q = new population;
00474
00475
00476 randGenerator.seed(static_cast<unsigned int>((randomSeed == -1) ? time(NULL) : randomSeed));
00477 seed = uniReal() * 0.9 + 0.05;
00478
00479 if (popsize < 4 || popsize % 4 != 0)
00480 {
00481 cerr << "Population size should be a multiple of 4! Exiting." << endl;
00482 return 1;
00483 }
00484
00485
00486 #ifndef ROBUST
00487 sampleSetSize = 1;
00488 #endif
00489
00490 if (evaluations > 0)
00491 #ifdef NSGAII
00492 ngen = (evaluations - popsize * sampleSetSize) / (popsize * sampleSetSize);
00493 #else
00494 ngen = (evaluations - popsize * sampleSetSize) / sampleSetSize;
00495 #endif
00496 else
00497 {
00498 cerr << "Supplied value for mumber of evaluations is " << evaluations << ", but should be larger than 0! Exiting." << endl;
00499 exit(1);
00500 }
00501
00502 obj_direction = (int*) malloc(nobj * sizeof(int));
00503 for (int i = 0; i < nobj; i++)
00504 obj_direction[i] = direction[i];
00505
00506 if (nreal != 0)
00507 {
00508 min_realvar = (double*) malloc(nreal * sizeof(double));
00509 max_realvar = (double*) malloc(nreal * sizeof(double));
00510
00511 for (int i = 0; i < nreal; i++)
00512 {
00513 min_realvar[i] = lBound_r[i];
00514 max_realvar[i] = uBound_r[i];
00515 }
00516 }
00517
00518 if (nbin != 0)
00519 {
00520 nbits = (int*) malloc(nbin * sizeof(int));
00521 min_binvar = (double*) malloc(nbin * sizeof(double));
00522 max_binvar = (double*) malloc(nbin * sizeof(double));
00523 bitlength = 0;
00524
00525 for (int i = 0; i < nbin; i++)
00526 {
00527 min_binvar[i] = lBound_z[i];
00528 max_binvar[i] = uBound_z[i];
00529
00530 nbits[i] = (int) ceil(pow(uBound_z[i] - lBound_z[i], 0.5));
00531 bitlength += nbits[i];
00532 }
00533 }
00534
00535 if (nnom != 0)
00536 {
00537 min_nomvar = (int*) malloc(nnom * sizeof(int));
00538 max_nomvar = (int*) malloc(nnom * sizeof(int));
00539
00540 for (int i = 0; i < nnom; i++)
00541 {
00542 min_nomvar[i] = lBound_d[i];
00543 max_nomvar[i] = uBound_d[i];
00544 }
00545 }
00546
00547 test_problem = &simulatorWrapper;
00548
00549 allocateMemoryPop(P, popsize);
00550
00551 #ifdef NSGAII
00552 allocateMemoryPop(O, popsize);
00553 allocateMemoryPop(Q, 2*popsize);
00554 #else
00555 individual* q = new individual;
00556 allocateMemoryInd(q);
00557 #endif
00558
00559 if (ES == "WDN_MIES")
00560 {
00561 fixedNadirIdeal = 0;
00562
00563 vector<double> nadir_, ideal_;
00564 network->getNadirIdeal(nadir_, ideal_, nnom);
00565
00566 nadir = new double[3];
00567 ideal = new double[3];
00568 for (int i = 0; i < 3; i++)
00569 {
00570 nadir[i] = nadir_[i];
00571 ideal[i] = ideal_[i];
00572 }
00573
00574 #ifdef SMSEMOA3D
00575 setNadirIdeal(nadir, ideal);
00576 #endif
00577 }
00578
00579 time_t start = time(NULL);
00580 time_t startGen = start;
00581
00582 randomize();
00583
00584 if (feedback)
00585 cout << endl << emoa << "Generating initial generation" << endl;
00586
00587 #ifdef ROBUST
00588 if (ES == "WDN_MIES")
00589 network->generateSampleSet();
00590 #endif
00591 initialize_pop(P);
00592 decode_pop(P);
00593 evaluate_pop(P);
00594 #ifdef NSGAII
00595 assign_rank_and_crowding_distance(P);
00596 #endif
00597
00598 writePop(seed, P, 0);
00599 time_t elapsed = time(NULL) - startGen;
00600 writeLog(seed, elapsed, 0, feedback);
00601
00602 for (int i = 1; i <= ngen; i++)
00603 {
00604 startGen = time(NULL);
00605
00606 if (feedback && (i % REPORT_WINDOW == 1 || i == ngen))
00607 cout << endl << emoa << "At generation " << i << "/" << ngen << endl;
00608
00609 #ifdef ROBUST
00610 if (ES == "WDN_MIES")
00611 network->generateSampleSet();
00612 #endif
00613
00614 #ifdef NSGAII
00615
00616 selection(P, O);
00617 mutation_pop(O);
00618 decode_pop(O);
00619 evaluate_pop(O);
00620 merge(P, O, Q);
00621 fill_nondominated_sort(Q, P);
00622 #else
00623
00624 generate(P, q);
00625 reduce(P, q);
00626 #endif
00627
00628 writePop(seed, P, i);
00629 elapsed = time(NULL) - startGen;
00630 writeLog(seed, elapsed, i, feedback);
00631
00632 #ifdef LHSR
00633 if (ES == "WDN_MIES")
00634 ngen = network->updateTotalGen(ngen, currGen, evaluations, lambda);
00635 #endif
00636 }
00637
00638 elapsed = time(NULL) - start;
00639 writeLog(seed, elapsed, ngen+1, feedback);
00640
00641 if (ES == "WDN_MIES")
00642 {
00643 cout << "IdealTP: " << endl;
00644 for (int i = 0; i < 3; i++)
00645 cout << "F" << i << " = " << ideal[i] << endl;
00646 cout << endl;
00647
00648 cout << "BoundTP: " << endl;
00649 for (int i = 0; i < 3; i++)
00650 cout << "F" << i << " = " << nadir[i] << endl;
00651 cout << endl;
00652
00653 delete [] nadir;
00654 delete [] ideal;
00655 }
00656
00657 if (nreal != 0)
00658 {
00659 free(min_realvar);
00660 free(max_realvar);
00661 }
00662
00663 if (nbin != 0)
00664 {
00665 free(min_binvar);
00666 free(max_binvar);
00667 free(nbits);
00668 }
00669
00670 if (nnom != 0)
00671 {
00672 free(min_nomvar);
00673 free(max_nomvar);
00674 }
00675
00676 deallocateMemoryPop(P, popsize);
00677 delete P;
00678
00679 #ifdef NSGAII
00680 deallocateMemoryPop(O, popsize);
00681 delete O;
00682 deallocateMemoryPop(Q, 2*popsize);
00683 delete Q;
00684 #else
00685 deallocateMemoryInd(q);
00686 delete q;
00687 #endif
00688
00689 delete testFunctions;
00690 delete network;
00691 #endif
00692
00693 return 0;
00694 }
00695