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  * <http://shark-ml.org/>
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 
47 #include <shark/Core/DLLSupport.h>
51 
52 
53 namespace shark {
54 /**
55 * \brief Implements the CMSA.
56 *
57 * The algorithm is described in
58 *
59 * H. G. Beyer, B. Sendhoff (2008).
60 * Covariance Matrix Adaptation Revisited: The CMSA Evolution Strategy
61 * In Proceedings of the Tenth International Conference on Parallel Problem Solving from Nature
62 * (PPSN X), pp. 123-132, LNCS, Springer-Verlag
63 */
64 class CMSA : public AbstractSingleObjectiveOptimizer<RealVector > {
65  /** \cond */
66 
67  struct LightChromosome {
68  RealVector step;
69  double sigma;
70  };
71  /** \endcond */
72 public:
73 
74  /// \brief Default c'tor.
75  CMSA(random::rng_type& rng = random::globalRng)
76  : m_mu( 100 )
77  , m_lambda( 200 )
78  , m_userSetMu(false)
79  ,m_userSetLambda(false)
80  , m_initSigma(0)
81  , mpe_rng(&rng){
83  }
84 
85  /// \brief From INameable: return the class name.
86  std::string name() const
87  { return "CMSA"; }
88 
89  SHARK_EXPORT_SYMBOL void read( InArchive & archive );
90  SHARK_EXPORT_SYMBOL void write( OutArchive & archive ) const;
91 
93 
94  /// \brief Initializes the algorithm for the supplied objective function.
95  SHARK_EXPORT_SYMBOL void init( ObjectiveFunctionType const& function, SearchPointType const& p);
96 
97  /**
98  * \brief Initializes the algorithm for the supplied objective function.
99  */
101  ObjectiveFunctionType const& function,
102  SearchPointType const& initialSearchPoint,
103  std::size_t lambda,
104  std::size_t mu,
105  double initialSigma,
106  const boost::optional< RealMatrix > & initialCovarianceMatrix = boost::optional< RealMatrix >()
107  );
108 
109  /// \brief Executes one iteration of the algorithm.
110  SHARK_EXPORT_SYMBOL void step(ObjectiveFunctionType const& function);
111 
112  /// \brief sets the initial step length sigma
113  ///
114  /// It is by default <=0 which means that sigma =1/sqrt(numVariables)
115  void setInitialSigma(double initSigma){
116  m_initSigma = initSigma;
117  }
118 
119  /// \brief Sets the number of selected samples
120  void setMu(std::size_t mu){
121  m_mu = mu;
122  m_userSetMu = true;
123  }
124  /// \brief Sets the number of sampled points
125  void setLambda(std::size_t lambda){
126  m_lambda = lambda;
127  m_userSetLambda = true;
128  }
129  /// \brief Accesses the size of the parent population.
130  std::size_t mu() const {
131  return m_mu;
132  }
133 
134  /// \brief Accesses the size of the offspring population.
135  std::size_t lambda() const {
136  return m_lambda;
137  }
138 
139  RealVector eigenValues()const{
140  return sqr(diag(m_mutationDistribution.lowerCholeskyFactor()));
141  }
142 
143  double sigma()const{
144  return m_sigma;
145  }
146 protected:
147  /// \brief The type of individual used by the CMSA
149 
150  /// \brief Samples lambda individuals from the search distribution
151  SHARK_EXPORT_SYMBOL std::vector<IndividualType> generateOffspring( ) const;
152 
153  /// \brief Updates the strategy parameters based on the supplied offspring population.
154  SHARK_EXPORT_SYMBOL void updatePopulation( std::vector< IndividualType > const& offspring );
155 
156  /// \brief Initializes the internal data structures of the CMSA
158  std::vector<SearchPointType> const& points,
159  std::vector<ResultType> const& functionValues,
160  std::size_t lambda,
161  std::size_t mu,
162  double initialSigma
163  );
164 private:
165  std::size_t m_numberOfVariables; ///< Stores the dimensionality of the search space.
166  std::size_t m_mu; ///< The size of the parent population.
167  std::size_t m_lambda; ///< The size of the offspring population, needs to be larger than mu.
168 
169  bool m_userSetMu; /// <The user set a value via setMu, do not overwrite with default
170  bool m_userSetLambda; /// <The user set a value via setMu, do not overwrite with default
171  double m_initSigma; ///< The initial step size
172 
173  double m_sigma; ///< The current step size.
174  double m_cSigma;
175  double m_cC; ///< Constant for adapting the covariance matrix.
176 
177  RealVector m_mean; ///< The current cog of the population.
178 
179  MultiVariateNormalDistributionCholesky m_mutationDistribution; ///< Multi-variate normal mutation distribution.
180  random::rng_type* mpe_rng;
181 };
182 }
183 
184 #endif