TournamentSelection.h
Go to the documentation of this file.
1 /**
2 *
3 * \brief Implements tournament selection.
4 *
5 * See http://en.wikipedia.org/wiki/Tournament_selection
6 *
7 * \par Copyright (c) 1998-2008:
8 * Institut f&uuml;r Neuroinformatik<BR>
9 * Ruhr-Universit&auml;t Bochum<BR>
10 * D-44780 Bochum, Germany<BR>
11 * Phone: +49-234-32-25558<BR>
12 * Fax: +49-234-32-14209<BR>
13 * eMail: shark-admin@neuroinformatik.ruhr-uni-bochum.de<BR>
14 * www: http://www.neuroinformatik.ruhr-uni-bochum.de<BR>
15 * <BR>
16 *
17 *
18 * <BR><HR>
19 * This file is part of Shark. This library is free software;
20 * you can redistribute it and/or modify it under the terms of the
21 * GNU General Public License as published by the Free Software
22 * Foundation; either version 3, or (at your option) any later version.
23 *
24 * This library 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 General Public License for more details.
28 *
29 * You should have received a copy of the GNU General Public License
30 * along with this library; 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 
38 #include <shark/Rng/GlobalRng.h>
39 
40 namespace shark {
41 
42  /**
43  * \brief Tournament selection operator.
44  *
45  * See http://en.wikipedia.org/wiki/Fitness_proportionate_selection.
46  */
48  /**
49  * \brief Selects an individual from the range of individuals with prob. proportional to its fitness.
50  * \tparam Extractor Type for mapping elements from the underlying range on the real line.
51  * \param [in] it Iterator pointing to the first valid element.
52  * \param [in] itE Iterator pointing to the first invalid element.
53  * \param [in, out] e Object of type Extractor for mapping the elements from the underlying range on the real line.
54  * \param [in] k Size of the tournament, needs to be larger than 0.
55  * \returns An iterator pointing to the selected individual.
56  */
57  template< typename Iterator, typename Extractor >
58  Iterator operator()( Iterator it, Iterator itE, Extractor & e, std::size_t k ) const {
59 
60  if( k == 0 )
61  throw( SHARKEXCEPTION( "TournamentSelection: Tournament size k needs to be larger than 0" ) );
62 
63  std::size_t n = std::distance( it, itE );
64 
65  if( n < k )
66  throw( SHARKEXCEPTION( "TournamentSelecion: Size of population needs to be larger than size of tournament" ) );
67 
68  Iterator result = it + Rng::discrete( 0, n-1 );
69  Iterator itt;
70  for( std::size_t i = 1; i < k; i++ ) {
71  itt = it + static_cast<unsigned int>( Rng::uni() * n );
72 
73  if( e( *itt ) < e( *result ) ) {
74  std::swap( itt, result );
75  }
76  }
77 
78  return( result );
79  }
80  };
81 
82 }
83 
84 #endif