- 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 <comparator/moeoComparator.h>
    00017
    00018
    00019 template <class It,class MOEOT>
    00020 It mo_deterministic_tournament(It _begin, It _end, unsigned int _t_size,moeoComparator<MOEOT>& _comparator ,eoRng& _gen = rng)
    00021 {
    00022     It best = _begin + _gen.random(_end - _begin);
    00023
    00024     for (unsigned int i = 0; i < _t_size - 1; ++i)
    00025     {
    00026         It competitor = _begin + _gen.random(_end - _begin);
    00027         // compare the two individuals by using the comparator
    00028         if (_comparator(*best, *competitor))
    00029             // best "better" than competitor
    00030             best=competitor;
    00031     }
    00032     return best;
    00033 }
    00034
    00035
    00036 template <class MOEOT>
    00037 const MOEOT& mo_deterministic_tournament(const eoPop<MOEOT>& _pop, unsigned int _t_size,moeoComparator<MOEOT>& _comparator, eoRng& _gen = rng)
    00038 {
    00039     return *mo_deterministic_tournament(_pop.begin(), _pop.end(),_t_size,_comparator, _gen);
    00040 }
    00041
    00042
    00043 template <class MOEOT>
    00044 MOEOT& mo_deterministic_tournament(eoPop<MOEOT>& _pop, unsigned int _t_size,moeoComparator<MOEOT>& _comparator,eoRng& _gen = rng)
    00045 {
    00046     return *mo_deterministic_tournament(_pop.begin(), _pop.end(), _t_size,_comparator, _gen);
    00047 }
    00048
    00049
    00050 template <class It,class MOEOT>
    00051 It mo_stochastic_tournament(It _begin, It _end, double _t_rate,moeoComparator<MOEOT>& _comparator ,eoRng& _gen = rng)
    00052 {
    00053     It i1 = _begin + _gen.random(_end - _begin);
    00054     It i2 = _begin + _gen.random(_end - _begin);
    00055
    00056     bool return_better = _gen.flip(_t_rate);
    00057
    00058     if (_comparator(*i1, *i2))
    00059     {
    00060         if (return_better) return i2;
    00061         // else
    00062
    00063         return i1;
    00064     }
    00065     else
    00066     {
    00067         if (return_better) return i1;
    00068         // else
    00069     }
    00070     // else
    00071
    00072     return i2;
    00073 }
    00074
    00075
    00076 template <class MOEOT>
    00077 const MOEOT& mo_stochastic_tournament(const eoPop<MOEOT>& _pop, double _t_rate,moeoComparator<MOEOT>& _comparator, eoRng& _gen = rng)
    00078 {
    00079     return *mo_stochastic_tournament(_pop.begin(), _pop.end(), _t_rate,_comparator, _gen);
    00080 }
    00081
    00082
    00083 template <class MOEOT>
    00084 MOEOT& mo_stochastic_tournament(eoPop<MOEOT>& _pop, double _t_rate, eoRng& _gen = rng)
    00085 {
    00086     return *mo_stochastic_tournament(_pop.begin(), _pop.end(), _t_rate, _gen);
    00087 }
    00088
    00089
    00090 template <class It>
    00091 It mo_roulette_wheel(It _begin, It _end, double total, eoRng& _gen = rng)
    00092 {
    00093
    00094     float roulette = _gen.uniform(total);
    00095
    00096     if (roulette == 0.0)           // covers the case where total==0.0
    00097         return _begin + _gen.random(_end - _begin); // uniform choice
    00098
    00099     It i = _begin;
    00100
    00101     while (roulette > 0.0)
    00102     {
    00103         roulette -= static_cast<double>(*(i++));
    00104     }
    00105
    00106     return --i;
    00107 }
    00108
    00109
    00110 template <class MOEOT>
    00111 const MOEOT& mo_roulette_wheel(const eoPop<MOEOT>& _pop, double total, eoRng& _gen = rng)
    00112 {
    00113     float roulette = _gen.uniform(total);
    00114
    00115     if (roulette == 0.0)           // covers the case where total==0.0
    00116         return _pop[_gen.random(_pop.size())]; // uniform choice
    00117
    00118     typename eoPop<MOEOT>::const_iterator i = _pop.begin();
    00119
    00120     while (roulette > 0.0)
    00121     {
    00122         roulette -= static_cast<double>((i++)->fitness());
    00123     }
    00124
    00125     return *--i;
    00126 }
    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 only
    00142         roulette -= static_cast<double>((i++)->fitness());
    00143     }
    00144
    00145     return *--i;
    00146 }
    00147
    00148
    00149 #endif /*MOEOSELECTORS_H_*/
    00150
    00151
    00152
    00153
    00154
    00155
    00156
    00157
    00158