- Main Page
- Classes
- Files

- Search for |

# moeoSelectors.h

    00001 // -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
    00002
    00003 //-----------------------------------------------------------------------------
    00004 // moeoSelectors.h
    00005 // (c) OPAC Team (LIFL), Dolphin Project (INRIA), 2007
    00006 /*
    00007     This library...
    00008
    00009     Contact: paradiseo-help@lists.gforge.inria.fr, http://paradiseo.gforge.inria.fr
    00010  */
    00011 //-----------------------------------------------------------------------------
    00012
    00013 #ifndef MOEOSELECTORS_H_
    00014 #define MOEOSELECTORS_H_
    00015
    00016 #include <moeoComparator.h>
    00017
    00018
    00019 template <class It,class MOEOT>
    00020 It mo_deterministic_tournament(It _begin, It _end, unsigned _t_size,moeoComparator<MOEOT>& _comparator ,eoRng& _gen = rng)
    00021 {
    00022     It best = _begin + _gen.random(_end - _begin);
    00023
    00024     for (unsigned i = 0; i < _t_size - 1; ++i)
    00025     {
    00026         It competitor = _begin + _gen.random(_end - _begin);
    00027
    00028         // compare the two individuals by using the comparator
    00029         if (_comparator(*best,*competitor))
    00030
    00031             // best "better" than competitor
    00032             best=competitor;
    00033     }
    00034
    00035     return best;
    00036 }
    00037
    00038
    00039 template <class MOEOT>
    00040 const MOEOT& mo_deterministic_tournament(const eoPop<MOEOT>& _pop, unsigned _t_size,moeoComparator<MOEOT>& _comparator, eoRng& _gen = rng)
    00041 {
    00042     return *mo_deterministic_tournament(_pop.begin(), _pop.end(),_t_size,_comparator, _gen);
    00043 }
    00044
    00045 template <class MOEOT>
    00046 MOEOT& mo_deterministic_tournament(eoPop<MOEOT>& _pop, unsigned _t_size,moeoComparator<MOEOT>& _comparator,eoRng& _gen = rng)
    00047 {
    00048     return *mo_deterministic_tournament(_pop.begin(), _pop.end(), _t_size,_comparator, _gen);
    00049 }
    00050
    00051
    00052
    00053 template <class It,class MOEOT>
    00054 It mo_stochastic_tournament(It _begin, It _end, double _t_rate,moeoComparator<MOEOT>& _comparator ,eoRng& _gen = rng)
    00055 {
    00056     It i1 = _begin + _gen.random(_end - _begin);
    00057     It i2 = _begin + _gen.random(_end - _begin);
    00058
    00059     bool return_better = _gen.flip(_t_rate);
    00060
    00061     if (_comparator(*i1 , *i2))
    00062     {
    00063         if (return_better) return i2;
    00064         // else
    00065
    00066         return i1;
    00067     }
    00068     else
    00069     {
    00070         if (return_better) return i1;
    00071         // else
    00072     }
    00073     // else
    00074
    00075     return i2;
    00076 }
    00077
    00078 template <class MOEOT>
    00079 const MOEOT& mo_stochastic_tournament(const eoPop<MOEOT>& _pop, double _t_rate,moeoComparator<MOEOT>& _comparator, eoRng& _gen = rng)
    00080 {
    00081     return *mo_stochastic_tournament(_pop.begin(), _pop.end(), _t_rate,_comparator, _gen);
    00082 }
    00083
    00084 template <class MOEOT>
    00085 MOEOT& mo_stochastic_tournament(eoPop<MOEOT>& _pop, double _t_rate, eoRng& _gen = rng)
    00086 {
    00087     return *mo_stochastic_tournament(_pop.begin(), _pop.end(), _t_rate, _gen);
    00088 }
    00089
    00090
    00091
    00092 template <class It>
    00093 It mo_roulette_wheel(It _begin, It _end, double total, eoRng& _gen = rng)
    00094 {
    00095
    00096     float roulette = _gen.uniform(total);
    00097
    00098     if (roulette == 0.0)           // covers the case where total==0.0
    00099         return _begin + _gen.random(_end - _begin); // uniform choice
    00100
    00101     It i = _begin;
    00102
    00103     while (roulette > 0.0)
    00104     {
    00105         roulette -= static_cast<double>(*(i++));
    00106     }
    00107
    00108     return --i;
    00109 }
    00110
    00111 template <class MOEOT>
    00112 const MOEOT& mo_roulette_wheel(const eoPop<MOEOT>& _pop, double total, eoRng& _gen = rng)
    00113 {
    00114     float roulette = _gen.uniform(total);
    00115
    00116     if (roulette == 0.0)           // covers the case where total==0.0
    00117         return _pop[_gen.random(_pop.size())]; // uniform choice
    00118
    00119     typename eoPop<MOEOT>::const_iterator i = _pop.begin();
    00120
    00121     while (roulette > 0.0)
    00122     {
    00123         roulette -= static_cast<double>((i++)->fitness());
    00124     }
    00125
    00126     return *--i;
    00127 }
    00128
    00129 template <class MOEOT>
    00130 MOEOT& mo_roulette_wheel(eoPop<MOEOT>& _pop, double total, eoRng& _gen = rng)
    00131 {
    00132     float roulette = _gen.uniform(total);
    00133
    00134     if (roulette == 0.0)           // covers the case where total==0.0
    00135         return _pop[_gen.random(_pop.size())]; // uniform choice
    00136
    00137     typename eoPop<MOEOT>::iterator i = _pop.begin();
    00138
    00139     while (roulette > 0.0)
    00140     {
    00141         // fitness ?
    00142         roulette -= static_cast<double>((i++)->fitness());
    00143     }
    00144
    00145     return *--i;
    00146 }
    00147
    00148 #endif /*MOEOSELECTORS_H_*/
    00149
    00150
    00151
    00152
    00153
    00154
    00155
    00156
    00157