EPTournamentSelection.h
Go to the documentation of this file.
1 /**
2  *
3  * \brief EP-Tournament selection operator.
4  * \author T.Voss
5  * \date 2010-2011
6  *
7  * \par Copyright (c):
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  * <BR><HR>
18  * This file is part of Shark. This library is free software;
19  * you can redistribute it and/or modify it under the terms of the
20  * GNU General Public License as published by the Free Software
21  * Foundation; either version 3, or (at your option) any later version.
22  *
23  * This library is distributed in the hope that it will be useful,
24  * but WITHOUT ANY WARRANTY; without even the implied warranty of
25  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26  * GNU General Public License for more details.
27  *
28  * You should have received a copy of the GNU General Public License
29  * along with this library; if not, see <http://www.gnu.org/licenses/>.
30  *
31  */
32 #ifndef SHARK_ALGORITHMS_DIRECTSEARCH_OPERATORS_SELECTION_EP_TOURNAMENT_H
33 #define SHARK_ALGORITHMS_DIRECTSEARCH_OPERATORS_SELECTION_EP_TOURNAMENT_H
34 
37 
38 namespace shark {
39 
40  /**
41  * \brief Selects individuals from the range of parent and offspring individuals.
42  */
43  template< typename FitnessType >
45 
46  /** \brief Marks the user selected fitness type. */
47  typedef FitnessType fitness_type;
48 
49  /**
50  * \brief Selects individuals from the range of parent and offspring individuals.
51  * \param [in] parents Iterator pointing to the first valid parent individual.
52  * \param [in] parentsE Iterator pointing to the first invalid parent individual.
53  * \param [in] offspring Iterator pointing to the first valid offspring individual.
54  * \param [in] offspringE Iterator pointing to the first invalid offspring individual.
55  * \param [in] out Iterator pointing to the first valid element of the output range.
56  * \param [in] outE Iterator pointing to the first invalid element of the output range.
57  * \param [in] tournamentSize Number of individuals participating in tournament.
58  *
59  */
60  template<
61  typename InIterator,
62  typename OutIterator
63  > void operator()( InIterator parents,
64  InIterator parentsE,
65  InIterator offspring,
66  InIterator offspringE,
67  OutIterator out,
68  OutIterator outE,
69  std::size_t tournamentSize ) {
70 
72 
73  std::size_t noParents = std::distance( parents, parentsE );
74  std::size_t noOffspring = std::distance( offspring, offspringE );
75  std::size_t totalSize = noParents + noOffspring;
76 
77  std::vector< InIterator > view( totalSize );
78  typename std::vector< InIterator >::iterator itv = view.begin();
79 
80  for( InIterator it = parents; it != parentsE; ++it, ++itv ) {
81  it->fitness( shark::tag::ScaledFitness() )( 0 ) = 0.;
82  *itv = it;
83  for( std::size_t round = 0; round < tournamentSize; round++ ) {
84  std::size_t idx = shark::Rng::discrete( 0, totalSize-1 );
85  it->fitness( shark::tag::ScaledFitness() )( 0 ) += comp( *it, *( idx < noParents ? parents + idx : offspring + idx - noParents ) ) ? 1. : 0.;
86  }
87  }
88 
89  for( InIterator it = offspring; it != offspringE; ++it, ++itv ) {
90  it->fitness( shark::tag::ScaledFitness() )( 0 ) = 0.;
91  *itv = it;
92  for( std::size_t round = 0; round < tournamentSize; round++ ) {
93  std::size_t idx = shark::Rng::discrete( 0, totalSize-1 );
94  it->fitness( shark::tag::ScaledFitness() )( 0 ) += comp( *it, *( idx < noParents ? parents + idx : offspring + idx - noParents ) ) ? 1. : 0.;
95  }
96  }
97 
98  std::sort( view.begin(), view.end(), shark::IndirectFitnessComparator< FitnessType >() );
99 
100  itv = view.begin();
101  for( ; out != outE; ++out, ++itv )
102  *out = **itv;
103  }
104 
105  /**
106  * \brief Selects individuals from the range of parent and offspring individuals.
107  * \param [in] parents Range of parent individuals.
108  * \param [in] offspring Range of offspring individuals.
109  * \param [in] out Output range.
110  * \param [in] tournamentSize Number of individuals participating in tournament.
111  */
112  template<
113  typename InRange,
114  typename OutRange
115  > void operator()( InRange parents,
116  InRange offspring,
117  OutRange out,
118  std::size_t tournamentSize ) {
119  (*this)( parents.begin(), parents.end(), offspring.begin(), offspring.end(), out.begin(), out.end(), tournamentSize );
120  }
121  };
122 
123 }
124 
125 #endif