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 * <https://shark-ml.github.io/Shark/>
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
42namespace 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] rng Random number generator.
53 * \param [in] it Iterator pointing to the first valid element.
54 * \param [in] itE Iterator pointing to the first invalid element.
55 * \param [in] probabilities selection probabilities of the individuals
56 */
57 template<typename Rng, typename Iterator>
58 Iterator operator()(Rng& rng, Iterator it, Iterator itE, RealVector const& probabilities) const
59 {
60 std::size_t n = probabilities.size();
61 double rnd = random::uni(rng, 0,1);
62 double sum = 0;
63 for(std::size_t pos = 0; pos != n; ++pos,++it){
64 sum += probabilities(pos);
65 if(rnd <= sum)
66 return it;
67 }
68 return it;
69 }
70};
71}
72
73#endif