• Main Page
  • Related Pages
  • Classes
  • Files
  • Examples
  • File List
  • File Members

Population.h

Go to the documentation of this file.
00001 //===========================================================================
00041 //===========================================================================
00042 
00043 
00044 
00045 
00046 #ifndef __POPULATION_H
00047 #define __POPULATION_H
00048 
00049 #include <EALib/Individual.h>
00050 
00051 
00055 class Population : protected std::vector< Individual * >
00056 {
00057 public:
00058     Population();
00059     explicit Population(unsigned);
00060     Population(const Individual&);
00061     Population(unsigned, const Individual&);
00062     Population(unsigned, const Chromosome&);
00063     Population(unsigned, const Chromosome&,
00064                const Chromosome&);
00065     Population(unsigned, const Chromosome&,
00066                const Chromosome&,
00067                const Chromosome&);
00068     Population(unsigned, const Chromosome&,
00069                const Chromosome&,
00070                const Chromosome&,
00071                const Chromosome&);
00072     Population(unsigned, const Chromosome&,
00073                const Chromosome&,
00074                const Chromosome&,
00075                const Chromosome&,
00076                const Chromosome&);
00077     Population(unsigned, const Chromosome&,
00078                const Chromosome&,
00079                const Chromosome&,
00080                const Chromosome&,
00081                const Chromosome&,
00082                const Chromosome&);
00083     Population(unsigned, const Chromosome&,
00084                const Chromosome&,
00085                const Chromosome&,
00086                const Chromosome&,
00087                const Chromosome&,
00088                const Chromosome&,
00089                const Chromosome&);
00090     Population(unsigned, const Chromosome&,
00091                const Chromosome&,
00092                const Chromosome&,
00093                const Chromosome&,
00094                const Chromosome&,
00095                const Chromosome&,
00096                const Chromosome&,
00097                const Chromosome&);
00098     Population(unsigned, const std::vector< Chromosome * >&);
00099     Population(const Population&);
00100     Population(const std::vector< Individual * >&);
00101     virtual ~Population();
00102 
00103     unsigned          size() const
00104     {
00105         return static_cast< const std::vector< Individual * > * >(this)->size();
00106     }
00107 
00108     void              resize(unsigned n);
00109 
00110     void              setMaximize()
00111     {
00112         ascending = false;
00113     }
00114     void              setMinimize()
00115     {
00116         ascending = true;
00117     }
00118 
00119     void              spinWheelOneTime()
00120     {
00121         spinOnce  = true;
00122     }
00123     void              spinWheelMultipleTimes()
00124     {
00125         spinOnce  = false;
00126     }
00127 
00128     bool              ascendingFitness() const
00129     {
00130         return ascending;
00131     }
00132 
00133     Individual&       operator [ ](unsigned i)
00134     {
00135         RANGE_CHECK(i < size())
00136         return *(*(begin() + i));
00137     }
00138 
00139     const Individual& operator [ ](unsigned i) const
00140     {
00141         RANGE_CHECK(i < size())
00142         return *(*(begin() + i));
00143     }
00144 
00145     Population        operator()(unsigned from, unsigned to) const
00146     {
00147         RANGE_CHECK(from <= to && to < size())
00148         return Population(std::vector< Individual * >(
00149                               begin() + from, begin() + to + 1));
00150     }
00151 
00152     Population&       operator = (const Individual& ind);
00153     Population&       operator = (const Population& pop);
00154 
00155     std::vector< const Chromosome* > matingPool(unsigned chrom,
00156             unsigned from = 0,
00157             unsigned to   = 0) const;
00158 
00159     void              swap(unsigned i, unsigned j)
00160     {
00161         RANGE_CHECK(i < size() && j < size())
00162         std::swap(*(begin() + i), *(begin() + j));
00163     }
00164 
00165     void              sort();
00166     void              shuffle();
00167 
00168     //
00169     // note that individual pop[ i ] may change type and size
00170     // in contrast to pop[ i ] = ind, where type and size of pop[ i ] and ind
00171     // must be the same
00172     //
00173     void              replace(unsigned i, const Individual& ind);
00174     void              replace(unsigned i, const Population& pop);
00175     void              insert(unsigned i, const Individual& ind);
00176     void              insert(unsigned i, const Population& pop);
00177     void              append(const Individual& ind);
00178     void              append(const Population& pop);
00179     void              remove (unsigned i);
00180     void              remove (unsigned from, unsigned to);
00181 
00182     //
00183     // exchange pointer to individuals
00184     //
00185     void              exchange(Population& pop);
00186 
00187     unsigned          bestIndex() const;
00188     unsigned          worstIndex() const;
00189 
00190     //
00191     // it is possible that there are several "best" individuals in the
00192     // population, i.e., several individuals with the same "best" fitness
00193     // value.
00194     // oneOfBest() randomly returns one of these "best" individuals.
00195     //
00196     Individual&       oneOfBest();
00197     const Individual& oneOfBest() const;
00198 
00199     Individual&       best()
00200     {
00201         return (*this)[ bestIndex()];
00202     }
00203     const Individual& best() const
00204     {
00205         return (*this)[ bestIndex()];
00206     }
00207 
00208     Individual&       worst()
00209     {
00210         return (*this)[ worstIndex()];
00211     }
00212     const Individual& worst() const
00213     {
00214         return (*this)[ worstIndex()];
00215     }
00216 
00217     Individual&       random()
00218     {
00219         return (*this)[ Rng::discrete(0, size()-1)];
00220     }
00221     const Individual& random() const
00222     {
00223         return (*this)[ Rng::discrete(0, size()-1)];
00224     }
00225 
00226     void              reproduce(Population& parents,
00227                                 unsigned nelitists = 0)
00228     {
00229         selectUniformRanking(parents, nelitists);
00230     }
00231 
00232     void              linearDynamicScaling(std::vector< double >& window,
00233                                            unsigned long t);
00234     Individual&       selectOneIndividual();
00235     void              selectMuLambda(Population& parents,
00236                                      unsigned nelitists = 0);
00237     void              selectMuLambdaKappa(Population& parents,
00238                                           unsigned lifespan = 1,
00239                                           unsigned adolescence = 0);
00240     void              selectProportional(Population& parents,
00241                                          unsigned nelitists = 0);
00242     void              selectLinearRanking(Population& parents,
00243                                           double etaMax = 1.1,
00244                                           unsigned nelitists = 0);
00245     void              selectUniformRanking(Population& parents,
00246                                            unsigned nelitists = 0);
00247     void              selectTournament(Population& parents,
00248                                        unsigned q = 2,
00249                                        unsigned nelitists = 0);
00250     void              selectLinearRankingWhitley(Population& parents,
00251             double a = 1.1,
00252             unsigned nelitists = 0);
00253     void              selectEPTournament(Population& parents, unsigned q);
00254     void              replaceUnconditional(Population& parents);
00255     void              replaceConditional(Population& parents);
00256     void              replaceRanking(Population& parents);
00257     void              replaceRankingConditional(Population& parents);
00258 
00259     double            minFitness() const;
00260     double            maxFitness() const;
00261     double            meanFitness() const;
00262     double            stdDevFitness() const;
00263 
00264     void              setAge(unsigned a = 0);
00265     void              incAge();
00266 
00267     bool operator == (const Population&) const;
00268     bool operator < (const Population&) const;
00269 
00270     void              setIndex(unsigned i)
00271     {
00272         index = i;
00273     }
00274 
00275     unsigned          getIndex() const
00276     {
00277         return index;
00278     }
00279 
00280     void              setSubPop(bool sub)
00281     {
00282         subPop = sub;
00283     }
00284 
00285     bool              getSubPop() const
00286     {
00287         return subPop;
00288     }
00289 
00290     bool              getSpinOnce() const
00291     {
00292         return spinOnce;
00293     }
00294 
00295     //=======================================================================
00296 
00297     //
00298     // Added by Stefan Wiegand at 20.11.2002
00299     //
00300 
00302     int pvm_pkpop();
00303 
00305     int pvm_upkpop();
00306 
00307 
00308     //=======================================================================
00309 
00310 protected:
00311     unsigned index;
00312     bool     subPop;
00313     bool     ascending;
00314     bool     spinOnce;
00315 
00316     static bool lessFitness(Individual*const&, Individual*const&);
00317     static bool greaterFitness(Individual*const&, Individual*const&);
00318 
00319     static bool       greaterScoreAscending(Individual*const& i1, Individual*const& i2);
00320     static bool       greaterScoreDescending(Individual*const& i1, Individual*const& i2);
00321 
00322     Individual& best(Individual&, Individual&) const;
00323     Individual& worst(Individual&, Individual&) const;
00324 
00325     void        sortIndividuals(std::vector< Individual * >&);
00326 
00327     void        selectInit();
00328     void        selectElitists(Population&, unsigned);
00329     void        selectRouletteWheel(Population&, unsigned);
00330 
00331 #ifndef __NO_GENERIC_IOSTREAM
00332     friend std::ostream& operator << (std::ostream& os,
00333                                       const Population& pop)
00334     {
00335         os << "Population(" << pop.size() << ")\n"
00336         << pop.index     << '\n'
00337         << pop.subPop    << '\n'
00338         << pop.ascending << '\n'
00339         << pop.spinOnce  << std::endl;
00340         for (unsigned i = 0; i < pop.size(); ++i)
00341             os << pop[ i ];
00342         return os;
00343     }
00344 
00345     friend std::istream& operator >> (std::istream& is,
00346                                       Population& pop)
00347     {
00348         unsigned i, popSize(0);
00349         std::string s, t;
00350 
00351         is >> s;
00352         is.get();    // skip end of line
00353 
00354         if ((!pop.subPop || pop.size() == 0) && is.good() &&
00355                 s.substr(0, 11) == "Population(" &&
00356                 s.find(')') != std::string::npos) {
00357 
00358             // Extract the size indication from the string:
00359             t = s.substr(s.find('(') + 1, s.find(')') - s.find('(') - 1);
00360             popSize = atoi(t.c_str());
00361 
00362             // Adapt size of Individual:
00363             pop.resize(popSize);
00364 
00365             is >> pop.index
00366             >> pop.subPop
00367             >> pop.ascending
00368             >> pop.spinOnce;
00369 
00370             for (i = 0; i < pop.size(); ++i)
00371                 is >> pop[ i ];
00372         }
00373 
00374         return is;
00375     }
00376 #endif // !__NO_GENERIC_IOSTREAM
00377 
00378 };
00379 
00380 //===========================================================================
00381 
00382 #endif /* !__POPULATION_H */
00383 
  • Shark Main Page
  • Array
  • Rng
  • LinAlg
  • FileUtil
  • EALib
  • MOO-EALib
  • ReClaM
  • Fuzzy
  • Mixture
  • Tutorials
  • FAQ