CMA.h
Go to the documentation of this file.
1//===========================================================================
2/*!
3 *
4 *
5 * \brief Implements the most recent version of the non-elitist CMA-ES.
6 *
7 * Hansen, N. The CMA Evolution Startegy: A Tutorial, June 28, 2011
8 * and the eqation numbers refer to this publication (retrieved April 2014).
9 *
10 *
11 * \author Thomas Voss and Christian Igel
12 * \date April 2014
13 *
14 * \par Copyright 1995-2017 Shark Development Team
15 *
16 * <BR><HR>
17 * This file is part of Shark.
18 * <https://shark-ml.github.io/Shark/>
19 *
20 * Shark is free software: you can redistribute it and/or modify
21 * it under the terms of the GNU Lesser General Public License as published
22 * by the Free Software Foundation, either version 3 of the License, or
23 * (at your option) any later version.
24 *
25 * Shark is distributed in the hope that it will be useful,
26 * but WITHOUT ANY WARRANTY; without even the implied warranty of
27 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28 * GNU Lesser General Public License for more details.
29 *
30 * You should have received a copy of the GNU Lesser General Public License
31 * along with Shark. If not, see <http://www.gnu.org/licenses/>.
32 *
33 */
34//===========================================================================
35
36
37#ifndef SHARK_ALGORITHMS_DIRECT_SEARCH_CMA_H
38#define SHARK_ALGORITHMS_DIRECT_SEARCH_CMA_H
39
44
45
46namespace shark {
47/// \brief Implements the CMA-ES.
48///
49/// The algorithm is described in
50///
51/// Hansen, N., S. Kern (2004). Evaluating the CMA Evolution Strategy
52/// on Multimodal Test Functions. In Proceedings of the Eighth
53/// International Conference on Parallel Problem Solving from Nature
54/// (PPSN VIII), pp. 282-291, LNCS, Springer-Verlag
55///
56/// For noisy function, noise handling is supported using the
57/// noise level detection algorithm described in
58/// Hansen, N., et al. "A method for handling uncertainty in evolutionary
59/// optimization with an application to feedback control of combustion."
60/// IEEE Transactions on Evolutionary Computation 13.1 (2009): 180-197.
61/// Our implementation varies in small details, e.g. instead of the average rank
62/// the rank of the average function value is used for updating the strategy parameters
63/// which ensures asymptotic unbiasedness. We further do not have an upper bound on
64/// the number of reevaluations for the same reason.
65/// \ingroup singledirect
66class CMA : public AbstractSingleObjectiveOptimizer<RealVector >
67{
68public:
69 /// \brief Models the recombination type.
75
76 /// \brief Default c'tor.
77 SHARK_EXPORT_SYMBOL CMA(random::rng_type& rng = random::globalRng);
78
79 /// \brief From INameable: return the class name.
80 std::string name() const
81 { return "CMA-ES"; }
82
83 /// \brief Calculates lambda for the supplied dimensionality n.
84 SHARK_EXPORT_SYMBOL static std::size_t suggestLambda( std::size_t dimension ) ;
85
86 /// \brief Calculates mu for the supplied lambda and the recombination strategy.
87 SHARK_EXPORT_SYMBOL static std::size_t suggestMu( std::size_t lambda, RecombinationType recomb = SUPERLINEAR ) ;
88
90 SHARK_EXPORT_SYMBOL void write( OutArchive & archive ) const;
91
93 /// \brief Initializes the algorithm for the supplied objective function.
95
96 /// \brief Initializes the algorithm for the supplied objective function.
98 ObjectiveFunctionType const& function,
99 SearchPointType const& initialSearchPoint,
100 std::size_t lambda,
101 std::size_t mu,
102 double initialSigma,
103 const boost::optional< RealMatrix > & initialCovarianceMatrix = boost::optional< RealMatrix >()
104 );
105
106 /// \brief Executes one iteration of the algorithm.
108
109 /// \brief sets the initial step length sigma
110 ///
111 /// It is by default <=0 which means that sigma =1/sqrt(numVariables)
112 void setInitialSigma(double initSigma){
113 m_initSigma = initSigma;
114 }
115
116 /// \brief Accesses the current step size.
117 double sigma() const {
118 return m_sigma;
119 }
120
121 /// \brief Accesses the current population mean.
122 RealVector const& mean() const {
123 return m_mean;
124 }
125
126 /// \brief Accesses the current weighting vector.
127 RealVector const& weights() const {
128 return m_weights;
129 }
130
131 /// \brief Accesses the evolution path for the covariance matrix update.
132 RealVector const& evolutionPath() const {
133 return m_evolutionPathC;
134 }
135
136 /// \brief Accesses the evolution path for the step size update.
137 RealVector const& evolutionPathSigma() const {
138 return m_evolutionPathSigma;
139 }
140
141 /// \brief Accesses the covariance matrix of the normal distribution used for generating offspring individuals.
142 RealMatrix const& covarianceMatrix() const {
143 return m_mutationDistribution.covarianceMatrix();
144 }
145
146 /// \brief Accesses the recombination type.
148 return m_recombinationType;
149 }
150
151 ///\brief Returns a mutable reference to the recombination type.
153 return m_recombinationType;
154 }
155
156 ///\brief Returns a const reference to the lower bound on sigma times smalles eigenvalue.
157 const double & lowerBound() const {
158 return m_lowerBound;
159 }
160
161 ///\brief Set the lower bound on sigma times smalles eigenvalue.
163 m_lowerBound = lowerBound;
164 }
165
166 /// \brief Returns the size of the parent population \f$\mu\f$.
167 std::size_t mu() const {
168 return m_mu;
169 }
170
171 /// \brief Sets the number of selected samples
172 void setMu(std::size_t mu){
173 m_mu = mu;
174 m_userSetMu = true;
175 }
176 /// \brief Sets the number of sampled points
177 void setLambda(std::size_t lambda){
178 m_lambda = lambda;
179 m_userSetLambda = true;
180 }
181
182 ///\brief Returns a immutable reference to the size of the offspring population \f$\mu\f$.
183 std::size_t lambda()const{
184 return m_lambda;
185 }
186
187 /// \brief Returns eigenvectors of covariance matrix (not considering step size)
188 RealMatrix const& eigenVectors() const {
189 return m_mutationDistribution.eigenVectors();
190 }
191
192 ///\brief Returns a eigenvectors of covariance matrix (not considering step size)
193 RealVector const& eigenValues() const {
194 return m_mutationDistribution.eigenValues();
195 }
196
197 ///\brief Returns condition of covariance matrix
198 double condition() const {
199 RealVector const& eigenValues = m_mutationDistribution.eigenValues();
200 return max(eigenValues)/min(eigenValues);
201 }
202
203 ///\brief Returns how often a point is evaluated
204 std::size_t numberOfEvaluations()const{
205 return m_numEvaluations;
206 }
207
208
209protected:
210 /// \brief The type of individual used for the CMA
212
213 /// \brief Samples lambda individuals from the search distribution
214 SHARK_EXPORT_SYMBOL std::vector<IndividualType> generateOffspring( ) const;
215
216 /// \brief Updates the strategy parameters based on the supplied offspring population.
217 SHARK_EXPORT_SYMBOL void updatePopulation( std::vector<IndividualType > const& offspring ) ;
218
220 std::vector<SearchPointType> const& points,
221 std::vector<ResultType> const& functionValues,
222 std::size_t lambda,
223 std::size_t mu,
224 double initialSigma
225 );
226private:
227
228 std::size_t m_numberOfVariables; ///< Stores the dimensionality of the search space.
229 std::size_t m_mu; ///< The size of the parent population.
230 std::size_t m_lambda; ///< The size of the offspring population, needs to be larger than mu.
231
232 bool m_userSetMu; /// <The user set a value via setMu, do not overwrite with default
233 bool m_userSetLambda; /// <The user set a value via setMu, do not overwrite with default
234 double m_initSigma; ///< use provided initial value of sigma<=0 =>algorithm chooses
235
236 RecombinationType m_recombinationType; ///< Stores the recombination type.
237
238
239 double m_sigma;
240 double m_cC;
241 double m_c1;
242 double m_cMu;
243 double m_cSigma;
244 double m_dSigma;
245 double m_muEff;
246
247 double m_lowerBound;
248
249 RealVector m_mean;
250 RealVector m_weights;
251
252 RealVector m_evolutionPathC;
253 RealVector m_evolutionPathSigma;
254
255 std::size_t m_counter; ///< counter for generations
256
257 std::size_t m_numEvaluations;
258 double m_numEvalIncreaseFactor;
259 double m_rLambda;
260 double m_rankChangeQuantile;
261
262 MultiVariateNormalDistribution m_mutationDistribution;
263 random::rng_type* mpe_rng;
264};
265}
266
267#endif