ElitistSelection.h
Go to the documentation of this file.
1/*!
2 *
3 *
4 * \brief Elitist Selection Operator suitable for (mu,lambda) and (mu+lambda) selection
5 *
6 *
7 * \author O.Krause
8 * \date 2014
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_ELITIST_SELECTION_H
32#define SHARK_ALGORITHMS_DIRECTSEARCH_OPERATORS_SELECTION_ELITIST_SELECTION_H
33
34#include <shark/LinAlg/Base.h>
35#include <vector>
36#include <numeric>
37namespace shark {
38
39/// \brief Survival selection to find the next parent set
40///
41/// Given a set of individuals, selects the mu best performing individuals.
42/// The elements are ordered using the given Ordering Relation
43template< typename Ordering >
45
46 /// \brief Selects individuals from the range of individuals.
47 ///
48 /// \param [in] it Iterator pointing to the first valid parent individual.
49 /// \param [in] itE Iterator pointing to the first invalid parent individual.
50 /// \param [in] out Iterator pointing to the first valid element of the output range.
51 /// \param [in] outE Iterator pointing to the first invalid element of the output range.
52 template<typename InIterator,typename OutIterator>
54 InIterator it, InIterator itE,
55 OutIterator out, OutIterator outE
56 ){
57 std::size_t outputSize = std::distance( out, outE );
58 std::vector<InIterator> results = order(it, itE);
59 SHARK_RUNTIME_CHECK(results.size() > outputSize, "Input range must be bigger than output range");
60
61 for(std::size_t i = 0; i != outputSize; ++i, ++out){
62 *out = *results[i];
63 }
64 }
65
66 /// \brief Selects individuals from the range of individuals.
67 ///
68 /// Instead of using an output range, surviving individuals are marked as selected.
69 ///
70 /// \param [in] population The population where individuals are selected from
71 /// \param [in] mu number of individuals to select
72 template<typename Population>
74 Population& population,std::size_t mu
75 ){
76 SHARK_RUNTIME_CHECK(population.size() >= mu, "Population Size must be at least mu");
77
78 typedef typename Population::iterator InIterator;
79 std::vector<InIterator> results = order(population.begin(),population.end());
80
81 for(std::size_t i = 0; i != mu; ++i){
82 results[i]->select()=true;
83 }
84 for(std::size_t i = mu; i != results.size(); ++i){
85 results[i]->select() = false;
86 }
87 }
88private:
89 /// Returns a sorted range of pairs indicating, how often every individual won.
90 /// The best individuals are in the back of the range.
91 template<class InIterator>
92 std::vector<InIterator> order(InIterator it, InIterator itE){
93 std::size_t size = std::distance( it, itE );
94 std::vector<InIterator > individuals(size);
95 std::iota(individuals.begin(),individuals.end(),it);
96 std::sort(
97 individuals.begin(),
98 individuals.end(),
99 [](InIterator lhs, InIterator rhs){
100 Ordering ordering;
101 return ordering(*lhs,*rhs);
102 }
103 );
104 return individuals;
105 }
106};
107
108}
109
110#endif