moeoSelectors.h

00001 /* 
00002 * <moeoSelectors.h>
00003 * Copyright (C) DOLPHIN Project-Team, INRIA Futurs, 2006-2007
00004 * (C) OPAC Team, LIFL, 2002-2007
00005 *
00006 * Arnaud Liefooghe
00007 *
00008 * This software is governed by the CeCILL license under French law and
00009 * abiding by the rules of distribution of free software.  You can  use,
00010 * modify and/ or redistribute the software under the terms of the CeCILL
00011 * license as circulated by CEA, CNRS and INRIA at the following URL
00012 * "http://www.cecill.info".
00013 *
00014 * As a counterpart to the access to the source code and  rights to copy,
00015 * modify and redistribute granted by the license, users are provided only
00016 * with a limited warranty  and the software's author,  the holder of the
00017 * economic rights,  and the successive licensors  have only  limited liability.
00018 *
00019 * In this respect, the user's attention is drawn to the risks associated
00020 * with loading,  using,  modifying and/or developing or reproducing the
00021 * software by the user in light of its specific status of free software,
00022 * that may mean  that it is complicated to manipulate,  and  that  also
00023 * therefore means  that it is reserved for developers  and  experienced
00024 * professionals having in-depth computer knowledge. Users are therefore
00025 * encouraged to load and test the software's suitability as regards their
00026 * requirements in conditions enabling the security of their systems and/or
00027 * data to be ensured and,  more generally, to use and operate it in the
00028 * same conditions as regards security.
00029 * The fact that you are presently reading this means that you have had
00030 * knowledge of the CeCILL license and that you accept its terms.
00031 *
00032 * ParadisEO WebSite : http://paradiseo.gforge.inria.fr
00033 * Contact: paradiseo-help@lists.gforge.inria.fr
00034 *
00035 */
00036 //-----------------------------------------------------------------------------
00037 
00038 #ifndef MOEOSELECTORS_H_
00039 #define MOEOSELECTORS_H_
00040 
00041 #include <comparator/moeoComparator.h>
00042 
00043 
00044 template <class It,class MOEOT>
00045 It mo_deterministic_tournament(It _begin, It _end, unsigned int _t_size,moeoComparator<MOEOT>& _comparator ,eoRng& _gen = rng)
00046 {
00047     It best = _begin + _gen.random(_end - _begin);
00048 
00049     for (unsigned int i = 0; i < _t_size - 1; ++i)
00050     {
00051         It competitor = _begin + _gen.random(_end - _begin);
00052         // compare the two individuals by using the comparator
00053         if (_comparator(*best, *competitor))
00054             // best "better" than competitor
00055             best=competitor;
00056     }
00057     return best;
00058 }
00059 
00060 
00061 template <class MOEOT>
00062 const MOEOT& mo_deterministic_tournament(const eoPop<MOEOT>& _pop, unsigned int _t_size,moeoComparator<MOEOT>& _comparator, eoRng& _gen = rng)
00063 {
00064     return *mo_deterministic_tournament(_pop.begin(), _pop.end(),_t_size,_comparator, _gen);
00065 }
00066 
00067 
00068 template <class MOEOT>
00069 MOEOT& mo_deterministic_tournament(eoPop<MOEOT>& _pop, unsigned int _t_size,moeoComparator<MOEOT>& _comparator,eoRng& _gen = rng)
00070 {
00071     return *mo_deterministic_tournament(_pop.begin(), _pop.end(), _t_size,_comparator, _gen);
00072 }
00073 
00074 
00075 template <class It,class MOEOT>
00076 It mo_stochastic_tournament(It _begin, It _end, double _t_rate,moeoComparator<MOEOT>& _comparator ,eoRng& _gen = rng)
00077 {
00078     It i1 = _begin + _gen.random(_end - _begin);
00079     It i2 = _begin + _gen.random(_end - _begin);
00080 
00081     bool return_better = _gen.flip(_t_rate);
00082 
00083     if (_comparator(*i1, *i2))
00084     {
00085         if (return_better) return i2;
00086         // else
00087 
00088         return i1;
00089     }
00090     else
00091     {
00092         if (return_better) return i1;
00093         // else
00094     }
00095     // else
00096 
00097     return i2;
00098 }
00099 
00100 
00101 template <class MOEOT>
00102 const MOEOT& mo_stochastic_tournament(const eoPop<MOEOT>& _pop, double _t_rate,moeoComparator<MOEOT>& _comparator, eoRng& _gen = rng)
00103 {
00104     return *mo_stochastic_tournament(_pop.begin(), _pop.end(), _t_rate,_comparator, _gen);
00105 }
00106 
00107 
00108 template <class MOEOT>
00109 MOEOT& mo_stochastic_tournament(eoPop<MOEOT>& _pop, double _t_rate, eoRng& _gen = rng)
00110 {
00111     return *mo_stochastic_tournament(_pop.begin(), _pop.end(), _t_rate, _gen);
00112 }
00113 
00114 
00115 template <class It>
00116 It mo_roulette_wheel(It _begin, It _end, double total, eoRng& _gen = rng)
00117 {
00118 
00119     float roulette = _gen.uniform(total);
00120 
00121     if (roulette == 0.0)           // covers the case where total==0.0
00122         return _begin + _gen.random(_end - _begin); // uniform choice
00123 
00124     It i = _begin;
00125 
00126     while (roulette > 0.0)
00127     {
00128         roulette -= static_cast<double>(*(i++));
00129     }
00130 
00131     return --i;
00132 }
00133 
00134 
00135 template <class MOEOT>
00136 const MOEOT& mo_roulette_wheel(const eoPop<MOEOT>& _pop, double total, eoRng& _gen = rng)
00137 {
00138     float roulette = _gen.uniform(total);
00139 
00140     if (roulette == 0.0)           // covers the case where total==0.0
00141         return _pop[_gen.random(_pop.size())]; // uniform choice
00142 
00143     typename eoPop<MOEOT>::const_iterator i = _pop.begin();
00144 
00145     while (roulette > 0.0)
00146     {
00147         roulette -= static_cast<double>((i++)->fitness());
00148     }
00149 
00150     return *--i;
00151 }
00152 
00153 
00154 template <class MOEOT>
00155 MOEOT& mo_roulette_wheel(eoPop<MOEOT>& _pop, double total, eoRng& _gen = rng)
00156 {
00157     float roulette = _gen.uniform(total);
00158 
00159     if (roulette == 0.0)           // covers the case where total==0.0
00160         return _pop[_gen.random(_pop.size())]; // uniform choice
00161 
00162     typename eoPop<MOEOT>::iterator i = _pop.begin();
00163 
00164     while (roulette > 0.0)
00165     {
00166         // fitness only
00167         roulette -= static_cast<double>((i++)->fitness());
00168     }
00169 
00170     return *--i;
00171 }
00172 
00173 
00174 #endif /*MOEOSELECTORS_H_*/
00175 
00176 
00177 
00178 
00179 
00180 
00181 
00182 
00183 

Generated on Fri Oct 12 15:16:04 2007 for ParadisEO-MOEO:MultiObjectiveEvolvingObjects by  doxygen 1.4.7