diff --git a/branches/paradiseo-moeo-1.0/src/selection/moeoDetTournamentSelect.h b/branches/paradiseo-moeo-1.0/src/selection/moeoDetTournamentSelect.h new file mode 100644 index 000000000..e60709c5b --- /dev/null +++ b/branches/paradiseo-moeo-1.0/src/selection/moeoDetTournamentSelect.h @@ -0,0 +1,83 @@ +// -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*- + +//----------------------------------------------------------------------------- +// moeoDetTournamentSelect.h +// (c) OPAC Team (LIFL), Dolphin Project (INRIA), 2007 +/* + This library... + + Contact: paradiseo-help@lists.gforge.inria.fr, http://paradiseo.gforge.inria.fr + */ +//----------------------------------------------------------------------------- + +#ifndef MOEODETTOURNAMENTSELECT_H_ +#define MOEODETTOURNAMENTSELECT_H_ + +#include +#include +#include +#include + +/** + * Selection strategy that selects ONE individual by deterministic tournament. + */ +template < class MOEOT > class moeoDetTournamentSelect:public moeoSelectOne < MOEOT > +{ +public: + + /** + * Full Ctor. + * @param _comparator the comparator (used to compare 2 individuals) + * @param _tSize the number of individuals in the tournament (default: 2) + */ + moeoDetTournamentSelect (moeoComparator < MOEOT > & _comparator, unsigned int _tSize = 2) : comparator (_comparator), tSize (_tSize) + { + // consistency check + if (tSize < 2) + { + std:: + cout << "Warning, Tournament size should be >= 2\nAdjusted to 2\n"; + tSize = 2; + } + } + + + /** + * Ctor without comparator. A moeoFitnessThenDiversityComparator is used as default. + * @param _tSize the number of individuals in the tournament (default: 2) + */ + moeoDetTournamentSelect (unsigned int _tSize = 2) : comparator (defaultComparator), tSize (_tSize) + { + // consistency check + if (tSize < 2) + { + std:: + cout << "Warning, Tournament size should be >= 2\nAdjusted to 2\n"; + tSize = 2; + } + } + + + /** + * Apply the tournament to the given population + * @param _pop the population + */ + const MOEOT & operator() (const eoPop < MOEOT > &_pop) + { + // use the selector + return mo_deterministic_tournament (_pop, tSize, comparator); + } + + +protected: + + /** the comparator (used to compare 2 individuals) */ + moeoComparator < MOEOT > & comparator; + /** a fitness then diversity comparator can be used as default */ + moeoFitnessThenDiversityComparator < MOEOT > defaultComparator; + /** the number of individuals in the tournament */ + unsigned int tSize; + +}; + +#endif /*MOEODETTOURNAMENTSELECT_H_ */ diff --git a/branches/paradiseo-moeo-1.0/src/selection/moeoRandomSelect.h b/branches/paradiseo-moeo-1.0/src/selection/moeoRandomSelect.h new file mode 100644 index 000000000..2b5bbd088 --- /dev/null +++ b/branches/paradiseo-moeo-1.0/src/selection/moeoRandomSelect.h @@ -0,0 +1,43 @@ +// -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*- + +//----------------------------------------------------------------------------- +// moeoRandomSelect.h +// (c) OPAC Team (LIFL), Dolphin Project (INRIA), 2007 +/* + This library... + + Contact: paradiseo-help@lists.gforge.inria.fr, http://paradiseo.gforge.inria.fr + */ +//----------------------------------------------------------------------------- + +#ifndef MOEORANDOMSELECT_H_ +#define MOEORANDOMSELECT_H_ + +#include +#include + + +/** + * Selection strategy that selects only one element randomly from a whole population. + */ +template < class MOEOT > class moeoRandomSelect:public moeoSelectOne < MOEOT >, public eoRandomSelect +{ +public: + + /** + * Ctor. + */ + moeoRandomSelect(){} + + + /** + * Return one individual at random by using an eoRandomSelect. + */ + const MOEOT & operator () (const eoPop < MOEOT > &_pop) + { + return eoRandomSelect < MOEOT >::operator ()(_pop); + } + +}; + +#endif /*MOEORANDOMSELECT_H_ */ diff --git a/branches/paradiseo-moeo-1.0/src/selection/moeoRouletteSelect.h b/branches/paradiseo-moeo-1.0/src/selection/moeoRouletteSelect.h new file mode 100644 index 000000000..e53b82cfc --- /dev/null +++ b/branches/paradiseo-moeo-1.0/src/selection/moeoRouletteSelect.h @@ -0,0 +1,62 @@ +// -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*- + +//----------------------------------------------------------------------------- +// moeoRouletteSelect.h +// (c) OPAC Team (LIFL), Dolphin Project (INRIA), 2007 +/* + This library... + + Contact: paradiseo-help@lists.gforge.inria.fr, http://paradiseo.gforge.inria.fr + */ +//----------------------------------------------------------------------------- + +#ifndef MOEOROULETTESELECT_H_ +#define MOEOROULETTESELECT_H_ + +#include +#include + +/** + * Selection strategy that selects ONE individual by using roulette wheel process. + * @WARNING This selection only uses fitness values (and not diversity values). + */ +template < class MOEOT > +class moeoRouletteSelect:public moeoSelectOne < MOEOT > +{ +public: + + /** + * Ctor. + * @param _tSize the number of individuals in the tournament (default: 2) + */ + moeoRouletteSelect (unsigned int _tSize = 2) : tSize (_tSize) + { + // consistency check + if (tSize < 2) + { + std:: + cout << "Warning, Tournament size should be >= 2\nAdjusted to 2\n"; + tSize = 2; + } + } + + + /** + * Apply the tournament to the given population + * @param _pop the population + */ + const MOEOT & operator () (const eoPop < MOEOT > & _pop) + { + // use the selector + return mo_roulette_wheel(_pop,tSize); + } + + +protected: + + /** size */ + double & tSize; + +}; + +#endif /*MOEOROULETTESELECT_H_ */ diff --git a/branches/paradiseo-moeo-1.0/src/selection/moeoSelectFromPopAndArch.h b/branches/paradiseo-moeo-1.0/src/selection/moeoSelectFromPopAndArch.h new file mode 100644 index 000000000..078a51c19 --- /dev/null +++ b/branches/paradiseo-moeo-1.0/src/selection/moeoSelectFromPopAndArch.h @@ -0,0 +1,92 @@ +// -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*- + +//----------------------------------------------------------------------------- +// moeoSelectFormPopAndArch.h +// (c) OPAC Team (LIFL), Dolphin Project (INRIA), 2007 +/* + This library... + + Contact: paradiseo-help@lists.gforge.inria.fr, http://paradiseo.gforge.inria.fr + */ +//----------------------------------------------------------------------------- + +#ifndef MOEOSELECTONEFROMPOPANDARCH_H_ +#define MOEOSELECTONEFROMPOPANDARCH_H_ + +#include +#include +#include +#include +#include + +/** + * Elitist selection process that consists in choosing individuals in the archive as well as in the current population. + */ +template < class MOEOT > +class moeoSelectFromPopAndArch : public moeoSelectOne < MOEOT > +{ +public: + + /** + * Ctor + * @param _popSelectOne the population's selection operator + * @param _archSelectOne the archive's selection operator + * @param _arch the archive + * @param _ratioFromPop the ratio of selected individuals from the population + */ + moeoSelectFromPopAndArch (moeoSelectOne < MOEOT > & _popSelectOne, moeoSelectOne < MOEOT > _archSelectOne, moeoArchive < MOEOT > & _arch, double _ratioFromPop=0.5) + : popSelectOne(_popSelectOne), archSelectOne(_archSelectOne), arch(_arch), ratioFromPop(_ratioFromPop) + {} + + + /** + * Defaulr ctor - the archive's selection operator is a random selector + * @param _popSelectOne the population's selection operator + * @param _arch the archive + * @param _ratioFromPop the ratio of selected individuals from the population + */ + moeoSelectFromPopAndArch (moeoSelectOne < MOEOT > & _popSelectOne, moeoArchive < MOEOT > & _arch, double _ratioFromPop=0.5) + : popSelectOne(_popSelectOne), archSelectOne(randomSelectOne), arch(_arch), ratioFromPop(_ratioFromPop) + {} + + + /** + * The selection process + */ + virtual const MOEOT & operator () (const eoPop < MOEOT > & pop) + { + if (arch.size() > 0) + if (rng.flip(ratioFromPop)) + return popSelectOne(pop); + else + return archSelectOne(arch); + else + return popSelectOne(pop); + } + + + /** + * Setups some population stats + */ + virtual void setup (const eoPop < MOEOT > & _pop) + { + popSelectOne.setup(_pop); + } + + +private: + + /** The population's selection operator */ + moeoSelectOne < MOEOT > & popSelectOne; + /** The archive's selection operator */ + moeoSelectOne < MOEOT > & archSelectOne; + /** The archive */ + moeoArchive < MOEOT > & arch; + /** The ratio of selected individuals from the population*/ + double ratioFromPop; + /** A random selection operator (used as default for archSelectOne) */ + moeoRandomSelect < MOEOT > randomSelectOne; + +}; + +#endif /*MOEOSELECTONEFROMPOPANDARCH_H_*/ diff --git a/branches/paradiseo-moeo-1.0/src/selection/moeoSelectOne.h b/branches/paradiseo-moeo-1.0/src/selection/moeoSelectOne.h new file mode 100644 index 000000000..7f4129b99 --- /dev/null +++ b/branches/paradiseo-moeo-1.0/src/selection/moeoSelectOne.h @@ -0,0 +1,24 @@ +// -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*- + +//----------------------------------------------------------------------------- +// moeoSelectOne.h +// (c) OPAC Team (LIFL), Dolphin Project (INRIA), 2007 +/* + This library... + + Contact: paradiseo-help@lists.gforge.inria.fr, http://paradiseo.gforge.inria.fr + */ +//----------------------------------------------------------------------------- + +#ifndef MOEOSELECTONE_H_ +#define MOEOSELECTONE_H_ + +#include + +/** + * Selection strategy for multi-objective optimization that selects only one element from a whole population. + */ +template < class MOEOT > +class moeoSelectOne : public eoSelectOne < MOEOT > {}; + +#endif /*MOEOSELECTONE_H_*/ diff --git a/branches/paradiseo-moeo-1.0/src/selection/moeoSelectors.h b/branches/paradiseo-moeo-1.0/src/selection/moeoSelectors.h new file mode 100644 index 000000000..35f913ae4 --- /dev/null +++ b/branches/paradiseo-moeo-1.0/src/selection/moeoSelectors.h @@ -0,0 +1,158 @@ +// -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*- + +//----------------------------------------------------------------------------- +// moeoSelectors.h +// (c) OPAC Team (LIFL), Dolphin Project (INRIA), 2007 +/* + This library... + + Contact: paradiseo-help@lists.gforge.inria.fr, http://paradiseo.gforge.inria.fr + */ +//----------------------------------------------------------------------------- + +#ifndef MOEOSELECTORS_H_ +#define MOEOSELECTORS_H_ + +#include + + +template +It mo_deterministic_tournament(It _begin, It _end, unsigned int _t_size,moeoComparator& _comparator ,eoRng& _gen = rng) +{ + It best = _begin + _gen.random(_end - _begin); + + for (unsigned int i = 0; i < _t_size - 1; ++i) + { + It competitor = _begin + _gen.random(_end - _begin); + // compare the two individuals by using the comparator + if (_comparator(*best, *competitor)) + // best "better" than competitor + best=competitor; + } + return best; +} + + +template +const MOEOT& mo_deterministic_tournament(const eoPop& _pop, unsigned int _t_size,moeoComparator& _comparator, eoRng& _gen = rng) +{ + return *mo_deterministic_tournament(_pop.begin(), _pop.end(),_t_size,_comparator, _gen); +} + + +template +MOEOT& mo_deterministic_tournament(eoPop& _pop, unsigned int _t_size,moeoComparator& _comparator,eoRng& _gen = rng) +{ + return *mo_deterministic_tournament(_pop.begin(), _pop.end(), _t_size,_comparator, _gen); +} + + +template +It mo_stochastic_tournament(It _begin, It _end, double _t_rate,moeoComparator& _comparator ,eoRng& _gen = rng) +{ + It i1 = _begin + _gen.random(_end - _begin); + It i2 = _begin + _gen.random(_end - _begin); + + bool return_better = _gen.flip(_t_rate); + + if (_comparator(*i1, *i2)) + { + if (return_better) return i2; + // else + + return i1; + } + else + { + if (return_better) return i1; + // else + } + // else + + return i2; +} + + +template +const MOEOT& mo_stochastic_tournament(const eoPop& _pop, double _t_rate,moeoComparator& _comparator, eoRng& _gen = rng) +{ + return *mo_stochastic_tournament(_pop.begin(), _pop.end(), _t_rate,_comparator, _gen); +} + + +template +MOEOT& mo_stochastic_tournament(eoPop& _pop, double _t_rate, eoRng& _gen = rng) +{ + return *mo_stochastic_tournament(_pop.begin(), _pop.end(), _t_rate, _gen); +} + + +template +It mo_roulette_wheel(It _begin, It _end, double total, eoRng& _gen = rng) +{ + + float roulette = _gen.uniform(total); + + if (roulette == 0.0) // covers the case where total==0.0 + return _begin + _gen.random(_end - _begin); // uniform choice + + It i = _begin; + + while (roulette > 0.0) + { + roulette -= static_cast(*(i++)); + } + + return --i; +} + + +template +const MOEOT& mo_roulette_wheel(const eoPop& _pop, double total, eoRng& _gen = rng) +{ + float roulette = _gen.uniform(total); + + if (roulette == 0.0) // covers the case where total==0.0 + return _pop[_gen.random(_pop.size())]; // uniform choice + + typename eoPop::const_iterator i = _pop.begin(); + + while (roulette > 0.0) + { + roulette -= static_cast((i++)->fitness()); + } + + return *--i; +} + + +template +MOEOT& mo_roulette_wheel(eoPop& _pop, double total, eoRng& _gen = rng) +{ + float roulette = _gen.uniform(total); + + if (roulette == 0.0) // covers the case where total==0.0 + return _pop[_gen.random(_pop.size())]; // uniform choice + + typename eoPop::iterator i = _pop.begin(); + + while (roulette > 0.0) + { + // fitness only + roulette -= static_cast((i++)->fitness()); + } + + return *--i; +} + + +#endif /*MOEOSELECTORS_H_*/ + + + + + + + + + diff --git a/branches/paradiseo-moeo-1.0/src/selection/moeoStochTournamentSelect.h b/branches/paradiseo-moeo-1.0/src/selection/moeoStochTournamentSelect.h new file mode 100644 index 000000000..61365c038 --- /dev/null +++ b/branches/paradiseo-moeo-1.0/src/selection/moeoStochTournamentSelect.h @@ -0,0 +1,91 @@ +// -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*- + +//----------------------------------------------------------------------------- +// moeoStochTournamentSelect.h +// (c) OPAC Team (LIFL), Dolphin Project (INRIA), 2007 +/* + This library... + + Contact: paradiseo-help@lists.gforge.inria.fr, http://paradiseo.gforge.inria.fr + */ +//----------------------------------------------------------------------------- + +#ifndef MOEOSTOCHTOURNAMENTSELECT_H_ +#define MOEOSTOCHTOURNAMENTSELECT_H_ + +#include +#include +#include +#include + +/** + * Selection strategy that selects ONE individual by stochastic tournament. + */ +template < class MOEOT > class moeoStochTournamentSelect:public moeoSelectOne +{ +public: + + /** + * Full Ctor + * @param _comparator the comparator (used to compare 2 individuals) + * @param _tRate the tournament rate + */ + moeoStochTournamentSelect (moeoComparator < MOEOT > & _comparator, double _tRate = 1.0) : comparator (_comparator), tRate (_tRate) + { + // consistency checks + if (tRate < 0.5) + { + std::cerr << "Warning, Tournament rate should be > 0.5\nAdjusted to 0.55\n"; + tRate = 0.55; + } + if (tRate > 1) + { + std::cerr << "Warning, Tournament rate should be < 1\nAdjusted to 1\n"; + tRate = 1; + } + } + + + /** + * Ctor without comparator. A moeoFitnessThenDiversityComparator is used as default. + * @param _tRate the tournament rate + */ + moeoStochTournamentSelect (double _tRate = 1.0) : comparator (defaultComparator), tRate (_tRate) + { + // consistency checks + if (tRate < 0.5) + { + std::cerr << "Warning, Tournament rate should be > 0.5\nAdjusted to 0.55\n"; + tRate = 0.55; + } + if (tRate > 1) + { + std::cerr << "Warning, Tournament rate should be < 1\nAdjusted to 1\n"; + tRate = 1; + } + } + + + /** + * Apply the tournament to the given population + * @param _pop the population + */ + const MOEOT & operator() (const eoPop < MOEOT > &_pop) + { + // use the selector + return mo_stochastic_tournament(_pop,tRate,comparator); + } + + +protected: + + /** the comparator (used to compare 2 individuals) */ + moeoComparator < MOEOT > & comparator; + /** a fitness then diversity comparator can be used as default */ + moeoFitnessThenDiversityComparator < MOEOT > defaultComparator; + /** the tournament rate */ + double tRate; + +}; + +#endif /*MOEOSTOCHTOURNAMENTSELECT_H_ */