- Main Page
- Classes
- Files
- Related Pages

- Search for |

# moeoReplacement.h

    00001 // -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
    00002
    00003 //-----------------------------------------------------------------------------
    00004 // moeoReplacement.h
    00005 // (c) OPAC Team (LIFL), Dolphin Project (INRIA), 2006
    00006 /*
    00007     This library...
    00008
    00009     Contact: paradiseo-help@lists.gforge.inria.fr, http://paradiseo.gforge.inria.fr
    00010  */
    00011 //-----------------------------------------------------------------------------
    00012
    00013 #ifndef MOEOREPLACEMENT_H_
    00014 #define MOEOREPLACEMENT_H_
    00015
    00016 #include <eoPerf2Worth.h>
    00017 #include <eoPop.h>
    00018 #include <eoReplacement.h>
    00019
    00020
    00024 template < class EOT, class WorthT > class moeoReplacement:public eoReplacement <
    00025   EOT >
    00026 {
    00027 };
    00028
    00029
    00034 template < class EOT, class WorthT =
    00035   double >class moeoElitistReplacement:public moeoReplacement < EOT, WorthT >
    00036 {
    00037 public:
    00038
    00043   moeoElitistReplacement (eoPerf2Worth < EOT,
    00044                           WorthT > &_perf2worth):perf2worth (_perf2worth)
    00045   {
    00046   }
    00047
    00048
    00054   void operator () (eoPop < EOT > &_parents, eoPop < EOT > &_offspring)
    00055   {
    00056     unsigned size = _parents.size ();
    00057     _parents.reserve (_parents.size () + _offspring.size ());
    00058     copy (_offspring.begin (), _offspring.end (), back_inserter (_parents));
    00059
    00060     // calculate worths
    00061     perf2worth (_parents);
    00062     perf2worth.sort_pop (_parents);
    00063     perf2worth.resize (_parents, size);
    00064
    00065     _offspring.clear ();
    00066   }
    00067
    00068 private:
    00070   eoPerf2Worth < EOT, WorthT > &perf2worth;
    00071 };
    00072
    00073
    00077 template < class EOT, class WorthT =
    00078   double >class moeoDisctinctElitistReplacement:public moeoReplacement < EOT,
    00079   WorthT >
    00080 {
    00081 public:
    00082
    00087   moeoDisctinctElitistReplacement (eoPerf2Worth < EOT,
    00088                                    WorthT >
    00089                                    &_perf2worth):perf2worth (_perf2worth)
    00090   {
    00091   }
    00092
    00093
    00099   void operator () (eoPop < EOT > &_parents, eoPop < EOT > &_offspring)
    00100   {
    00101     unsigned size = _parents.size ();
    00102     _parents.reserve (_parents.size () + _offspring.size ());
    00103     copy (_offspring.begin (), _offspring.end (), back_inserter (_parents));
    00104
    00105     // creation of the new population (of size 'size')
    00106     createNewPop (_parents, size);
    00107
    00108     _offspring.clear ();
    00109   }
    00110
    00111
    00112 private:
    00113
    00115   eoPerf2Worth < EOT, WorthT > &perf2worth;
    00116
    00117
    00123   void createNewPop (eoPop < EOT > &_pop, unsigned _size)
    00124   {
    00125     // the number of occurences for each individual
    00126     std::map < EOT, unsigned >nb_occurences;
    00127     for (unsigned i = 0; i < _pop.size (); i++)
    00128       nb_occurences[_pop[i]] = 0;
    00129     // the new population
    00130     eoPop < EOT > new_pop;
    00131     new_pop.reserve (_pop.size ());
    00132     for (unsigned i = 0; i < _pop.size (); i++)
    00133       {
    00134         if (nb_occurences[_pop[i]] == 0)
    00135           new_pop.push_back (_pop[i]);
    00136         nb_occurences[_pop[i]]++;
    00137       }
    00138
    00139     // calculate worths (on the new population)
    00140     perf2worth (new_pop);
    00141     perf2worth.sort_pop (new_pop);
    00142
    00143     // if case there's not enough individuals in the population...
    00144     unsigned new_pop_size_init = new_pop.size ();
    00145     unsigned k = 0;
    00146     while (new_pop.size () < _size)
    00147       {
    00148         if (k < new_pop_size_init)
    00149           {
    00150             if (nb_occurences[new_pop[k]] > 1)
    00151               {
    00152                 new_pop.push_back (new_pop[k]);
    00153                 nb_occurences[new_pop[k]]--;
    00154               }
    00155             k++;
    00156           }
    00157         else
    00158           k = 0;
    00159       }
    00160
    00161     // resize and swap the populations
    00162     perf2worth.resize (new_pop, _size);
    00163     _pop.resize (_size);
    00164     _pop.swap (new_pop);
    00165   }
    00166
    00167 };
    00168
    00169 #endif /*MOEOREPLACEMENT_H_ */