RouletteWheelSelection.h
Go to the documentation of this file.
1 /*!
2  *
3  *
4  * \brief Implements fitness proportional selection.
5  *
6  * See http://en.wikipedia.org/wiki/Fitness_proportionate_selection
7  *
8  *
9  *
10  * \author T. Voss
11 * \par Copyright (c) 1998-2008:
12 * Institut für Neuroinformatik
13  * \date -
14  *
15  *
16  * \par Copyright 1995-2017 Shark Development Team
17  *
18  * <BR><HR>
19  * This file is part of Shark.
20  * <http://shark-ml.org/>
21  *
22  * Shark is free software: you can redistribute it and/or modify
23  * it under the terms of the GNU Lesser General Public License as published
24  * by the Free Software Foundation, either version 3 of the License, or
25  * (at your option) any later version.
26  *
27  * Shark is distributed in the hope that it will be useful,
28  * but WITHOUT ANY WARRANTY; without even the implied warranty of
29  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
30  * GNU Lesser General Public License for more details.
31  *
32  * You should have received a copy of the GNU Lesser General Public License
33  * along with Shark. If not, see <http://www.gnu.org/licenses/>.
34  *
35  */
36 #ifndef SHARK_ALGORITHMS_DIRECTSEARCH_OPERATORS_SELECTION_ROULETTE_WHEEL_SELECTION_H
37 #define SHARK_ALGORITHMS_DIRECTSEARCH_OPERATORS_SELECTION_ROULETTE_WHEEL_SELECTION_H
38 
39 #include <shark/Core/Random.h>
40 #include <shark/LinAlg/Base.h>
41 
42 namespace shark {
43 /**
44 * \brief Fitness-proportional selection operator.
45 *
46 * See http://en.wikipedia.org/wiki/Fitness_proportionate_selection.
47 */
49  /**
50  * \brief Selects an individual from the range of individuals with prob. proportional to its fitness.
51  *
52  * \param [in] it Iterator pointing to the first valid element.
53  * \param [in] itE Iterator pointing to the first invalid element.
54  * \param [in] probabilities selection probabilities of the individuals
55  */
56  template<typename Rng, typename Iterator>
57  Iterator operator()(Rng& rng, Iterator it, Iterator itE, RealVector const& probabilities) const
58  {
59  std::size_t n = probabilities.size();
60  double rnd = random::uni(rng, 0,1);
61  double sum = 0;
62  for(std::size_t pos = 0; pos != n; ++pos,++it){
63  sum += probabilities(pos);
64  if(rnd <= sum)
65  return it;
66  }
67  return it;
68  }
69 };
70 }
71 
72 #endif