• Main Page
  • Namespaces
  • Classes
  • Files
  • File List
  • File Members

WDN_MIES.cpp

Go to the documentation of this file.
00001 #include "WDN_MIES.h"
00002 
00003 //------------------------------------------------------------------------------
00004 // Class WDN_MIES
00005 //-------------------------------------------------
00006 // Private Members
00007 
00008 /* Initialize Individuals in population P with uniform randomly 
00009  * chosen values within various variable and parameter domains
00010  * or read previously generated population in from file 
00011  * [and re-evaluate in case of robustness evaluation, then writePop() and exit]
00012  */
00013 void WDN_MIES::initialize()
00014 {
00015 #ifdef DEBUG
00016   cout << "WDN_MIES::initialize()" << endl;
00017 #endif
00018 
00019   MixedIntegerES::initialize();
00020 
00021 #ifdef ROBUSTNESS_EVAL
00022   evaluate(); // Re-evaluate with MC samples
00023   writePop(-1); // When ROBUSTNESS_EVAL, RunID stored in optimalF[0] is used
00024   exit(0);
00025 #endif
00026 }
00027 
00028 /* Determine fitness values for population P or O */
00029 void WDN_MIES::evaluate()
00030 {
00031 #ifdef DEBUG
00032   cout << "WDN_MIES::evaluate()" << endl;
00033 #endif
00034 
00035 #ifdef ROBUST
00036   generateSampleSet();
00037 #endif
00038 
00039   MixedIntegerES::evaluate();
00040 }
00041 
00042 /* Run simulator using the objective variables contained in Individual I */
00043 void WDN_MIES::simulate(Individual* I) 
00044 { 
00045 #ifdef DEBUG
00046   cout << "WDN_MIES::simulate()" << endl;
00047 #endif
00048 
00049 #ifdef SMSEMOA3D // Comment out for "fixed normalization" of crowding distance in NSGAII_MIES
00050   if (!fixedNadirIdeal)
00051   {
00052     fixedNadirIdeal = true; 
00053     getNadirIdeal(nadir, ideal, (n_d == 0) ? n_z : n_d);
00054   }
00055 #endif
00056  
00057 #ifdef ROBUST   
00058   WDN::robustSimulate(I, NULL, n_d); 
00059 #else
00060   if (n_d == 0) // Added for integer-based optimization
00061   {
00062     I->D = I->Z;
00063     WDN::simulate(I, NULL, n_z, NULL);
00064   }
00065   else
00066     WDN::simulate(I, NULL, n_d, NULL); 
00067 #endif    
00068 } 
00069 
00070 /* Create lambda offspring */
00071 void WDN_MIES::recombineMutate()
00072 {
00073 #ifdef DEBUG
00074   cout << "WDN_MIES::recombineMutate()" << endl;
00075 #endif
00076 
00077 #ifdef SP
00078   /* Create lambda offspring by mutation of a single (recombined) parent */
00079   MixedIntegerES::recombineMutateSingleParent();
00080 #else
00081   /* Create lambda offspring by recombination and mutation */
00082   MixedIntegerES::recombineMutate();
00083 #endif
00084 }
00085 
00086 /* Write current population P to file */
00087 void WDN_MIES::writePop(time_t id)
00088 {
00089 #ifdef DEBUG
00090   cout << "WDN_MIES::writePop()" << endl;
00091 #endif
00092 
00093   string filename;
00094   fstream outfile;
00095   if (snapShots)
00096   {
00097     writeStep(id);
00098 
00099     unsigned  evalsUsed = mu + currGen * lambda;
00100 
00101 #ifdef ROBUST   
00102 # ifdef LHSR 
00103       evalsUsed = getEvalsUsed();
00104 # else
00105       evalsUsed *= sampleSetSize;
00106 # endif
00107 #endif
00108 
00109     if (evalsUsed % mu != 0)
00110       return;
00111       
00112     filename = "RunID_" + itos((int) id) + "_P_" + itos(evalsUsed) + ".txt"; 
00113   }
00114   else
00115   {
00116 #ifdef ROBUSTNESS_EVAL  
00117     string filename = "RunID_" + boost::lexical_cast<string>((double) optimalF[0]) + "_P_robust.txt"; 
00118 #else
00119     if (currGen == 0)
00120       filename = "RunID_" + itos((int) id) + "_P_0.txt"; 
00121     else if (currGen == generations)
00122       filename = "RunID_" + itos((int) id) + "_P.txt"; 
00123     else
00124       return;
00125 #endif
00126   }       
00127 
00128   outfile.open(filename.c_str(), ios::out);
00129 
00130   int size;
00131   for (unsigned i = 0; i < mu; i++)
00132   {
00133     outfile << ((P[i]->feasible) ? "F" : "P" ) << "\t";
00134 
00135     size = P[i]->F.size();        
00136     for (int j = 0; j < size; j++)
00137       outfile << P[i]->F[j] << "\t";
00138 
00139     outfile << "\t\t\t\t";
00140             
00141     for (unsigned j = 0; j < n_r; j++)
00142       outfile << P[i]->R[j] << "\t";
00143 
00144     for (unsigned j = 0; j < n_z; j++)
00145       outfile << P[i]->Z[j] << "\t";
00146 
00147     for (unsigned j = 0; j < n_d; j++)
00148       outfile << P[i]->D[j] << "\t";
00149 
00150     outfile << "\t\t\t\t";
00151 
00152     for (unsigned j = 0; j < n_sigma_r; j++)
00153       outfile << P[i]->S_r[j] << "\t";
00154 
00155     for (unsigned j = 0; j < n_sigma_z; j++)
00156       outfile << P[i]->S_z[j] << "\t";
00157 
00158     for (unsigned j = 0; j < n_prob; j++)
00159       outfile << P[i]->Prob[j] << "\t";
00160 
00161     outfile << endl << endl << endl; // Two white lines to indicate new gnuplot index
00162   }
00163   outfile.close();
00164 }
00165 
00166 /* Output stepsizes */
00167 void WDN_MIES::writeStep(time_t id)
00168 {
00169   string filename = "RunID_" + itos((int) id) + "_stepsizes.txt"; 
00170   fstream outfile(filename.c_str(), ios::out | ios::ate | ios::app);
00171 
00172   outfile << currGen << "\t\t";
00173 
00174   double mean = 0, min = DBL_MAX, max = 0;
00175   if (n_sigma_r > 0)
00176   {
00177     for (unsigned j = 0; j < mu; j++)
00178     {
00179       if (P[j]->S_r[0] < min)
00180         min = P[j]->S_r[0];
00181 
00182       if (P[j]->S_r[0] > max)   
00183         max = P[j]->S_r[0];      
00184       
00185       mean += P[j]->S_r[0];
00186     }
00187     mean /= mu;
00188 
00189     outfile << mean << "\t" << min << "\t" << max << "\t\t";
00190   }
00191 
00192   if (n_sigma_z > 0)
00193   {
00194     mean = 0;
00195     min = DBL_MAX; 
00196     max = 0;
00197     for (unsigned j = 0; j < mu; j++)
00198     {
00199       if (P[j]->S_z[0] < min)
00200         min = P[j]->S_z[0];
00201 
00202       if (P[j]->S_z[0] > max)   
00203         max = P[j]->S_z[0];      
00204       
00205       mean += P[j]->S_z[0];
00206     }
00207     mean /= mu;
00208 
00209     outfile << mean << "\t" << min << "\t" << max << "\t\t";
00210   }
00211 
00212   if (n_prob > 0)
00213   {
00214     mean = 0;
00215     min = DBL_MAX; 
00216     max = 0;
00217     for (unsigned j = 0; j < mu; j++)
00218     {
00219       if (P[j]->Prob[0] < min)
00220         min = P[j]->Prob[0];
00221 
00222       if (P[j]->Prob[0] > max)  
00223         max = P[j]->Prob[0];      
00224       
00225       mean += P[j]->Prob[0];
00226     }
00227     mean /= mu;
00228 
00229     outfile << (1.0/n_d + mean) << "\t" << (1.0/n_d + min) << "\t" << (1.0/n_d + max) << "\t\t";
00230   }
00231 
00232   outfile << endl;
00233   outfile.close();
00234 }
00235 
00236 /* Log time that was needed to complete generation */
00237 void WDN_MIES::writeLog(time_t elapsed, time_t id)
00238 {
00239 #ifdef DEBUG
00240   cout << "WDN_MIES::writeLog()" << endl;
00241 #endif
00242 
00243   if (snapShots)
00244     return;
00245 
00246   string filename = "RunID_" + itos((int) id) + "_log.txt"; 
00247   fstream outfile(filename.c_str(), ios::out | ios::ate | ios::app);
00248 
00249   if (currGen <= generations)
00250   {
00251     if (feedback)
00252       cout << endl << "Generation " << currGen << " took " << elapsed << "s" << endl;
00253     outfile << "Generation " << currGen << ": \t" << elapsed << "s" << endl;
00254   }
00255   else
00256   {
00257     if (feedback) 
00258     {
00259 #ifdef LHSR
00260       cout << "Total number of evaluations used: " << getEvalsUsed() << endl;
00261 #endif
00262       cout << "Optimization took " << elapsed << "s" << endl << endl;
00263     }
00264     outfile << endl << "Total optimization: \t";
00265 #ifdef LHSR
00266     outfile << getEvalsUsed() << " evaluations, ";
00267 #endif
00268     outfile << elapsed << "s" << endl;
00269   }
00270 
00271   outfile.close();
00272 }

Generated on Tue Oct 4 2011 16:25:19 for WDN by  doxygen 1.7.2