void resetSignals(cmaes_t *evo, int numDipoles)
{
  int k;
  //Reset typical x -- all other mandatory settings are overwritten in reinit()

  evo->sp.typicalX = new_double(evo->sp.N);

  for (k=0;k<numDipoles;k++)
  {
    evo->sp.typicalX[k*6+0] = 6.000000;
    evo->sp.typicalX[k*6+1] = 8.0;
    evo->sp.typicalX[k*6+2] = 15.0;
    evo->sp.typicalX[k*6+3] = 90.0;
    evo->sp.typicalX[k*6+4] = 90.0;
    evo->sp.typicalX[k*6+5] = 0.5;
  }


  //Reset optional settings:

  evo->sp.stopMaxFunEvals=1e299;
  evo->sp.facmaxeval=1.0;
  evo->sp.stopMaxIter=1e299;
  evo->sp.stopTolFun=1e-12;
  evo->sp.stopTolFunHist=1e-13;
  evo->sp.stopTolX=1e-11;
  evo->sp.stopTolUpXFactor=1e3;

  evo->sp.updateCmode.maxtime=1;
  
}
예제 #2
0
int main(int argc,char**argv){
  int N,M,i,j; double tau,h,eps,*v,*hat_v,*delta_v,*pts[3];
  if (argc<4) {printf("%s 1/tau 1/h eps\n",argv[0]); return 0;}
  N = atoi(argv[1]); M = atoi(argv[2]);  eps = atof(argv[3]);
  h = 1./M;  tau = 1./N;
  new_double(3,N,pts); v = pts[0]; hat_v = pts[1];  delta_v = pts[2];
    v0(N,v);
//    pl(N,v);
  for(j=0;j<M;j++){
    v_bounds(N,j/M,hat_v);
    for (i=2;i<N-2;i++) hat_v[i] = v[i];
    layer(eps,N,tau,h,v,hat_v);
    
    for (i=0;i<N;i++) delta_v[i] = fabs(analitic( ((double)j)/M ,((double)i)/N) - hat_v[i] );
    
    printf("layer %d с ошибкой %e\n",j,norm_max(N,delta_v));
    for (i=2;i<N-2;i++) v[i] = hat_v[i];
  }
  clean_all(3,pts);
  return 0;
}
예제 #3
0
//Пусть эта штука будет рекурсивно себя вызывать и возвращать число итераций
int layer(double eps,int n,double tau,double h,double *v,double *hat_v){
  double err,gamma,*a,*c,*d,*b,*w,*pts[5]; int i;
  new_double(5,n,pts);
  a = pts[0];  c = pts[1];  d = pts[2];  b = pts[3]; w = pts[4];
  _G(n,b,tau,h,v,hat_v);
  err = norm_max(n,b);
  if (err<eps) printf("Ошибка %e\neps %e\n",norm_max(n,b),eps);
  if (norm_max(n,b) < eps) {
    clean_all(5,pts); return 0;
  }
  for(i=n-6,a[n-5] = 1/tau;i>=0;i--) {
    a[i] = 1/tau;
    c[i] = hat_v[i+3]/(2*h); d[i] = -hat_v[i+2]/(2*h);
  }
  thomas(n-4,w+2,a,c,d,b); for (gamma=1.;gamma>AESH_MIN;gamma/=2.){
     if (gamma<10*AESH_MIN) exit(-1);
    for (i=2;i<n-2;i++) hat_v[i] += gamma * w[i];
    _G(n,b,tau,h,v,hat_v); printf("Ошибка %e\n",norm_max(n,b)); if (norm_max(n,b)<err) break;
    for (i=2;i<n-2;i++) hat_v[i] -= gamma * w[i];
  }
  clean_all(5,pts);
  return 1 + layer(eps,n,tau,h,v,hat_v);
}
예제 #4
0
int thomas(int n,double *dest,double*a,double*c,double*d,double *b){
  double *l,*v,*u,*pts[3]; int i;
  if (n<2) return n-2;
 // for (i=0;i<n-5;i++) printf("%e %e %e %e\n",a[i],c[i],d[i],b[i]);
  new_double(3,n,pts);
  l = pts[0]; v = pts[1], u = pts[2];
  // Богачёв первый этап прогонки
//  printf("d0 = %e\n",d[0]);
  v[0]=d[0]; if (fabs(v[0])<AESH_MIN) {printf("d[0] zero delimeter\n"); clean_all(3,pts); exit(-1);}
  l[0] = a[0]/v[0];if (fabs(l[0])<AESH_MIN) {printf("l[0] zero delimeter %e/%e = %e\n",a[0],v[0],l[0]); clean_all(3,pts);exit(-1);}
  u[0] = c[0]/l[0];
  for (i=1;i<n-1;i++) {
    v[i] = d[i]; if (fabs(v[i])<AESH_MIN) {printf("v[%d] zero delimeter\n",i); exit(-1);}
    l[i] = (a[i] - u[i-1])/v[i];if (fabs(l[i])<AESH_MIN) {printf("l[%d] zero delimeter\n",i); clean_all(3,pts); exit(-1);}
    u[i] = c[i]/l[i];
  }
  l[n-1]=a[n-1]-u[n-2];
  v[n-1]=1;
  // Богачёв замечание 4
  for(i=1,dest[0]=b[0]/l[0]; i<n; i++) dest[i] = (b[i]-dest[i-1])/l[i];
  for(i=n-2;i>=0; i--) dest[i] = (dest[i] - u[i]*dest[i+1])/v[i];
  clean_all(3,pts);
  return 0; 
}
double * reinit(cmaes_t * evo, int lambda, int numDipoles)
{
  int i, j, k, N;
  double dtest, trace;
  double *rgrgxdata;

/** Unknown if these are needed, most seem to be related to reading the init file (which we are removing) **/

  evo->sp.rgsformat = (char **) new_void(55, sizeof(char *));
  evo->sp.rgpadr = (void **) new_void(55, sizeof(void *)); 
  evo->sp.rgskeyar = (char **) new_void(11, sizeof(char *));
  evo->sp.rgp2adr = (double ***) new_void(11, sizeof(double **));
  evo->sp.weigkey = (char *)new_void(7, sizeof(char)); 

  /* All scalars:  */
  i = 0;
  evo->sp.rgsformat[i] = " N %d";        evo->sp.rgpadr[i++] = (void *) &evo->sp.N; 
  evo->sp.rgsformat[i] = " seed %d";    evo->sp.rgpadr[i++] = (void *) &evo->sp.seed;
  evo->sp.rgsformat[i] = " stopMaxFunEvals %lg"; evo->sp.rgpadr[i++] = (void *) &evo->sp.stopMaxFunEvals;
  evo->sp.rgsformat[i] = " stopMaxIter %lg"; evo->sp.rgpadr[i++] = (void *) &evo->sp.stopMaxIter;
  evo->sp.rgsformat[i] = " stopFitness %lg"; evo->sp.rgpadr[i++]=(void *) &evo->sp.stStopFitness.val;
  evo->sp.rgsformat[i] = " stopTolFun %lg"; evo->sp.rgpadr[i++]=(void *) &evo->sp.stopTolFun;
  evo->sp.rgsformat[i] = " stopTolFunHist %lg"; evo->sp.rgpadr[i++]=(void *) &evo->sp.stopTolFunHist;
  evo->sp.rgsformat[i] = " stopTolX %lg"; evo->sp.rgpadr[i++]=(void *) &evo->sp.stopTolX;
  evo->sp.rgsformat[i] = " stopTolUpXFactor %lg"; evo->sp.rgpadr[i++]=(void *) &evo->sp.stopTolUpXFactor;
  evo->sp.rgsformat[i] = " lambda %d";      evo->sp.rgpadr[i++] = (void *) &evo->sp.lambda;
  evo->sp.rgsformat[i] = " mu %d";          evo->sp.rgpadr[i++] = (void *) &evo->sp.mu;
  evo->sp.rgsformat[i] = " weights %5s";    evo->sp.rgpadr[i++] = (void *) evo->sp.weigkey;
  evo->sp.rgsformat[i] = " fac*cs %lg";evo->sp.rgpadr[i++] = (void *) &evo->sp.cs;
  evo->sp.rgsformat[i] = " fac*damps %lg";   evo->sp.rgpadr[i++] = (void *) &evo->sp.damps;
  evo->sp.rgsformat[i] = " ccumcov %lg";    evo->sp.rgpadr[i++] = (void *) &evo->sp.ccumcov;
  evo->sp.rgsformat[i] = " mucov %lg";     evo->sp.rgpadr[i++] = (void *) &evo->sp.mucov;
  evo->sp.rgsformat[i] = " fac*ccov %lg";  evo->sp.rgpadr[i++]=(void *) &evo->sp.ccov;
  evo->sp.rgsformat[i] = " diagonalCovarianceMatrix %lg"; evo->sp.rgpadr[i++]=(void *) &evo->sp.diagonalCov;
  evo->sp.rgsformat[i] = " updatecov %lg"; evo->sp.rgpadr[i++]=(void *) &evo->sp.updateCmode.modulo;
  evo->sp.rgsformat[i] = " maxTimeFractionForEigendecompostion %lg"; evo->sp.rgpadr[i++]=(void *) &evo->sp.updateCmode.maxtime;
  evo->sp.rgsformat[i] = " resume %59s";    evo->sp.rgpadr[i++] = (void *) evo->sp.resumefile;
  evo->sp.rgsformat[i] = " fac*maxFunEvals %lg";   evo->sp.rgpadr[i++] = (void *) &evo->sp.facmaxeval;
  evo->sp.rgsformat[i] = " fac*updatecov %lg"; evo->sp.rgpadr[i++]=(void *) &evo->sp.facupdateCmode;
  evo->sp.n1para = i; 
  evo->sp.n1outpara = i-2; /* disregard last parameters in WriteToFile() */

  /* arrays */
  i = 0;
  evo->sp.rgskeyar[i]  = " typicalX %d";   evo->sp.rgp2adr[i++] = &evo->sp.typicalX;
  evo->sp.rgskeyar[i]  = " initialX %d";   evo->sp.rgp2adr[i++] = &evo->sp.xstart;
  evo->sp.rgskeyar[i]  = " initialStandardDeviations %d"; evo->sp.rgp2adr[i++] = &evo->sp.rgInitialStds;
  evo->sp.rgskeyar[i]  = " diffMinChange %d"; evo->sp.rgp2adr[i++] = &evo->sp.rgDiffMinChange;
  evo->sp.n2para = i;  

/** End Unknown section **/

  //First reset the strategy parameters (sp)
  evo->sp.N=6*numDipoles;
  evo->sp.seed=0;
  evo->sp.xstart=NULL;
  evo->sp.typicalXcase=0;
  evo->sp.rgInitialStds = NULL;
  evo->sp.rgDiffMinChange = NULL; 
  evo->sp.stopMaxFunEvals = -1;
  evo->sp.stopMaxIter = -1;
  evo->sp.facmaxeval = 1; 
  evo->sp.stStopFitness.flg = 1;
  evo->sp.stStopFitness.val = 1e-5;
  evo->sp.stopTolFun = 1e-12; 
  evo->sp.stopTolFunHist = 1e-13; 
  evo->sp.stopTolX = 0; /* 1e-11*insigma would also be reasonable */ 
  evo->sp.stopTolUpXFactor = 1e3; 

  evo->sp.lambda=lambda;
  evo->sp.mu=-1;
  evo->sp.mucov = -1;
  evo->sp.weights = NULL;
  evo->sp.weigkey=malloc(5*sizeof(char));
  strcpy(evo->sp.weigkey, "log\0");

  evo->sp.cs = -1;
  evo->sp.ccumcov = -1;
  evo->sp.damps = -1;
  evo->sp.ccov = -1;

  evo->sp.diagonalCov = 0; /* default is 0, but this might change in future, see below */

  evo->sp.updateCmode.modulo = -1;  
  evo->sp.updateCmode.maxtime = -1;
  evo->sp.updateCmode.flgalways = 0;
  evo->sp.facupdateCmode = 1;

  strcpy(evo->sp.resumefile, "_no_");

//printf("reinit:0\n");

  readpara_SupplementDefaults(&(evo->sp));

//printf("reinit:1\n");
  //Now reset the state variables
  evo->version = "3.11.00.beta";
  evo->sp.seed = random_init( &evo->rand, (long unsigned int) evo->sp.seed);

  N = evo->sp.N; /* for convenience */


  //printf("reinit:2\n");

  evo->sp.rgInitialStds = new_double(N);
  evo->sp.xstart = new_double(N);
  for (k=0;k<numDipoles;k++)
  {
    evo->sp.rgInitialStds[k*6+0] = 3.000000;
    evo->sp.rgInitialStds[k*6+1] = 4.0;
    evo->sp.rgInitialStds[k*6+2] = 5.0;
    evo->sp.rgInitialStds[k*6+3] = 90.0;
    evo->sp.rgInitialStds[k*6+4] = 90.0;
    evo->sp.rgInitialStds[k*6+5] = 3.75;


    evo->sp.xstart[k*6+0] = 6.000000;
    evo->sp.xstart[k*6+1] = 8.0;
    evo->sp.xstart[k*6+2] = 15.0;
    evo->sp.xstart[k*6+3] = 90.0;
    evo->sp.xstart[k*6+4] = 90.0;
    evo->sp.xstart[k*6+5] = 0.5;
  }
  /* initialization  */
  for (i = 0, trace = 0.; i < N; ++i)
    trace += evo->sp.rgInitialStds[i]*evo->sp.rgInitialStds[i];
  evo->sigma = sqrt(trace/N); /* evo->sp.mueff/(0.2*evo->sp.mueff+sqrt(N)) * sqrt(trace/N); */

//printf("reinit:3\n");

  evo->chiN = sqrt((double) N) * (1. - 1./(4.*N) + 1./(21.*N*N));
  evo->flgEigensysIsUptodate = 1;
  evo->flgCheckEigen = 0; 
  evo->genOfEigensysUpdate = 0;
  timings_init(&evo->eigenTimings);
  evo->flgIniphase = 0; /* do not use iniphase, hsig does the job now */
  evo->flgresumedone = 0;
  evo->flgStop = 0;


  for (dtest = 1.; dtest && dtest < 1.1 * dtest; dtest *= 2.) 
    if (dtest == dtest + 1.)
      break;
  evo->dMaxSignifKond = dtest / 1000.; /* not sure whether this is really safe, 100 does not work well enough */

  evo->gen = 0;
  evo->countevals = 0;
  evo->state = 0;
  evo->dLastMinEWgroesserNull = 1.0;
  evo->printtime = evo->writetime = evo->firstwritetime = evo->firstprinttime = 0; 

  evo->rgpc = new_double(N);
  evo->rgps = new_double(N);
  evo->rgdTmp = new_double(N+1);
  evo->rgBDz = new_double(N);
  evo->rgxmean = new_double(N+2); evo->rgxmean[0] = N; ++evo->rgxmean;
  evo->rgxold = new_double(N+2); evo->rgxold[0] = N; ++evo->rgxold; 
  evo->rgxbestever = new_double(N+3); evo->rgxbestever[0] = N; ++evo->rgxbestever; 
  evo->rgout = new_double(N+2); evo->rgout[0] = N; ++evo->rgout;
  evo->rgD = new_double(N);
  evo->C = (double**)new_void(N, sizeof(double*));
  evo->B = (double**)new_void(N, sizeof(double*));
  evo->publicFitness = new_double(evo->sp.lambda); 
  evo->rgFuncValue = new_double(evo->sp.lambda+1); 
  evo->rgFuncValue[0]=evo->sp.lambda; ++evo->rgFuncValue;
  evo->arFuncValueHist = new_double(10+(int)ceil(3.*10.*N/evo->sp.lambda)+1);
  evo->arFuncValueHist[0] = (double)(10+(int)ceil(3.*10.*N/evo->sp.lambda));
  evo->arFuncValueHist++; 

//printf("reinit:4\n");

  for (i = 0; i < N; ++i) {
      evo->C[i] = new_double(i+1);
      evo->B[i] = new_double(N);
    }
  evo->index = (int *) new_void(evo->sp.lambda, sizeof(int));
  for (i = 0; i < evo->sp.lambda; ++i) 
    evo->index[i] = i; /* should not be necessary */
  evo->rgrgx = (double **)new_void(evo->sp.lambda, sizeof(double*));
  
  rgrgxdata = new_double(evo->sp.lambda*(N+2));
  for (i = 0; i < evo->sp.lambda; ++i) {
    evo->rgrgx[i] = &rgrgxdata[i*(N+2)];
    evo->rgrgx[i][0] = N; 
    evo->rgrgx[i]++;
  }
//  printf("Arggg:%d\n",evo->sp.lambda*(N+2));

//printf("reinit:5\n");
  /* Initialize newed space  */

  for (i = 0; i < N; ++i)
    for (j = 0; j < i; ++j)
       evo->C[i][j] = evo->B[i][j] = evo->B[j][i] = 0.;
        
  for (i = 0; i < N; ++i)
    {
      evo->B[i][i] = 1.;
      evo->C[i][i] = evo->rgD[i] = evo->sp.rgInitialStds[i] * sqrt(N / trace);
      evo->C[i][i] *= evo->C[i][i];
      evo->rgpc[i] = evo->rgps[i] = 0.;
    }

//printf("reinit:6\n");

  evo->minEW = rgdouMin(evo->rgD, N); evo->minEW = evo->minEW * evo->minEW;
  evo->maxEW = rgdouMax(evo->rgD, N); evo->maxEW = evo->maxEW * evo->maxEW; 

  evo->maxdiagC=evo->C[0][0]; for(i=1;i<N;++i) if(evo->maxdiagC<evo->C[i][i]) evo->maxdiagC=evo->C[i][i];
  evo->mindiagC=evo->C[0][0]; for(i=1;i<N;++i) if(evo->mindiagC>evo->C[i][i]) evo->mindiagC=evo->C[i][i];

  /* set xmean */
  for (i = 0; i < N; ++i)
  {
    evo->rgxmean[i] = evo->rgxold[i] = evo->sp.xstart[i]; 

  }



//printf("reinit:7\n");
  return evo->publicFitness;
}