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

D:/LIACS/mies/SerialES/TestFunctions_MIES.cpp

Go to the documentation of this file.
00001 #include "TestFunctions_MIES.h"
00002 
00003 //------------------------------------------------------------------------------
00004 // Class TestFunctions_MIES
00005 //-------------------------------------------------
00006 // Protected Members
00007 
00008 /* Run simulator using the object variables contained in Individual I */
00009 void TestFunctions_MIES::simulate(Individual* I)
00010 {
00011 #ifdef DEBUG
00012   cout << "TestFunctions_MIES::simulate()" << endl;
00013 #endif
00014 
00015   if (MOproblem != " ")
00016   {  
00017 #ifdef MSCTHES    
00018     if (MOproblem == "DTLZ1")
00019       DTLZ1(I); 
00020     else if (MOproblem == "DTLZ2")
00021       DTLZ2(I);
00022 
00023     for (int i = 0; i < 3; i++) 
00024       I->F[i] = F[i];
00025 #endif
00026   }
00027   else
00028   {
00029     I->F[0] = f1(I);
00030     I->F[1] = f2(I);
00031     I->F[2] = f3(I);
00032     I->F[3] = f_sphere(I, n_r, n_z, n_d);
00033     I->F[4] = f_barrier(I, 100);
00034   }
00035 
00036   /* Update sum of F values */
00037   I->F[n_f] = 0;
00038   for (unsigned i = 0; i < selectDimension; i++)
00039     I->F[n_f] += I->F[selectFunction[i]];
00040 
00041   if (feedback)
00042   {
00043     cout << endl << "Results: " << endl;
00044 
00045     for (unsigned i = 0; i < n_f; i++)
00046       cout << "F[" << i << "]=" << I->F[i] << endl;
00047   }
00048 }
00049 
00050 /* f1: modified step function             (see Mixed-Integer Evolution Strategy by Emmerich, 2000) */
00051 double TestFunctions_MIES::f1(Individual* I)
00052 {
00053 #ifdef DEBUG
00054   cout << "TestFunctions_MIES::f1()" << endl;
00055 #endif
00056 
00057   double result = 0;
00058 
00059   for (unsigned i = 0; i < n_r; i++)
00060     result += pow(floor(I->R[i]), 2);
00061 
00062   for (unsigned i = 0; i < n_z; i++)
00063     result += pow((double) ((int) (I->Z[i] / 10)), 2);
00064 
00065   for (unsigned i = 0; i < n_d; i++)
00066     result += pow((double) (I->D[i] % 2), 2);
00067 
00068   return result;
00069 }
00070 
00071 /* f2: weighted sphere function           (see Mixed-Integer Evolution Strategy by Emmerich, 2000) */
00072 double TestFunctions_MIES::f2(Individual* I)
00073 {
00074 #ifdef DEBUG
00075   cout << "TestFunctions_MIES::f2()" << endl;
00076 #endif
00077 
00078   double result = 0;
00079 
00080   for (unsigned i = 0; i < n_r; i++)
00081     result += (i+1) * pow(I->R[i], 2);
00082 
00083   for (unsigned i = 0; i < n_z; i++)
00084     result += (i+1) * pow((double) I->Z[i], 2);
00085 
00086   for (unsigned i = 0; i < n_d; i++)
00087     result += (i+1) * pow((double) I->D[i], 2);
00088 
00089   return result;
00090 }
00091 
00092 /* f3: general quadratic fitness function (see Mixed-Integer Evolution Strategy by Emmerich, 2000) */
00093 double TestFunctions_MIES::f3(Individual* I)
00094 {
00095 #ifdef DEBUG
00096   cout << "TestFunctions_MIES::f3()" << endl;
00097 #endif
00098 
00099   double result = 0, temp = 0;
00100  
00101   for (unsigned i = 0; i < n_r; i++)
00102   {
00103     temp = 0;
00104     for (unsigned j = 0; j <= i; j++)
00105       temp += I->R[j] + I->Z[j] + I->D[j];
00106 
00107     result += pow(temp, 2);
00108   }
00109 
00110   return result;
00111 }
00112 
00113 /* f_sphere: generalized sphere function  (see Mixed-Integer Evolution Strategies by Li, 2006) */
00114 double TestFunctions_MIES::f_sphere(Individual* I, unsigned n_r, unsigned n_z, unsigned n_d)
00115 {
00116 #ifdef DEBUG
00117   cout << "TestFunctions_MIES::f_sphere()" << endl;
00118 #endif
00119 
00120   double result = 0;
00121 
00122   for (unsigned i = 0; i < n_r; i++)
00123     result += pow(I->R[i], 2);
00124 
00125   for (unsigned i = 0; i < n_z; i++)
00126     result += pow((double) I->Z[i], 2);
00127 
00128   for (unsigned i = 0; i < n_d; i++)
00129     result += pow((double) I->D[i], 2);
00130 
00131   return result;
00132 }
00133 
00134 /* f_barrier: multimodal barrier problem  (see Mixed-Integer Evolution Strategies by Li, 2006) */
00135 double TestFunctions_MIES::f_barrier(Individual* I, int C)
00136 {
00137 #ifdef DEBUG
00138   cout << "TestFunctions_MIES::f_barrier()" << endl;
00139 #endif
00140 
00141   double result = 0;
00142   static vector<int> A;
00143   int j, temp;
00144 
00145   if (A.empty())
00146   { 
00147     for (int i = 0; i < 21; i++)
00148       A.push_back(i);
00149 
00150     for (int k = 0; k < C; k++)
00151     {
00152       j = uniInt() % 20;
00153       temp = A[j];
00154       A[j] = A[j+1];
00155       A[j+1] = temp;
00156     }
00157   }
00158 
00159   result += f_sphere(I, n_r, 0, n_d);
00160 
00161   for (unsigned i = 0; i < n_z; i++)
00162     result += pow((double) A[I->Z[i]], 2);
00163 
00164   return result;
00165 }
00166 
00167 /* Write current population P to file */
00168 void TestFunctions_MIES::writePop(time_t id)
00169 {
00170 #ifdef DEBUG
00171   cout << "TestFunctions_MIES::writePop()" << endl;
00172 #endif
00173 
00174   /* Write current population */        
00175   string filename = "RunID_" + itos((int) id) + "_P.txt"; 
00176   fstream outfile(filename.c_str(), ios::out);
00177         
00178   int size;
00179   for (int k = 0; k < 2; k++)
00180   {
00181     for (unsigned i = 0; i < mu; i++)
00182     {
00183       outfile << ((P[i]->feasible) ? "F" : "P" ) << "\t";
00184 
00185       size = P[i]->F.size();      
00186       for (int j = 0; j < size; j++)
00187         outfile << P[i]->F[j] << "\t";
00188 
00189       outfile << "\t\t\t\t";
00190             
00191       for (unsigned j = 0; j < n_r; j++)
00192         outfile << P[i]->R[j] << "\t";
00193 
00194       for (unsigned j = 0; j < n_z; j++)
00195         outfile << P[i]->Z[j] << "\t";
00196 
00197       for (unsigned j = 0; j < n_d; j++)
00198         outfile << P[i]->D[j] << "\t";
00199 
00200       outfile << "\t\t\t\t";
00201 
00202       for (unsigned j = 0; j < n_sigma_r; j++)
00203         outfile << P[i]->S_r[j] << "\t";
00204 
00205       for (unsigned j = 0; j < n_sigma_z; j++)
00206         outfile << P[i]->S_z[j] << "\t";
00207 
00208       for (unsigned j = 0; j < n_prob; j++)
00209         outfile << P[i]->Prob[j] << "\t";
00210 
00211       outfile << endl << endl << endl; // Two white lines to indicate new gnuplot index
00212     }
00213     outfile.close();
00214 
00215     if (k < 1)
00216     {
00217       /* Only for first generation write population to permanent file */
00218       if (currGen > 0) // Comment out these lines
00219         break;         // for all generations to be written to files
00220 
00221       filename = "RunID_" + itos((int) id) + "_P_g" + itos(currGen) + ".txt"; 
00222       outfile.open(filename.c_str(), ios::out);
00223     }
00224   }
00225 
00226   /* Output stepsizes */
00227   filename = "RunID_" + itos((int) id) + "_stepsizes.txt"; 
00228   outfile.open(filename.c_str(), ios::out | ios::ate | ios::app);
00229   outfile << currGen << "\t"; 
00230 
00231   for (unsigned i = 0; i < mu; i++)
00232   {
00233     for (unsigned j = 0; j < n_sigma_r; j++)
00234       outfile << P[i]->S_r[j] << "\t";
00235 
00236     for (unsigned j = 0; j < n_sigma_z; j++)
00237       outfile << P[i]->S_z[j] << "\t";
00238 
00239     for (unsigned j = 0; j < n_prob; j++)
00240       outfile << P[i]->Prob[j] << "\t";
00241 
00242     outfile << "\t\t\t\t";
00243   }
00244 
00245   outfile << endl;
00246   outfile.close();
00247 
00248   /* Determine bestF for initial population */
00249   if (currGen == 0) 
00250     determineBestF(); 
00251 
00252   /* Append best fitness values to file */
00253   filename = "RunID_" + itos((int) id) + "_bestF.txt"; 
00254   outfile.open(filename.c_str(), ios::out | ios::ate | ios::app);
00255 
00256   outfile << currGen << "\t";
00257 
00258 #ifdef APRIORI
00259   outfile << bestF[selectFunctionActual];
00260 #else
00261   for (unsigned i = 0; i < selectDimension; i++) 
00262     outfile << bestF[selectFunction[i]] << "\t";
00263 #endif
00264 
00265   outfile << endl;
00266 
00267   outfile.close();
00268 }
00269 
00270 /* Log time that was needed to complete generation */
00271 void TestFunctions_MIES::writeLog(time_t elapsed, time_t id)
00272 {
00273 #ifdef DEBUG
00274   cout << "TestFunctions_MIES::writeLog()" << endl;
00275 #endif
00276 
00277   string filename = "RunID_" + itos((int) id) + "_log.txt"; 
00278   fstream outfile(filename.c_str(), ios::out | ios::ate | ios::app);
00279 
00280   if (currGen <= generations)
00281   {
00282     if (feedback)
00283       cout << endl << "Generation " << currGen << " took " << elapsed << "s" << endl;
00284     outfile << "Generation " << currGen << ": \t" << elapsed << "s" << endl;
00285   }
00286   else
00287   {
00288     if (feedback)
00289       cout << "Optimization took " << elapsed << "s" << endl << endl;
00290     outfile << endl << "Total optimization: \t" << elapsed << "s" << endl;
00291   }
00292 
00293   outfile.close();
00294 }
00295 

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