Go to the documentation of this file.00001 #include "APriori_MIES.h"
00002
00003
00004
00005
00006
00007
00008
00009 void APriori_MIES::select()
00010 {
00011 #ifdef DEBUG
00012 cout << "APriori_MIES::select()" << endl;
00013 #endif
00014
00015
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
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
00036 vector<int> indices;
00037 for (int i = 0; i < size; i++)
00038 indices.push_back(i);
00039
00040
00041 for (unsigned i = 0; i < mu; i++)
00042 {
00043 delete P[i];
00044 P[i] = NULL;
00045 }
00046
00047
00048 P.clear();
00049 quickSort(indices, 0, size-1, &Q, selectFunctionActual);
00050
00051
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
00061 for (int i = 0; i < size; i++)
00062 delete Q[i];
00063
00064
00065 determineBestF();
00066 }
00067
00068
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
00076
00077
00078 int middle;
00079 if (top < bottom)
00080 {
00081 middle = partition(indices, top, bottom, Q, objective);
00082 quickSort(indices, top, middle, Q, objective);
00083 quickSort(indices, middle+1, bottom, Q, objective);
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
00114 temp = indices[i];
00115 indices[i] = indices[j];
00116 indices[j] = temp;
00117 }
00118 } while (i < j);
00119
00120
00121 return j;
00122 }
00123
00124
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
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))
00144 continue;
00145
00146 bestF[selectFunctionActual] = P[k]->F[selectFunctionActual];
00147 break;
00148
00149 bestF[selectFunctionActual] = -1;
00150 }
00151 }
00152
00153
00154 bool APriori_MIES::optimumReached()
00155 {
00156 #ifdef DEBUG
00157 cout << "APriori_MIES::optimumReached()" << endl;
00158 #endif
00159
00160 if (generations == USHRT_MAX)
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