TournamentSelection.h
Go to the documentation of this file.
1 /*!
2  *
3  *
4  * \brief Implements tournament selection.
5  *
6  * See http://en.wikipedia.org/wiki/Tournament_selection
7  *
8  *
9  * \author -
10  * \date -
11  *
12  *
13  * \par Copyright 1995-2017 Shark Development Team
14  *
15  * <BR><HR>
16  * This file is part of Shark.
17  * <http://shark-ml.org/>
18  *
19  * Shark is free software: you can redistribute it and/or modify
20  * it under the terms of the GNU Lesser General Public License as published
21  * by the Free Software Foundation, either version 3 of the License, or
22  * (at your option) any later version.
23  *
24  * Shark is distributed in the hope that it will be useful,
25  * but WITHOUT ANY WARRANTY; without even the implied warranty of
26  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27  * GNU Lesser General Public License for more details.
28  *
29  * You should have received a copy of the GNU Lesser General Public License
30  * along with Shark. If not, see <http://www.gnu.org/licenses/>.
31  *
32  */
33 #ifndef SHARK_ALGORITHMS_DIRECT_SEARCH_OPERATORS_SELECTION_TOURNAMENT_SELECTION_H
34 #define SHARK_ALGORITHMS_DIRECT_SEARCH_OPERATORS_SELECTION_TOURNAMENT_SELECTION_H
35 
36 #include <shark/Core/Exception.h>
37 #include <shark/Core/Random.h>
38 
39 namespace shark {
40 
41 /// \brief Tournament selection operator.
42 ///
43 /// Selects k individuals at random and returns the best. By default k is 2.
44 /// The Template parameter represents a Predicate that compares two individuals. It is assumed that this is
45 /// transitive, that is pred(a,b) = true && pred(b,c) == true => pred(a,c) == true.
46 /// Possible predicates could compare fitness values or the rank of the two individuals.
47 ///
48 /// The size of the tournament can either be set in the constructor or by setting the variable tournamentSize
49 template<class Predicate>
51  TournamentSelection(std::size_t size = 2){
52  tournamentSize = 2;
53  }
54 
55  template<typename IteratorType1, typename IteratorType2>
56  void operator()(
57  random::rng_type& rng,
58  IteratorType1 inIt,
59  IteratorType1 inItE,
60  IteratorType2 outIt,
61  IteratorType2 outItE
62  ){
63  for(; outIt != outItE; ++outIt ) {
64  *outIt = *(*this)(rng, inIt,inItE);
65  }
66  }
67 
68  /// \brief Selects an individual from the range of individuals with prob. proportional to its fitness.
69  /// \param [in] it Iterator pointing to the first valid element.
70  /// \param [in] itE Iterator pointing to the first invalid element.
71  /// \return An iterator pointing to the selected individual.
72  template< typename Iterator>
73  Iterator operator()(random::rng_type& rng, Iterator it, Iterator itE) const
74  {
75  std::size_t n = std::distance( it, itE );
76  SHARK_RUNTIME_CHECK(tournamentSize > 0, " Tournament size k needs to be larger than 0");
77  SHARK_RUNTIME_CHECK(n > tournamentSize, " Size of population needs to be larger than size of tournament");
78 
79  Predicate predicate;
80  Iterator result = it + random::discrete(rng, std::size_t(0), n-1 );
81  for( std::size_t i = 1; i < tournamentSize; i++ ) {
82  Iterator itt = it + random::discrete(rng, std::size_t(0),n-1);
83  if( predicate(*itt, *result) ){
84  result = itt;
85  }
86  }
87 
88  return result;
89  }
90 
91  /// \brief Size of the tournament. 2 by default.
92  std::size_t tournamentSize;
93 };
94 
95 }
96 
97 #endif