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 * <https://shark-ml.github.io/Shark/>
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
37#include <shark/Core/Random.h>
38
39namespace 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
49template<class Predicate>
51 TournamentSelection(std::size_t size = 2){
52 tournamentSize = size;
53 }
54
55 template<typename IteratorType1, typename IteratorType2>
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] rng Random number generator
70 /// \param [in] it Iterator pointing to the first valid element.
71 /// \param [in] itE Iterator pointing to the first invalid element.
72 /// \return An iterator pointing to the selected individual.
73 template< typename Iterator>
74 Iterator operator()(random::rng_type& rng, Iterator it, Iterator itE) const
75 {
76 std::size_t n = std::distance( it, itE );
77 SHARK_RUNTIME_CHECK(tournamentSize > 0, " Tournament size k needs to be larger than 0");
78 SHARK_RUNTIME_CHECK(n > tournamentSize, " Size of population needs to be larger than size of tournament");
79
80 Predicate predicate;
81 Iterator result = it + random::discrete(rng, std::size_t(0), n-1 );
82 for( std::size_t i = 1; i < tournamentSize; i++ ) {
83 Iterator itt = it + random::discrete(rng, std::size_t(0),n-1);
84 if( predicate(*itt, *result) ){
85 result = itt;
86 }
87 }
88
89 return result;
90 }
91
92 /// \brief Size of the tournament. 2 by default.
93 std::size_t tournamentSize;
94};
95
96}
97
98#endif