LinearRanking.h
Go to the documentation of this file.
1/*!
2 *
3 *
4 * \brief Roulette-Wheel-Selection based on fitness-rank-based selection probability assignment.
5 *
6 * The algorithm is described in: James E. Baker. Adaptive Selection
7 * Methods for Genetic Algorithms. In John J. Grefenstette (ed.):
8 * Proceedings of the 1st International Conference on Genetic
9 * Algorithms (ICGA), pp. 101-111, Lawrence Erlbaum Associates, 1985
10 *
11 *
12 *
13 * \author T.Voss
14 * \date 2010-2011
15 *
16 *
17 * \par Copyright 1995-2017 Shark Development Team
18 *
19 * <BR><HR>
20 * This file is part of Shark.
21 * <https://shark-ml.github.io/Shark/>
22 *
23 * Shark is free software: you can redistribute it and/or modify
24 * it under the terms of the GNU Lesser General Public License as published
25 * by the Free Software Foundation, either version 3 of the License, or
26 * (at your option) any later version.
27 *
28 * Shark is distributed in the hope that it will be useful,
29 * but WITHOUT ANY WARRANTY; without even the implied warranty of
30 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
31 * GNU Lesser General Public License for more details.
32 *
33 * You should have received a copy of the GNU Lesser General Public License
34 * along with Shark. If not, see <http://www.gnu.org/licenses/>.
35 *
36 */
37#ifndef SHARK_ALGORITHMS_DIRECTSEARCH_OPERATORS_SELECTION_LINEARRANKING_H
38#define SHARK_ALGORITHMS_DIRECTSEARCH_OPERATORS_SELECTION_LINEARRANKING_H
39
41#include <vector>
42#include <numeric>
43
44namespace shark {
45
46/**
47 * \brief Implements a fitness-proportional selection scheme for mating selection that
48 * scales the fitness values linearly before carrying out the actual selection.
49 *
50 * The algorithm is described in: James E. Baker. Adaptive Selection
51 * Methods for Genetic Algorithms. In John J. Grefenstette (ed.):
52 * Proceedings of the 1st International Conference on Genetic
53 * Algorithms (ICGA), pp. 101-111, Lawrence Erlbaum Associates, 1985
54 *
55 */
56template<typename Ordering >
59 etaMax = 1.1;
60 }
61 /// \brief Selects individualss from the range of parent and offspring individuals.
62 ///
63 /// The operator carries out the following steps:
64 /// - Calculate selection probabilities of parent and offspring individualss
65 /// according to their rank, which is determined by the ordering relation
66 /// - Carry out roulette wheel selection on the range of parent and
67 /// offspring individualss until the output range is filled.
68 ///
69 /// \param [in] rng Random number generator.
70 /// \param [in] individuals Iterator pointing to the first valid individual.
71 /// \param [in] individualsE Iterator pointing to the first invalid individual.
72 /// \param [in] out Iterator pointing to the first valid element of the output range.
73 /// \param [in] outE Iterator pointing to the first invalid element of the output range.
74 ///
75 template<typename RngType, typename InIterator,typename OutIterator>
77 RngType& rng,
78 InIterator individuals,
79 InIterator individualsE,
80 OutIterator out,
81 OutIterator outE
82 ) const{
83
84 //compute rank of each individual
85 std::size_t size = std::distance( individuals, individualsE );
86 std::vector<InIterator> sortedIndividuals(size);
87 std::iota(sortedIndividuals.begin(),sortedIndividuals.end(),individuals);
88 std::sort(
89 sortedIndividuals.begin(),
90 sortedIndividuals.end(),
91 [](InIterator lhs, InIterator rhs){
92 Ordering ordering;
93 return ordering(*lhs,*rhs);
94 }
95 );
96
97 RealVector selectionProbability(size);
98 double a = 2. * (etaMax - 1.)/(size - 1.);
99 for( std::size_t i = 0; i != size; ++i ) {
100 selectionProbability[i] = (etaMax - a*i);
101 }
102 selectionProbability /=sum(selectionProbability);
103
105 for( ; out != outE; ++out ){
106 InIterator individuals = rws(rng, sortedIndividuals.begin(), sortedIndividuals.end(), selectionProbability)->value;
107 *out = *individuals;
108 }
109 }
110
111 /// \brief Selective pressure, parameter in [1,2] conrolling selection strength. 1.1 by default
112 double etaMax;
113
114};
115}
116
117#endif