6 #include <boost/algorithm/minmax_element.hpp>
7 #include <boost/numeric/ublas/io.hpp>
20 bool operator()(
const gplib::rvec &fit1,
const gplib::rvec &fit2)
const
22 typedef vector<double>::const_iterator tit;
24 const size_t length = fit2.size();
25 for (
size_t i = 0; i < length; ++i)
27 if (fit1(i) > fit2(i))
29 if (fit1(i) < fit2(i))
36 void ParetoGA::CalcCrowdingDistance(gplib::rmat &LocalMisFit,
37 GeneralPopulation &LocalPopulation)
39 const double NearInfinity = 1e50;
40 const double tolerance = 1e-10;
41 const unsigned int popsize = LocalPopulation.GetPopsize();
44 for (
unsigned int i = 0; i < popsize; ++i)
45 CrowdingDistances(i) = 0;
46 for (
unsigned int i = 0; i < nobjective; ++i)
48 if (fcmp(GetWeights().at(i), 0, tolerance) != 0)
50 pair<tMisfitIterator, tMisfitIterator> MinMax =
51 boost::minmax_element(ublas::row(LocalMisFit, i).begin(),
52 ublas::row(LocalMisFit, i).end());
54 if (fcmp(*(MinMax.second) - *(MinMax.first), 0, tolerance)
56 normalize = *(MinMax.second) - *(MinMax.first);
57 for (
unsigned int j = 0; j < Ranks.size(); ++j)
60 for (
unsigned int k = 0; k < Ranks.at(j).size(); ++k)
62 IndexMap.insert(make_pair(LocalMisFit(i,
63 Ranks.at(j).at(k)), Ranks.at(j).at(k)));
65 tIndexMap::iterator end = --IndexMap.end();
66 tIndexMap::iterator pos = IndexMap.begin();
67 CrowdingDistances(pos->second) = NearInfinity;
70 CrowdingDistances(end->second) = NearInfinity;
71 tIndexMap::iterator previous = IndexMap.begin();
72 tIndexMap::iterator next = pos;
75 for (; pos != end; pos++)
77 CrowdingDistances(pos->second) += (next->first
78 - previous->first) / normalize;
86 LocalPopulation.SetCrowdingDistances(CrowdingDistances);
89 void ParetoGA::CalcProbabilities(
const int iterationnumber,
93 gplib::rvec currmisfit(nobjective), compmisfit(nobjective);
95 unsigned int rankedelements = 0;
97 vector<int> CurrRanks;
98 unsigned int i, j, k = 0;
99 vector<bool> dominated(size,
false);
100 unsigned int maxindex =
size;
104 Indices.reserve(size);
105 for (i = 0; i <
size; ++i)
106 Indices.push_back(i);
107 while (rankedelements < size)
110 for (i = 0; i < maxindex; ++i)
112 for (j = 0; j < nobjective; ++j)
113 currmisfit(j) = LocalMisFit(j, Indices.at(i));
115 dominated.at(i) =
false;
116 while (!dominated.at(i) && (j < maxindex))
118 for (k = 0; k < nobjective; ++k)
119 compmisfit(k) = LocalMisFit(k, Indices.at(j));
122 dominated.at(i) =
true;
128 for (i = 0; i < maxindex; ++i)
130 if (!dominated.at(i))
132 CurrRanks.push_back(Indices.at(i));
133 Indices.at(i) = Indices.at(maxindex - 1);
134 dominated.at(i) = dominated.at(maxindex - 1);
140 Ranks.push_back(CurrRanks);
142 const unsigned int nranks = Ranks.size();
143 cout <<
"NRanks : " << nranks << endl;
147 for (i = 0; i < nranks; ++i)
149 for (j = 0; j < Ranks.at(i).size(); ++j)
152 Probabilities(Ranks.at(i).at(j)) = prob;
156 Probabilities /= sum;
159 CalcCrowdingDistance(LocalMisFit, LocalPopulation);
162 void ParetoGA::Elitism(
const int iterationnumber)
164 const unsigned int popsize = Population->GetPopsize();
165 const unsigned int ngenes = Population->GetGenesize();
167 Population->GetOldPopulation());
168 gplib::rmat IntermediateMisFit(nobjective, 2 * popsize);
169 ublas::matrix_range<gplib::rmat> MisFitFirstHalf(IntermediateMisFit,
170 ublas::range(0, nobjective), ublas::range(0, popsize));
171 ublas::matrix_range<gplib::rmat> MisFitSecondHalf(IntermediateMisFit,
172 ublas::range(0, nobjective), ublas::range(popsize, 2 * popsize));
173 MisFitFirstHalf = MisFit;
174 MisFitSecondHalf = OldMisFit;
175 CalcProbabilities(iterationnumber, IntermediateMisFit, IntermediatePop);
177 unsigned int included = 0;
178 unsigned int currentrank = 0;
180 while (Ranks.at(currentrank).size() <= (popsize - included))
182 for (
unsigned int j = 0; j < Ranks.at(currentrank).size(); ++j)
184 ublas::row(Newpopulation, included) = ublas::row(
185 IntermediatePop.GetPopulation(),
186 Ranks.at(currentrank).at(j));
193 for (
unsigned int j = 0; j < Ranks.at(currentrank).size(); ++j)
194 IndexMap.insert(make_pair(IntermediatePop.GetCrowdingDistances()(
195 Ranks.at(currentrank).at(j)), Ranks.at(currentrank).at(j)));
196 tIndexMap::iterator pos = IndexMap.begin();
197 advance(pos, Ranks.at(currentrank).size() - (popsize - included));
198 while (pos != IndexMap.end())
200 ublas::row(Newpopulation, included) = ublas::row(
201 IntermediatePop.GetPopulation(), pos->second);
205 Population->SetPopulation(Newpopulation);
208 void ParetoGA::PrintRanks(std::ostream &output)
210 const unsigned int nobj = MisFit.size1();
211 const unsigned int nranks = Ranks.size();
212 for (
unsigned int i = 0; i < nranks; ++i)
214 output <<
"Rank: " << i << endl;
215 for (
unsigned int j = 0; j < Ranks.at(i).size(); ++j)
217 for (
unsigned int k = 0; k < nobj; ++k)
218 output << MisFit(k, Ranks.at(i).at(j)) <<
" ";
226 void ParetoGA::PrintFront(std::ostream &output)
228 const unsigned int nobj = MisFit.size1();
229 const unsigned int frontsize = Ranks.front().size();
230 for (
unsigned int i = 0; i < frontsize; ++i)
232 for (
unsigned int j = 0; j < nobj; ++j)
233 output << MisFit(j, Ranks.front().at(i)) <<
" ";
236 output << endl << endl;
239 ParetoGA::~ParetoGA()
The base class for the population of a genetic algorithm, implements storage and access functions...
ublas::vector< double > tprobabilityv
void SetProbabilities(const tprobabilityv &LocalProb)
bool operator()(const gplib::rvec &fit1, const gplib::rvec &fit2) const
Determines whether one vector of misfit values is partially less than the other.
std::multimap< double, int > tIndexMap
ublas::vector< double > tcrowddistv