27 #ifndef SHARK_ALGORITHMS_DIRECT_SEARCH_LM_CMA_H 28 #define SHARK_ALGORITHMS_DIRECT_SEARCH_LM_CMA_H 55 class IncrementalCholeskyMatrix{
57 IncrementalCholeskyMatrix(){}
58 void init (
double alpha,std::size_t dimensions, std::size_t numVectors, std::size_t Nsteps){
59 m_vArr.resize(numVectors,dimensions);
60 m_pcArr.resize(numVectors,dimensions);
61 m_b.resize(numVectors);
62 m_d.resize(numVectors);
63 m_l.resize(numVectors);
66 m_maxStoredVectors = numVectors;
79 void prod(RealVector& x, T
const& z)
const{
81 double a = std::sqrt(1-m_alpha);
82 for(std::size_t j=0; j != m_j.size(); j++){
83 std::size_t jcur = m_j[j];
84 double k = m_b(jcur) *inner_prod(row(m_vArr,jcur),z);
85 noalias(x) = a*x+k*row(m_pcArr,jcur);
91 void inv(RealVector& x, T
const& z)
const{
95 void update(RealVector
const& newPc){
97 if (m_j.size() < m_maxStoredVectors)
99 std::size_t index = m_j.size();
100 m_j.push_back(index);
108 std::size_t dmin = m_l[m_j[1]] - m_l[m_j[0]];
110 for(std::size_t j=2; j != m_j.size(); j++)
112 std::size_t dcur = m_l[m_j[j]] - m_l[m_j[j-1]];
121 if (dmin >= m_Nsteps)
124 if (imin != m_j.size()-1)
126 std::size_t sav = m_j[imin];
127 for(std::size_t j = imin; j != m_j.size()-1; j++)
133 int newidx = m_j.back();
134 m_l[newidx] = m_counter;
135 noalias(row(m_pcArr,newidx)) = newPc;
142 for(std::size_t i = imin; i != m_j.size(); ++i)
145 inv(v,row(m_pcArr,index),i);
146 noalias(row(m_vArr,index)) = v;
148 double normv2 = norm_sqr(row(m_vArr,index));
149 double c = std::sqrt(1.0-m_alpha);
150 double f = std::sqrt(1+m_alpha/(1-m_alpha)*normv2);
151 m_b[index] = c/normv2*(f-1);
152 m_d[index] = 1/(c*normv2)*(1-1/f);
158 void inv(RealVector& x, T
const& z,std::size_t k)
const{
160 double c= 1.0/std::sqrt(1-m_alpha);
161 for(std::size_t j=0; j != k; j++){
162 std::size_t jcur = m_j[j];
163 double k = m_d(jcur) * inner_prod(row(m_vArr,jcur),x);
164 noalias(x) = c*x - k*row(m_vArr,jcur);
175 std::vector<std::size_t> m_j;
176 std::vector<std::size_t> m_l;
177 std::size_t m_Nsteps;
178 std::size_t m_maxStoredVectors;
179 std::size_t m_counter;
203 m_features |= REQUIRES_VALUE;
209 {
return "LMCMA-ES"; }
213 unsigned lambda = unsigned( 4. + ::floor( 3. * ::log( static_cast<double>( dimension ) ) ) );
215 lambda = std::max<unsigned int>( 5, std::min( lambda, dimension ) );
228 unsigned int lambda = suggestLambda( p.size() );
229 unsigned int mu = suggestMu( lambda );
234 1.0/std::sqrt(
double(p.size()))
246 checkFeatures(
function);
248 m_numberOfVariables =
function.numberOfVariables();
250 m_mu =
static_cast<unsigned int>(::floor(mu));
253 m_mean = initialSearchPoint;
254 m_best.point = initialSearchPoint;
255 m_best.value =
function(initialSearchPoint);
258 m_stepSize.init(initialSigma);
261 m_weights.resize(m_mu);
262 for (
unsigned int i = 0; i < m_mu; i++){
263 m_weights(i) = ::log(mu + 0.5) - ::log(1. + i);
265 m_weights /= sum(m_weights);
268 m_muEff = 1. / sum(
sqr(m_weights));
269 double c1 = 1/(10*std::log(m_numberOfVariables+1.0));
273 m_evolutionPathC = blas::repeat(0.0,m_numberOfVariables);
274 m_A.init(c1,m_numberOfVariables,lambda,lambda);
280 std::vector< IndividualType > offspring( m_lambda );
283 for(
unsigned int i = 0; i < offspring.size(); i++ ) {
284 createSample(offspring[i].
searchPoint(),offspring[i].chromosome());
286 penalizingEvaluator(
function, offspring.begin(), offspring.end() );
291 std::vector< IndividualType > parents(
lambda() );
293 selection(offspring.begin(),offspring.end(),parents.begin(), parents.end());
294 updateStrategyParameters( parents );
297 m_best.point= parents[ 0 ].searchPoint();
298 m_best.value= parents[ 0 ].unpenalizedFitness();
303 return m_stepSize.stepSize();
307 RealVector
const&
mean()
const {
318 return m_evolutionPathC;
322 unsigned int mu()
const {
345 RealVector m( m_numberOfVariables, 0. );
346 for(
unsigned int j = 0; j < mu(); j++ ){
347 noalias(m) += m_weights( j ) * offspring[j].searchPoint();
351 noalias(m_evolutionPathC) = (1. - m_cC ) * m_evolutionPathC + std::sqrt( m_cC * (2. - m_cC) * m_muEff ) * (m - m_mean) / sigma();
358 m_A.update(m_evolutionPathC);
361 m_stepSize.update(offspring);
366 void createSample(RealVector& x,RealVector& z)
const{
367 x.resize(m_numberOfVariables);
368 z.resize(m_numberOfVariables);
369 for(std::size_t i = 0; i != m_numberOfVariables; ++i){
370 z(i) =
gauss(*mpe_rng,0,1);
373 noalias(x) = sigma()*x +m_mean;
376 unsigned int m_numberOfVariables;
378 unsigned int m_lambda;
383 detail::IncrementalCholeskyMatrix m_A;
387 RealVector m_weights;
390 RealVector m_evolutionPathC;
392 random::rng_type* mpe_rng;