CMSA.h
Go to the documentation of this file.
1//===========================================================================
2/*!
3 *
4 *
5 * \brief Implements the CMSA.
6 *
7 * The algorithm is described in
8 *
9 * H. G. Beyer, B. Sendhoff (2008).
10 * Covariance Matrix Adaptation Revisited: The CMSA Evolution Strategy
11 * In Proceedings of the Tenth International Conference on Parallel Problem Solving from Nature
12 * (PPSN X), pp. 123-132, LNCS, Springer-Verlag
13 *
14 * \par Copyright (c) 1998-2008:
15 * Institut für Neuroinformatik
16 *
17 * \author -
18 * \date -
19 *
20 *
21 * \par Copyright 1995-2017 Shark Development Team
22 *
23 * <BR><HR>
24 * This file is part of Shark.
25 * <https://shark-ml.github.io/Shark/>
26 *
27 * Shark is free software: you can redistribute it and/or modify
28 * it under the terms of the GNU Lesser General Public License as published
29 * by the Free Software Foundation, either version 3 of the License, or
30 * (at your option) any later version.
31 *
32 * Shark is distributed in the hope that it will be useful,
33 * but WITHOUT ANY WARRANTY; without even the implied warranty of
34 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
35 * GNU Lesser General Public License for more details.
36 *
37 * You should have received a copy of the GNU Lesser General Public License
38 * along with Shark. If not, see <http://www.gnu.org/licenses/>.
39 *
40 */
41//===========================================================================
42
43
44#ifndef SHARK_ALGORITHMS_DIRECTSEARCH_CMSA_H
45#define SHARK_ALGORITHMS_DIRECTSEARCH_CMSA_H
46
51
52
53namespace shark {
54/// \brief Implements the CMSA.
55///
56/// The algorithm is described in
57///
58/// H. G. Beyer, B. Sendhoff (2008).
59/// Covariance Matrix Adaptation Revisited: The CMSA Evolution Strategy
60/// In Proceedings of the Tenth International Conference on Parallel Problem Solving from Nature
61/// (PPSN X), pp. 123-132, LNCS, Springer-Verlag
62/// \ingroup singledirect
63class CMSA : public AbstractSingleObjectiveOptimizer<RealVector > {
64 /** \cond */
65
66 struct LightChromosome {
67 RealVector step;
68 double sigma;
69 };
70 /** \endcond */
71public:
72
73 /// \brief Default c'tor.
74 CMSA(random::rng_type& rng = random::globalRng)
75 : m_mu( 100 )
76 , m_lambda( 200 )
77 , m_userSetMu(false)
78 ,m_userSetLambda(false)
79 , m_initSigma(0)
80 , mpe_rng(&rng){
82 }
83
84 /// \brief From INameable: return the class name.
85 std::string name() const
86 { return "CMSA"; }
87
89 SHARK_EXPORT_SYMBOL void write( OutArchive & archive ) const;
90
92
93 /// \brief Initializes the algorithm for the supplied objective function.
95
96 /**
97 * \brief Initializes the algorithm for the supplied objective function.
98 */
100 ObjectiveFunctionType const& function,
101 SearchPointType const& initialSearchPoint,
102 std::size_t lambda,
103 std::size_t mu,
104 double initialSigma,
105 const boost::optional< RealMatrix > & initialCovarianceMatrix = boost::optional< RealMatrix >()
106 );
107
108 /// \brief Executes one iteration of the algorithm.
110
111 /// \brief sets the initial step length sigma
112 ///
113 /// It is by default <=0 which means that sigma =1/sqrt(numVariables)
114 void setInitialSigma(double initSigma){
115 m_initSigma = initSigma;
116 }
117
118 /// \brief Sets the number of selected samples
119 void setMu(std::size_t mu){
120 m_mu = mu;
121 m_userSetMu = true;
122 }
123 /// \brief Sets the number of sampled points
124 void setLambda(std::size_t lambda){
125 m_lambda = lambda;
126 m_userSetLambda = true;
127 }
128 /// \brief Accesses the size of the parent population.
129 std::size_t mu() const {
130 return m_mu;
131 }
132
133 /// \brief Accesses the size of the offspring population.
134 std::size_t lambda() const {
135 return m_lambda;
136 }
137
138 RealVector eigenValues()const{
139 return sqr(diag(m_mutationDistribution.lowerCholeskyFactor()));
140 }
141
142 double sigma()const{
143 return m_sigma;
144 }
145protected:
146 /// \brief The type of individual used by the CMSA
148
149 /// \brief Samples lambda individuals from the search distribution
150 SHARK_EXPORT_SYMBOL std::vector<IndividualType> generateOffspring( ) const;
151
152 /// \brief Updates the strategy parameters based on the supplied offspring population.
153 SHARK_EXPORT_SYMBOL void updatePopulation( std::vector< IndividualType > const& offspring );
154
155 /// \brief Initializes the internal data structures of the CMSA
157 std::vector<SearchPointType> const& points,
158 std::vector<ResultType> const& functionValues,
159 std::size_t lambda,
160 std::size_t mu,
161 double initialSigma
162 );
163private:
164 std::size_t m_numberOfVariables; ///< Stores the dimensionality of the search space.
165 std::size_t m_mu; ///< The size of the parent population.
166 std::size_t m_lambda; ///< The size of the offspring population, needs to be larger than mu.
167
168 bool m_userSetMu; /// <The user set a value via setMu, do not overwrite with default
169 bool m_userSetLambda; /// <The user set a value via setMu, do not overwrite with default
170 double m_initSigma; ///< The initial step size
171
172 double m_sigma; ///< The current step size.
173 double m_cSigma;
174 double m_cC; ///< Constant for adapting the covariance matrix.
175
176 RealVector m_mean; ///< The current cog of the population.
177
178 MultiVariateNormalDistributionCholesky m_mutationDistribution; ///< Multi-variate normal mutation distribution.
179 random::rng_type* mpe_rng;
180};
181}
182
183#endif