CCmaesCuda.cpp 17.4 KB
Newer Older
kruger's avatar
kruger committed
1 2 3 4 5 6 7 8 9 10
#include <math.h>
#include <time.h>
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include "include/CCmaesCuda.h"

//Functions for cma
long CaleatoireCuda::alea_Start(long unsigned inseed)
{
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
  long tmp;
  int i;

  this->flgstored = 0;
  this->startseed = inseed;
  if (inseed < 1)
    inseed = 1; 
  this->aktseed = inseed;
  for (i = 39; i >= 0; --i)
  {
    tmp = this->aktseed/127773;
    this->aktseed = 16807 * (this->aktseed - tmp * 127773)
        - 2836 * tmp;
    if (this->aktseed < 0) this->aktseed += 2147483647;
    if (i < 32)
      this->rgalea[i] = this->aktseed;
  }
  this->aktalea = this->rgalea[0];
  return inseed;
kruger's avatar
kruger committed
30 31 32 33
}

long CaleatoireCuda::alea_init(long unsigned inseed)
{
34 35 36 37 38 39 40 41 42
  clock_t cloc = clock();

  this->flgstored = 0;
  if (inseed < 1) {
    while ((long) (cloc - clock()) == 0)
      ; /* TODO: remove this for time critical applications? */
    inseed = (long)abs((long)(100*time(NULL)+clock()));
  }
  return this->alea_Start(inseed);
kruger's avatar
kruger committed
43 44 45 46
}

float CaleatoireCuda::alea_Uniform()
{
47 48 49 50 51 52 53 54 55 56 57
  long tmp;

  tmp = this->aktseed/127773;
  this->aktseed = 16807 * (this->aktseed - tmp * 127773)
      - 2836 * tmp;
  if (this->aktseed < 0) 
    this->aktseed += 2147483647;
  tmp = this->aktalea / 67108865;
  this->aktalea = this->rgalea[tmp];
  this->rgalea[tmp] = this->aktseed;
  return (float)(this->aktalea)/(2.147483647e9);
kruger's avatar
kruger committed
58 59 60 61
}

float CaleatoireCuda::alea_Gauss()
{
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
  float x1, x2, rquad, fac;

  if (this->flgstored)
  {    
    this->flgstored = 0;
    return this->hold;
  }
  do 
  {
    x1 = 2.0 * this->alea_Uniform() - 1.0;
    x2 = 2.0 * this->alea_Uniform() - 1.0;
    rquad = x1*x1 + x2*x2;
  } while(rquad >= 1 || rquad <= 0);
  fac = sqrt(-2.0*log(rquad)/rquad);
  this->flgstored = 1;
  this->hold = fac * x1;
  return fac * x2;
kruger's avatar
kruger committed
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
}

void CCmaesCuda::Cmaes_init_param(int lambda, int mu){
float s1, s2;
float t1, t2;
int i;
clock_t cloc = clock();

this->lambda = lambda;
this->mu = mu;
/*set weights*/
this->weights = (float*)malloc(this->mu*sizeof(float));
for (i=0; i<this->mu; ++i) 
      this->weights[i] = log(this->mu+1.)-log(i+1.);
/* normalize weights vector and set mueff */
s1=0., s2=0.;
for (i=0; i<this->mu; ++i) {
    s1 += this->weights[i];
    s2 += this->weights[i]*this->weights[i];
}
this->mueff = s1*s1/s2;
for (i=0; i<this->mu; ++i) 
    this->weights[i] /= s1;
if(this->mu < 1 || this->mu > this->lambda || (this->mu==this->lambda && this->weights[0]==this->weights[this->mu-1])){
    printf("readpara_SetWeights(): invalid setting of mu or lambda\n");
104
  exit(0);
kruger's avatar
kruger committed
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
}

/*supplement defaults*/
this->cs = (this->mueff + 2.) / (this->dim + this->mueff + 3.);
this->ccumcov = 4. / (this->dim + 4);
this->mucov = this->mueff;

t1 = 2. / ((this->dim+1.4142)*(this->dim+1.4142));
t2 = (2.*this->mueff-1.) / ((this->dim+2.)*(this->dim+2.)+this->mueff);
t2 = (t2 > 1) ? 1 : t2;
t2 = (1./this->mucov) * t1 + (1.-1./this->mucov) * t2;

this->ccov = t2;

//this->stopMaxIter = ceil((float)(this->stopMaxFunEvals / this->lambda));

this->damps = 1;

this->damps = this->damps * (1 + 2*MAX(0., sqrt((this->mueff-1.)/(this->dim+1.)) - 1)) * 0.3 + this->cs;

this->updateCmode.modulo = 1./this->ccov/(float)(this->dim)/10.;
this->updateCmode.modulo *= this->facupdateCmode;

while ((int) (cloc - clock()) == 0)
; /* TODO: remove this for time critical applications!? */
130
  this->seed = (unsigned int)abs((long)(100*time(NULL)+clock()));
kruger's avatar
kruger committed
131 132 133 134 135
}

void CCmaesCuda::TestMinStdDevs()
/* increases sigma */
{
136 137 138 139 140 141 142 143
  int i; 
  if (this->rgDiffMinChange == NULL)
    return;
  else{
  for (i = 0; i < this->dim; ++i)
    while (this->sigma * sqrt(this->C[i][i]) < this->rgDiffMinChange[i]) 
      this->sigma *= exp(0.05+this->cs/this->damps);
  }
kruger's avatar
kruger committed
144 145 146 147 148

} /* cmaes_TestMinStdDevs() */

int Check_Eigen(int taille,  float **C, float *diag, float **Q) 
{
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
  /* compute Q diag Q^T and Q Q^T to check */
  int i, j, k, res = 0;
  float cc, dd; 

  for (i=0; i < taille; ++i)
    for (j=0; j < taille; ++j) {
    for (cc=0.,dd=0., k=0; k < taille; ++k) {
      cc += diag[k] * Q[i][k] * Q[j][k];
      dd += Q[i][k] * Q[j][k];
    }
    /* check here, is the normalization the right one? */
    if (fabs(cc - C[i>j?i:j][i>j?j:i])/sqrt(C[i][i]*C[j][j]) > 1e-10 && fabs(cc - C[i>j?i:j][i>j?j:i]) > 3e-14) 
    {
      printf("cmaes_t:Eigen(): imprecise result detected \n");
      ++res; 
          }
          if (fabs(dd - (i==j)) > 1e-10) {
            printf("cmaes_t:Eigen(): imprecise result detected (Q not orthog.)\n");
            ++res;
          }
    }
  return res; 
kruger's avatar
kruger committed
171 172 173 174 175
}

float myhypot(float a, float b) 
/* sqrt(a^2 + b^2) numerically stable. */
{
176 177 178 179 180 181 182 183 184
  float r = 0;
  if (fabs(a) > fabs(b)) {
    r = b/a;
    r = fabs(a)*sqrt(1+r*r);
  } else if (b != 0) {
    r = a/b;
    r = fabs(b)*sqrt(1+r*r);
  }
  return r;
kruger's avatar
kruger committed
185 186 187
}

void Householder2(int n, float **V, float *d, float *e) {
188
  int i,j,k; 
kruger's avatar
kruger committed
189

190 191 192
  for (j = 0; j < n; j++) {
    d[j] = V[n-1][j];
  }
kruger's avatar
kruger committed
193

194
  /* Householder reduction to tridiagonal form */
kruger's avatar
kruger committed
195
   
196
  for (i = n-1; i > 0; i--) {
kruger's avatar
kruger committed
197
   
198
    /* Scale to avoid under/overflow */
kruger's avatar
kruger committed
199
   
200 201 202 203 204 205 206 207 208 209 210 211 212
    float scale = 0.0;
    float h = 0.0;
    for (k = 0; k < i; k++) {
      scale = scale + fabs(d[k]);
    }
    if (scale == 0.0) {
      e[i] = d[i-1];
      for (j = 0; j < i; j++) {
        d[j] = V[i-1][j];
        V[i][j] = 0.0;
        V[j][i] = 0.0;
      }
    } else {
kruger's avatar
kruger committed
213
   
214
      /* Generate Householder vector */
kruger's avatar
kruger committed
215

216
      float f, g, hh;
kruger's avatar
kruger committed
217
   
218 219 220 221 222 223 224 225 226 227 228 229 230 231 232
      for (k = 0; k < i; k++) {
        d[k] /= scale;
        h += d[k] * d[k];
      }
      f = d[i-1];
      g = sqrt(h);
      if (f > 0) {
        g = -g;
      }
      e[i] = scale * g;
      h = h - f * g;
      d[i-1] = f - g;
      for (j = 0; j < i; j++) {
        e[j] = 0.0;
      }
kruger's avatar
kruger committed
233
   
234
      /* Apply similarity transformation to remaining columns */
kruger's avatar
kruger committed
235
   
236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266
      for (j = 0; j < i; j++) {
        f = d[j];
        V[j][i] = f;
        g = e[j] + V[j][j] * f;
        for (k = j+1; k <= i-1; k++) {
          g += V[k][j] * d[k];
          e[k] += V[k][j] * f;
        }
        e[j] = g;
      }
      f = 0.0;
      for (j = 0; j < i; j++) {
        e[j] /= h;
        f += e[j] * d[j];
      }
      hh = f / (h + h);
      for (j = 0; j < i; j++) {
        e[j] -= hh * d[j];
      }
      for (j = 0; j < i; j++) {
        f = d[j];
        g = e[j];
        for (k = j; k <= i-1; k++) {
          V[k][j] -= (f * e[k] + g * d[k]);
        }
        d[j] = V[i-1][j];
        V[i][j] = 0.0;
      }
    }
    d[i] = h;
  }
kruger's avatar
kruger committed
267
   
268
  /* Accumulate transformations */
kruger's avatar
kruger committed
269
   
270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298
  for (i = 0; i < n-1; i++) {
    float h; 
    V[n-1][i] = V[i][i];
    V[i][i] = 1.0;
    h = d[i+1];
    if (h != 0.0) {
      for (k = 0; k <= i; k++) {
        d[k] = V[k][i+1] / h;
      }
      for (j = 0; j <= i; j++) {
        float g = 0.0;
        for (k = 0; k <= i; k++) {
          g += V[k][i+1] * V[k][j];
        }
        for (k = 0; k <= i; k++) {
          V[k][j] -= g * d[k];
        }
      }
    }
    for (k = 0; k <= i; k++) {
      V[k][i+1] = 0.0;
    }
  }
  for (j = 0; j < n; j++) {
    d[j] = V[n-1][j];
    V[n-1][j] = 0.0;
  }
  V[n-1][n-1] = 1.0;
  e[0] = 0.0;
kruger's avatar
kruger committed
299 300 301 302

} /* Housholder() */

void QLalgo2 (int n, float *d, float *e, float **V) {
303 304 305 306
  int i, k, l, m;
  float f = 0.0;
  float tst1 = 0.0;
  float eps = 2.22e-16; /* Math.pow(2.0,-52.0);  == 2.22e-16 */
kruger's avatar
kruger committed
307
  
308 309 310 311 312
  /* shift input e */
  for (i = 1; i < n; i++) {
    e[i-1] = e[i];
  }
  e[n-1] = 0.0; /* never changed again */
kruger's avatar
kruger committed
313
   
314
  for (l = 0; l < n; l++) { 
kruger's avatar
kruger committed
315

316
    /* Find small subdiagonal element */
kruger's avatar
kruger committed
317
   
318 319 320 321 322 323 324 325 326 327
    if (tst1 < fabs(d[l]) + fabs(e[l]))
      tst1 = fabs(d[l]) + fabs(e[l]);
    m = l;
    while (m < n) {
      if (fabs(e[m]) <= eps*tst1) {
        /* if (fabs(e[m]) + fabs(d[m]+d[m+1]) == fabs(d[m]+d[m+1])) { */
        break;
      }
      m++;
    }
kruger's avatar
kruger committed
328
   
329 330
    /* If m == l, d[l] is an eigenvalue, */
    /* otherwise, iterate. */
kruger's avatar
kruger committed
331
   
332 333 334 335 336 337 338 339 340
    if (m > l) { 
      int iter = 0;
      do { /* while (fabs(e[l]) > eps*tst1); */
        float dl1, h;
        float g = d[l];
        float p = (d[l+1] - g) / (2.0 * e[l]); 
        float r = myhypot(p, 1.); 

        iter = iter + 1;  /* Could check iteration count here */
kruger's avatar
kruger committed
341
   
342
        /* Compute implicit shift */
kruger's avatar
kruger committed
343
   
344 345 346 347 348 349 350 351 352 353 354
        if (p < 0) {
          r = -r;
        }
        d[l] = e[l] / (p + r);
        d[l+1] = e[l] * (p + r);
        dl1 = d[l+1];
        h = g - d[l];
        for (i = l+2; i < n; i++) {
          d[i] -= h;
        }
        f = f + h;
kruger's avatar
kruger committed
355
   
356
        /* Implicit QL transformation. */
kruger's avatar
kruger committed
357
   
358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377
        p = d[m];
        {
          float c = 1.0;
          float c2 = c;
          float c3 = c;
          float el1 = e[l+1];
          float s = 0.0;
          float s2 = 0.0;
          for (i = m-1; i >= l; i--) {
            c3 = c2;
            c2 = c;
            s2 = s;
            g = c * e[i];
            h = c * p;
            r = myhypot(p, e[i]);
            e[i+1] = s * r;
            s = e[i] / r;
            c = p / r;
            p = c * d[i] - s * g;
            d[i+1] = h + s * (c * g + s * d[i]);
kruger's avatar
kruger committed
378
   
379
            /* Accumulate transformation. */
kruger's avatar
kruger committed
380
   
381 382 383 384 385 386 387 388 389 390
            for (k = 0; k < n; k++) {
              h = V[k][i+1];
              V[k][i+1] = s * V[k][i] + c * h;
              V[k][i] = c * V[k][i] - s * h;
            }
          }
          p = -s * s2 * c3 * el1 * e[l] / dl1;
          e[l] = s * p;
          d[l] = c * p;
        }
kruger's avatar
kruger committed
391
   
392
        /* Check for convergence. */
kruger's avatar
kruger committed
393
   
394 395 396 397 398
      } while (fabs(e[l]) > eps*tst1);
    }
    d[l] = d[l] + f;
    e[l] = 0.0;
  }     
kruger's avatar
kruger committed
399 400
/* Sort eigenvalues and corresponding vectors. */

401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421
  int j; 
  float p;
  for (i = 0; i < n-1; i++) {
    k = i;
    p = d[i];
    for (j = i+1; j < n; j++) {
      if (d[j] < p) {
        k = j;
        p = d[j];
      }
    }
    if (k != i) {
      d[k] = d[i];
      d[i] = p;
      for (j = 0; j < n; j++) {
        p = V[j][i];
        V[j][i] = V[j][k];
        V[j][k] = p;
      }
    }
  }
kruger's avatar
kruger committed
422 423 424 425
} /* QLalgo2 */ 

void Eigen( int taille,  float **C, float *diag, float **Q, float *rgtmp)
{
426 427 428 429 430 431 432 433 434 435 436 437
  int i, j;
  if (rgtmp == NULL) /* was OK in former versions */
    printf("cmaes_t:Eigen(): input parameter float *rgtmp must be non-NULL\n");

  /* copy C to Q */
  if (C != Q) {
    for (i=0; i < taille; ++i)
      for (j = 0; j <= i; ++j)
        Q[i][j] = Q[j][i] = C[i][j];
  }
  Householder2( taille, Q, diag, rgtmp);
  QLalgo2( taille, diag, rgtmp, Q);
kruger's avatar
kruger committed
438 439 440 441 442 443 444 445 446 447 448 449 450 451 452
}

void CCmaesCuda::cmaes_UpdateEigensystem(int flgforce)
{
  int i;

  if(flgforce == 0) {
    if (this->flgEigensysIsUptodate == 1)
      return; 

    /* return on modulo generation number */ 
    if (this->gen < this->genOfEigensysUpdate + this->updateCmode.modulo)
      return;
  }

maitre's avatar
maitre committed
453

kruger's avatar
kruger committed
454 455 456 457 458 459
  Eigen( this->dim, this->C, this->rgD, this->B, this->rgdTmp);

  if (this->flgCheckEigen)
    /* needs O(n^3)! writes, in case, error message in error file */ 
    i = Check_Eigen( this->dim, this->C, this->rgD, this->B);
  
maitre's avatar
maitre committed
460 461 462 463 464 465
  for (i = 0; i < this->dim; ++i){
    //printf("%f ",this->rgD[i]);
    this->rgD[i] = sqrt(fabs(this->rgD[i]));
  }
  //printf("\n");
  /*for(i=0; i<this->dim; ++i){
466 467 468 469
    if(isnan(rgD[i])){
    printf("Ca merde apres sqrt %f \n",rgD[i]);
    exit(1);
  }
maitre's avatar
maitre committed
470
  }*/
kruger's avatar
kruger committed
471 472 473 474 475 476 477 478 479
  
  this->flgEigensysIsUptodate = 1;
  this->genOfEigensysUpdate = this->gen; 
  
  return;
} /* cmaes_UpdateEigensystem() */

/*Function for CMA*/
CCmaesCuda::CCmaesCuda(int lambda, int mu, int problemdim){
480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556
  int i, j;
  float trace;
  /*read param*/
  this->gen = 0;
  this->xstart = NULL; 
  this->typicalX = NULL;
  this->typicalXcase = 0;
  this->rgInitialStds = NULL; 
  this->rgDiffMinChange = NULL;
  this->lambda = lambda;
  this->dim = problemdim;
  this->mu = -1;
  this->mucov = -1;
  this->weights = NULL;
  this->cs = -1;
  this->ccumcov = -1;
  this->damps = -1;
  this->ccov = -1;
  this->updateCmode.modulo = -1;  
  this->updateCmode.maxtime = -1;
  this->updateCmode.flgalways = 0;
  this->facupdateCmode = 1;
  this->flgIniphase = 0;

  this->xstart = (float*)malloc((this->dim)*sizeof(float));
  
  this->typicalXcase = 1;
  for (i=0; i<this->dim; ++i)
    this->xstart[i] = 0.5;

  this->rgInitialStds = (float*)malloc(this->dim*sizeof(float));
  for (i=0; i<this->dim; ++i)
        this->rgInitialStds[i] = 0.3;

  this->Cmaes_init_param(lambda,mu);

  this->seed = this->alea.alea_init((unsigned) this->seed);

  /* initialization  */
  for (i = 0, trace = 0.; i < this->dim; ++i)
    trace += this->rgInitialStds[i]*this->rgInitialStds[i];
  this->sigma = sqrt(trace/this->dim); /* this->sp.mueff/(0.2*this->mueff+sqrt(this->dim)) * sqrt(trace/this->dim); */

  this->chiN = sqrt((float) this->dim) * (1. - 1./(4.*this->dim) + 1./(21.*this->dim*this->dim));
  this->flgEigensysIsUptodate = 1;
  this->flgCheckEigen = 0;
  this->genOfEigensysUpdate = 0;

  this->rgpc = (float*)malloc(this->dim*sizeof(float));
  this->rgps = (float*)malloc(this->dim*sizeof(float));
  if((this->rgdTmp = (float*)malloc((this->dim)*sizeof(float)))==NULL)
    puts("malloc failed");
  this->rgBDz = (float*)malloc((this->dim)*sizeof(float));
  this->rgxmean = (float*)malloc(this->dim*sizeof(float));
  this->rgxold = (float*)malloc(this->dim*sizeof(float));  
  this->rgD = (float*)malloc(this->dim*sizeof(float));
  this->C = (float**)malloc(this->dim*sizeof(float*));
  this->B = (float**)malloc(this->dim*sizeof(float*));

  for (i = 0; i < this->dim; ++i) {
    this->C[i] = (float*)malloc((i+1)*sizeof(float));;
    this->B[i] = (float*)malloc(this->dim*sizeof(float));
  }
  /* Initialize newed space  */

  for (i = 0; i < this->dim; ++i)
    for (j = 0; j < i; ++j){
      this->C[i][j] = this->B[i][j] = this->B[j][i] = 0.;
    }
  for (i = 0; i < this->dim; ++i)
  {
    this->B[i][i] = 1.;
    this->C[i][i] = this->rgD[i] = this->rgInitialStds[i] * sqrt(this->dim / trace);
    this->C[i][i] *= this->C[i][i];
    this->rgpc[i] = this->rgps[i] = 0.;
    this->rgdTmp[i] = 0.0;
  }
kruger's avatar
kruger committed
557 558

  //initialise mean;
559 560 561 562
    for ( i = 0; i < this->dim; ++i){
    this->rgxmean[i] = 0.5 + (this->sigma * this->rgD[i] * this->alea.alea_Gauss());
        this->rgxold[i] = this->rgxmean[i];
    }
kruger's avatar
kruger committed
563 564 565 566
}

void CCmaesCuda::Adapt_C2(int hsig, float **parents)
{
567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585
  int i, j, k;
  if (this->ccov != 0. && this->flgIniphase == 0) {
    
    /* definitions for speeding up inner-most loop */
    float ccovmu = this->ccov * (1-1./this->mucov); 
    float sigmasquare = this->sigma * this->sigma; 

    this->flgEigensysIsUptodate = 0;

    /* update covariance matrix */
    for (i = 0; i < this->dim; ++i)
      for (j = 0; j <=i; ++j) {
        this->C[i][j] = (1 - this->ccov) * this->C[i][j] + this->ccov * (1./this->mucov) * (this->rgpc[i] * this->rgpc[j] + (1-hsig)*this->ccumcov*(2.-this->ccumcov) * this->C[i][j]);
      for (k = 0; k < this->mu; ++k) { /* additional rank mu update */
        this->C[i][j] += ccovmu * this->weights[k] * (parents[k][i] - this->rgxold[i]) * (parents[k][j] - this->rgxold[j]) / sigmasquare;
      }
    }

  }
kruger's avatar
kruger committed
586 587 588 589
}

CCmaesCuda::~CCmaesCuda()
{
590 591 592 593 594 595 596 597 598 599 600 601 602 603
  int i;
  free( this->rgpc);
  free( this->rgps);
  free( this->rgdTmp);
  free( this->rgBDz);
  free( this->rgxmean);
  free( this->rgxold); 
  free( this->rgD);
  for (i = 0; i < this->dim; ++i) {
    free( this->C[i]);
    free( this->B[i]);
  }
  free( this->C);
  free( this->B);
kruger's avatar
kruger committed
604 605 606
} /* cmaes_exit() */

void CCmaesCuda::cmaes_update(float **popparent, float *fitpar){
maitre's avatar
maitre committed
607 608
  //printf("Dans update sigma debut %f\n",this->sigma);
  //if(isnan(this->sigma))
609 610 611 612 613 614 615 616 617
//  exit(1);
      int i, j, iNk, hsig;
  float sum; 
  float psxps;
  if (fitpar[0] == fitpar[(int)this->mu/2]){
    this->sigma *= exp(0.2+this->cs/this->damps);
    printf("Warning: sigma increased due to equal function values\n");
    printf("   Reconsider the formulation of the objective function\n");
  }
maitre's avatar
maitre committed
618 619
  //printf("Dans update sigma milieu %f %f %f\n",this->sigma, this->cs, this->damps);
  //if(isnan(this->sigma))
620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665
//  exit(1);
  for (i = 0; i < this->dim; ++i) {
    this->rgxold[i] = this->rgxmean[i]; 
    this->rgxmean[i] = 0.;
    for (iNk = 0; iNk < this->mu; ++iNk){ 
      this->rgxmean[i] += this->weights[iNk] * popparent[iNk][i];
    }
    this->rgBDz[i] = sqrt(this->mueff)*(this->rgxmean[i] - this->rgxold[i])/this->sigma; 
  }
  /* calculate z := D^(-1) * B^(-1) * rgBDz into rgdTmp */
  for (i = 0; i < this->dim; ++i) {
    sum = 0.;
    for (j = 0; j < this->dim; ++j){
      sum += this->B[j][i] * this->rgBDz[j];
    }
    this->rgdTmp[i] = sum / this->rgD[i];
    // ICI PROBLEME !!!!!!!!!!!!!!!!!!!!!!! ////////////////////////
//    if(isnan(this->rgdTmp[i])){
//      printf("%f %f\n", sum, rgD[i]);
//      exit(0);
//    }
  }
  /* cumulation for sigma (ps) using B*z */
  for (i = 0; i < this->dim; ++i) {
    sum = 0.;
    for (j = 0; j < this->dim; ++j){
      sum += this->B[i][j] * this->rgdTmp[j];
    }
    this->rgps[i] = (1. - this->cs) * this->rgps[i] + sqrt(this->cs * (2. - this->cs)) * sum;
  }
  /* calculate norm(ps)^2 */
  psxps = 0.;
  for (i = 0; i < this->dim; ++i)
    psxps += this->rgps[i] * this->rgps[i];
  /* cumulation for covariance matrix (pc) using B*D*z~N(0,C) */
  hsig = (sqrt(psxps) / sqrt(1. - pow(1.-this->cs, 2*this->gen)) / this->chiN) < (1.4 + 2./(this->dim+1));
  for (i = 0; i < this->dim; ++i) {
    this->rgpc[i] = (1. - this->ccumcov) * this->rgpc[i] + hsig * sqrt(this->ccumcov * (2. - this->ccumcov)) * this->rgBDz[i];
  }
  /* stop initial phase */
  if (this->flgIniphase && this->gen > MIN(1/this->cs, 1+this->dim/this->mucov)) 
  {
    if (psxps / this->damps / (1.-pow((1. - this->cs), this->gen)) < this->dim * 1.05) 
      this->flgIniphase = 0;
  }
  this->Adapt_C2(hsig, popparent);
maitre's avatar
maitre committed
666 667
//  printf("Dans update sigma apres Adapt %f\n",this->sigma);
  //if(isnan(this->sigma))
668 669 670 671 672
//  exit(1);
//  printf("TEST %f %f %f %f\n",psxps, this->chiN, this->cs, this->damps);
  /* update of sigma */
  this->sigma *= exp(((sqrt(psxps)/this->chiN)-1.)*this->cs/this->damps);
  this->gen++;
maitre's avatar
maitre committed
673 674
  //printf("Dans update sigma fin%f %f %f %f %f\n",this->sigma, psxps, this->chiN, this->cs, this->damps);
  //if(isnan(this->sigma))
675
//  exit(1);
kruger's avatar
kruger committed
676 677
}