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

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

Go to the documentation of this file.
00001 #include "APriori_MIES.h"
00002 
00003 //------------------------------------------------------------------------------
00004 // Class APriori_MIES
00005 //-------------------------------------------------
00006 // Protected Members
00007 
00008 /* Select mu Individuals from O and optionally P to form new population P */
00009 void APriori_MIES::select()
00010 {
00011 #ifdef DEBUG
00012   cout << "APriori_MIES::select()" << endl;
00013 #endif
00014 
00015   /* Partition population into fronts */
00016   vector<Individual*> Q;
00017   int size = lambda;
00018   for (unsigned i = 0; i < lambda; i++)
00019   {
00020     Q.push_back(O[i]);
00021     O[i] = NULL;
00022   }
00023 
00024   /* Select Individuals from parent population P that do not exceed maximum life span */
00025   for (unsigned i = 0; i < mu; i++)
00026   {
00027     if (P[i]->age <= kappa)
00028     {
00029       Q.push_back(P[i]);
00030       P[i] = NULL;
00031       size++;
00032     } 
00033   }
00034 
00035   /* Generate vector with indices for Q */
00036   vector<int> indices;
00037   for (int i = 0; i < size; i++)
00038     indices.push_back(i);
00039   
00040   /* Cleanup unused Individuals from parent population P */
00041   for (unsigned i = 0; i < mu; i++)
00042   {
00043     delete P[i];
00044     P[i] = NULL;
00045   }
00046 
00047   /* Determine new population */
00048   P.clear();
00049   quickSort(indices, 0, size-1, &Q, selectFunctionActual); // Sort Individuals in descending order 
00050 
00051   /* Add best mu Individuals to population */
00052   int k;
00053   for (unsigned i = (direction[selectFunctionActual] == 1) ? 0 : size-1, j = 0; j < mu; j++)
00054   {
00055     k = indices[i + j*direction[selectFunctionActual]];
00056     P.push_back(Q[k]);
00057     Q[k] = NULL;
00058   }
00059 
00060   /* Cleanup unused Individuals */
00061   for (int i = 0; i < size; i++)
00062     delete Q[i];
00063 
00064   /* Determine bestF value */
00065   determineBestF();
00066 }
00067 
00068 /* Sort Individuals on objective, in descending order */
00069 void APriori_MIES::quickSort(vector<int>& indices, int top, int bottom, vector<Individual*>* Q, int objective)
00070 {
00071 #ifdef DEBUG
00072   cout << "APriori_MIES::quickSort(), sorting on objective " + itos(objective) << endl;
00073 #endif
00074 
00075   /* top = subscript of beginning of vector being considered
00076    * bottom = subscript of end of vector being considered
00077    */
00078   int middle;
00079   if (top < bottom)
00080   {
00081     middle = partition(indices, top, bottom, Q, objective);
00082     quickSort(indices, top, middle, Q, objective); // Sort top partition
00083     quickSort(indices, middle+1, bottom, Q, objective); // Sort bottom partition
00084   }
00085 }
00086 
00087 int APriori_MIES::partition(vector<int>& indices, int top, int bottom, vector<Individual*>* Q, int objective)
00088 {
00089 #ifdef DEBUG
00090   cout << "APriori_MIES::partition(), top=" << top << ", bottom=" << bottom << endl;
00091 #endif
00092 
00093   int temp, 
00094       i = top - 1, 
00095       j = bottom + 1;
00096 
00097   double x = Q->at(indices[top])->F[objective];
00098 
00099   do
00100   {
00101     do     
00102     {
00103       j--;
00104     } while (x > Q->at(indices[j])->F[objective]);
00105 
00106     do          
00107     {
00108       i++;
00109     } while (x < Q->at(indices[i])->F[objective]);
00110 
00111     if (i < j)
00112     {     
00113       /* Switch elements at positions i and j */
00114       temp = indices[i];    
00115       indices[i] = indices[j];
00116       indices[j] = temp;
00117     }
00118   } while (i < j);    
00119   
00120   /* Return middle index */
00121   return j;           
00122 }
00123 
00124 /* Determine bestF value for current population */
00125 void APriori_MIES::determineBestF()
00126 {
00127 #ifdef DEBUG
00128   cout << "APriori_MIES::determineBestF()" << endl;
00129 #endif
00130 
00131   vector<int> indices;
00132   for (unsigned i = 0; i < mu; i++)
00133     indices.push_back(i);
00134 
00135   /* Sort the indices in descending order depending on the selectFunctionActual value of the individual it points to */
00136   quickSort(indices, 0, mu-1, &P, selectFunctionActual); 
00137    
00138   int k;
00139   for (unsigned i = (direction[selectFunctionActual] == 1) ? 0 : mu-1, j = 0; j < mu; j++)
00140   {
00141     k = indices[i + j*direction[selectFunctionActual]];
00142 
00143     if (!(P[k]->feasible)) // Allow only fitness value from feasible solution 
00144       continue;
00145 
00146     bestF[selectFunctionActual] = P[k]->F[selectFunctionActual];
00147     break;
00148 
00149     bestF[selectFunctionActual] = -1; // No feasible solutions in population, set to error value -1
00150   }
00151 }
00152 
00153 /* In case of convergence study (i.e., with known optima supplied) determine whether optima have been reached */
00154 bool APriori_MIES::optimumReached()
00155 {
00156 #ifdef DEBUG
00157   cout << "APriori_MIES::optimumReached()" << endl;
00158 #endif
00159 
00160   if (generations == USHRT_MAX) // Convergence study
00161   {
00162     if (direction[selectFunctionActual] == 1)
00163     {
00164       if (bestF[selectFunctionActual] >= optimalF[selectFunctionActual])
00165         return true;
00166     }
00167     else
00168     {
00169       if (bestF[selectFunctionActual] <= optimalF[selectFunctionActual])
00170         return true;
00171     }
00172   }
00173 
00174   return false;
00175 }
00176 

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