EPTournamentSelection.h
Go to the documentation of this file.
1/*!
2 *
3 *
4 * \brief EP-Tournament selection operator.
5 *
6 *
7 * \author T.Voss
8 * \date 2010-2011
9 *
10 *
11 * \par Copyright 1995-2017 Shark Development Team
12 *
13 * <BR><HR>
14 * This file is part of Shark.
15 * <https://shark-ml.github.io/Shark/>
16 *
17 * Shark is free software: you can redistribute it and/or modify
18 * it under the terms of the GNU Lesser General Public License as published
19 * by the Free Software Foundation, either version 3 of the License, or
20 * (at your option) any later version.
21 *
22 * Shark is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 * GNU Lesser General Public License for more details.
26 *
27 * You should have received a copy of the GNU Lesser General Public License
28 * along with Shark. If not, see <http://www.gnu.org/licenses/>.
29 *
30 */
31#ifndef SHARK_ALGORITHMS_DIRECTSEARCH_OPERATORS_SELECTION_EP_TOURNAMENT_H
32#define SHARK_ALGORITHMS_DIRECTSEARCH_OPERATORS_SELECTION_EP_TOURNAMENT_H
33
34#include <shark/LinAlg/Base.h>
36#include <vector>
37namespace shark {
38
39/// \brief Survival and mating selection to find the next parent set.
40///
41/// For a given Tournament size k, every individual is compared to k other individuals
42/// The ranking of the individuals is given by the template argument.
43/// The individuals which won the most tournaments are selected
44template< typename Ordering >
46
47 /// \brief Selects individuals from the range of individuals.
48 ///
49 /// \param [in] rng Random number generator.
50 /// \param [in] it Iterator pointing to the first valid parent individual.
51 /// \param [in] itE Iterator pointing to the first invalid parent individual.
52 /// \param [in] out Iterator pointing to the first valid element of the output range.
53 /// \param [in] outE Iterator pointing to the first invalid element of the output range.
54 template<typename InIterator,typename OutIterator>
56 rng_type& rng,
57 InIterator it, InIterator itE,
58 OutIterator out, OutIterator outE
59 ){
60 std::size_t outputSize = std::distance( out, outE );
61 std::vector<KeyValuePair<int, InIterator> > results = performTournament(rng, it, itE);
62 SHARK_RUNTIME_CHECK(results.size() > outputSize, "Input range must be bigger than output range");
63
64 for(std::size_t i = 0; i != outputSize; ++i, ++out){
65 *out = *results[i].value;
66 }
67 }
68
69 /// \brief Selects individuals from the range of individuals.
70 ///
71 /// Instead of using an output range, surviving individuals are marked as selected.
72 ///
73 /// \param [in] rng Random number generator.
74 /// \param [in] population The population where individuals are selected from
75 /// \param [in] mu number of individuals to select
76 template<typename Population>
78 rng_type& rng,
79 Population& population,
80 std::size_t mu
81 ){
82 SHARK_RUNTIME_CHECK(population.size() >= mu, "Population Size must be at least mu");
83 typedef typename Population::iterator InIterator;
84 std::vector<KeyValuePair<int, InIterator> > results = performTournament(rng, population.begin(),population.end());
85
86
87 for(std::size_t i = 0; i != mu; ++i){
88 individualPerform[i].value->select() = true;
89 }
90 for(std::size_t i = mu; i != results.size()-1; ++i){
91 individualPerform[i].value->select() = false;
92 }
93 }
94
95 /// \brief Size of the tournament. 4 by default.
96 std::size_t tournamentSize;
97private:
98 ///Returns a sorted range of pairs indicating, how often every individual won.
99 /// The best individuals are in the front of the range.
100 template<class InIterator>
101 std::vector<KeyValuePair<int, InIterator> > performTournament(rng_type& rng, InIterator it, InIterator itE){
102 std::size_t size = std::distance( it, itE );
103 UIntVector selectionProbability(size,0.0);
104 std::vector<KeyValuePair<int, InIterator> > individualPerformance(size);
105 Ordering smaller;
106 for( std::size_t i = 0; i != size(); ++i ) {
107 individualPerformance[i].value = it+i;
108 for( std::size_t round = 0; round < tournamentSize; round++ ) {
109 std::size_t idx = random::discrete(rng, 0,size-1 );
110 if(smaller(*it, *(it+idx)){
111 individualPerformance[i].key -= 1;
112 }
113 }
114 }
115
116 std::sort( individualPerformance.begin(), individualPerformance.end());
117 return individualPerformance;
118 }
119};
120
121}
122
123#endif