Example #1
0
/**
 * The optimization loop.
 */
int main(int, char**) {
  CMAES<double> evo;
  double *arFunvals, *const*pop, *xfinal;

  // Initialize everything
  const int dim = 22;
  double xstart[dim];
  for(int i=0; i<dim; i++) xstart[i] = 0.5;
  double stddev[dim];
  for(int i=0; i<dim; i++) stddev[i] = 0.3;
  Parameters<double> parameters;
  // TODO Adjust parameters here
  parameters.init(dim, xstart, stddev);
  arFunvals = evo.init(parameters);

  std::cout << evo.sayHello() << std::endl;

  // Iterate until stop criterion holds
  while(!evo.testForTermination())
  {
    // Generate lambda new search points, sample population
    pop = evo.samplePopulation(); // Do not change content of pop

    /* Here you may resample each solution point pop[i] until it
       becomes feasible, e.g. for box constraints (variable
       boundaries). function is_feasible(...) needs to be
       user-defined.  
       Assumptions: the feasible domain is convex, the optimum is
       not on (or very close to) the domain boundary, initialX is
       feasible and initialStandardDeviations are sufficiently small
       to prevent quasi-infinite looping.
    */
    /* for (i = 0; i < evo.get(CMAES<double>::PopSize); ++i)
         while (!is_feasible(pop[i]))
           evo.reSampleSingle(i);
    */

    // evaluate the new search points using fitfun from above
    for (int i = 0; i < evo.get(CMAES<double>::Lambda); ++i)
      arFunvals[i] = fitfun(pop[i], (int) evo.get(CMAES<double>::Dimension));

    // update the search distribution used for sampleDistribution()
    evo.updateDistribution(arFunvals);
  }
  std::cout << "Stop:" << std::endl << evo.getStopMessage();
  evo.writeToFile(CMAES<double>::WKResume, "resumeevo1.dat"); // write resumable state of CMA-ES

  // get best estimator for the optimum, xmean
  xfinal = evo.getNew(CMAES<double>::XMean); // "XBestEver" might be used as well

  // do something with final solution and finally release memory
  delete[] xfinal;

  return 0;
}
Example #2
0
double F(double *x, int *pn)
{
	int p = *pn;
	double sum = 0.0;
	double n[p];

	Noise(x, n, p, dr48_buffer[torc_i_worker_id()]);

	inc_nfc();
      	sum = fitfun(x, p, NULL, NULL);
	sum += n[0];

//	usleep(10);

#if VERBOSE
	printf("F(%lf,%lf,%lf,%lf) = %lf\n", x[0], x[1], x[2], x[3], sum);
#endif
	return sum;

}
Example #3
0
int main(int argc, char *argv[])
{
	int rank, size;
	int i;
	double lval = 0, gval;
	double TP[PROBDIM];

	/* print argv numbers */
	for (i = 0; i < argc; i++) {
		printf("simcode: arg %d argv %s\n", i, argv[i]); fflush(0);
	}

	/* read input parameters (argv) into TP */
	//for (i = 1; i < argc; i++) {
	//	TP[i-1] = atof(argv[i]);
	//}

	FILE *fp = fopen("params.dat", "r");
	fscanf(fp, "%lf", &TP[0]);
	fscanf(fp, "%lf", &TP[1]);
	fclose(fp);

	/* run the "simulation" */
	fitfun(TP, PROBDIM, &gval);

	/* write the results in the "fitness" file */
	char fname[256];
	FILE *fd;
	strcpy(fname,"fitness");
	fd = fopen(fname, "w");
	if (fd == NULL) printf("error with fopen\n"); fflush(0);

	//fprintf(fd, "RESULT %.16lf\n", gval);
	fprintf(fd, "%.16lf\n", gval);
	fclose(fd);	

	return 0;
}
/* the optimization loop */
int main(int argc, char **argv) {
  cmaes_t evo; /* an CMA-ES type struct or "object" */
  double *arFunvals,  *xfinal, *const*pop;
  int i,j;
  int numberDipoles;
  int id;  //Rank
  int p;  //Number processors
  double elapsed_time;//Time from beginning.
  double bestValue;
  int lambda;
  int maxLambda;
  int * sendCnts; //For MPI_Alltoallv for arFunVals
  int * sdispls;  //For MPI_Alltoallv for arFunVals
  int * recvCnts; //For MPI_Alltoallv for arFunVals
  int * rdispls; //For MPI_Alltoallv for arFunVals
  int * sendCntsPop; //For MPI_Alltoallv for pop
  int * sdisplsPop; //For MPI_Alltoallv for pop
  int * recvCntsPop; //For MPI_Alltoallv for pop
  int * rdisplsPop; //For MPI_Alltoallv for pop
  int canTerminate;
  int canTerminateBuffer;

  //Start MPI
  MPI_Init(&argc, &argv);
  MPI_Barrier(MPI_COMM_WORLD);
  elapsed_time = -MPI_Wtime(); //Set initial time.

  MPI_Comm_rank(MPI_COMM_WORLD, &id); //Set id
  MPI_Comm_size(MPI_COMM_WORLD, &p); //set p



  for (i=0;i<32;i++)
  {
    observations[i]/=1000.0;
  }

  //Set number of dipoles, either first argument or default value of 2.
  numberDipoles=2; 
  if (argc>=2)
  {
    numberDipoles=atoi(argv[1]);
  }

  //Set lambda based on entry, default of 40
  maxLambda=40;
  if (argc>=3)
  {
    maxLambda=atoi(argv[2]);
  }

  if (id==0)
  {
    printf("Dipoles:%d MaxLambda:%d\n",numberDipoles,maxLambda);
  }

  //Allocate lambda pieces to each processor, based on the size of maxLambda and the number of processors.
  lambda = BLOCK_SIZE(id,p,maxLambda);

  printf("Id:%d Lambda:%d\n",id,lambda);

  //Setup send and receive buffers for function evaluations and populations that resulted in those evaluation.
  sendCnts = malloc(p*sizeof(int));
  sdispls = malloc(p*sizeof(int));
  recvCnts = malloc(p*sizeof(int));
  rdispls = malloc(p*sizeof(int));
  sendCntsPop = malloc(p*sizeof(int));
  sdisplsPop = malloc(p*sizeof(int));
  recvCntsPop = malloc(p*sizeof(int));
  rdisplsPop = malloc(p*sizeof(int));

  for (i=0;i<p;i++)
  {

    sendCnts[i]=lambda;//Same for all others
    sdispls[i] = BLOCK_LOW(id,p,maxLambda);//Same for all others
    recvCnts[i] = BLOCK_SIZE(i,p,maxLambda);//Depends on which we receive from.
    rdispls[i] = BLOCK_LOW(i,p,maxLambda);

    sendCntsPop[i]=lambda*((numberDipoles*6+2));//Same for all others
    sdisplsPop[i] = BLOCK_LOW(id,p,maxLambda)*(numberDipoles*6+2);//Same for all others
    recvCntsPop[i] = BLOCK_SIZE(i,p,maxLambda)*(numberDipoles*6+2);//Depends on which we receive from.
    rdisplsPop[i] = BLOCK_LOW(i,p,maxLambda)*(numberDipoles*6+2);

  }

  for (i=0;i<p;i++)
  {

    printf("Id: %d recvCnts[%d]=%d\n",id,i,recvCnts[i]);
    printf("Id: %d rdispls[%d]=%d\n",id,i,rdispls[i]);

    printf("Id: %d recvCntsPop[%d]=%d\n",id,i,recvCntsPop[i]);
    printf("Id: %d rdisplsPop[%d]=%d\n",id,i,rdisplsPop[i]);

  }
  





  /* Initialize everything into the struct evo, 0 means default */
  //arFunvals = cmaes_init(&evo, 0, NULL, NULL, 0, 0, "initials.par"); 

//  printf("0\n");
  
  //The maxLambda parameter has been added so all of them will have enough space to store the results
  arFunvals = reinit(&evo, maxLambda, numberDipoles);

  //outputCMAES_t(evo,1);

//  printf("1\n");

  resetSignals(&evo, numberDipoles);  /* write header and initial values */

  //Reset the seed value based on processor (so they don't all come out the same!
  evo.sp.seed=evo.sp.seed*(id+1)/p;
  printf("proc: %d seed: %d\n",id,evo.sp.seed);


  //outputCMAES_t(evo,0);

//  printf("2\n");

//  printf("%s\n", cmaes_SayHello(&evo));
//  i=40;

//  for (i=32;i<40;i*=2)
//  { 

//    arFunvals = reinit(&evo, i);
    //outputCMAES_t(evo);


  evo.sp.lambda=lambda;
  canTerminate = (0==1);
  /* Iterate until stop criterion holds */
  while(!canTerminate)
    { 
      /* generate lambda new search points, sample population */
      pop = cmaes_SamplePopulation(&evo); /* do not change content of pop */

      /* Here you may resample each solution point pop[i] until it
	 becomes feasible, e.g. for box constraints (variable
	 boundaries). function is_feasible(...) needs to be
	 user-defined.  
	 Assumptions: the feasible domain is convex, the optimum is
	 not on (or very close to) the domain boundary, initialX is
	 feasible and initialStandardDeviations are sufficiently small
	 to prevent quasi-infinite looping.
      */
      /*for (i = 0; i < lambda; ++i) 
      {
          cmaes_ReSampleSingle(&evo, i); 
      }*/
      for (i = 0; i < lambda; ++i) 
      {
	   while (!is_feasible(evo.rgrgx[i],(int) cmaes_Get(&evo, "dim"))) 
	   {
             cmaes_ReSampleSingle(&evo, i); 
           }
      }

      for (i=0;i<lambda;i++)
      {
         for(j=0;j<(6*numberDipoles)+2;j++)
         {
	   evo.rgrgx[BLOCK_LOW(id,p,maxLambda)+i][j]=evo.rgrgx[i][j];
         }
      }
 
      /* evaluate the new search points using fitfun from above */ 
      for (i = BLOCK_LOW(id,p,maxLambda); i <= BLOCK_HIGH(id,p,maxLambda); ++i) {
	arFunvals[i] = fitfun(evo.rgrgx[i], (int) cmaes_Get(&evo, "dim"));
        //printf("ID:%d, arFunvals[%d]=%lf\n",id,i,arFunvals[i]);
      }

      



      //Now communicate the arFunvals around
      MPI_Alltoallv(arFunvals,sendCnts,sdispls,MPI_DOUBLE,arFunvals,recvCnts,rdispls,MPI_DOUBLE,MPI_COMM_WORLD);


      //Now communicate the populations being looked at around
      MPI_Alltoallv(&evo.rgrgx[0][0],sendCntsPop,sdisplsPop,MPI_DOUBLE,&evo.rgrgx[0][0],recvCntsPop,rdisplsPop,MPI_DOUBLE,MPI_COMM_WORLD);


      /* update the search distribution used for cmaes_SampleDistribution() */
      cmaes_UpdateDistribution(&evo, arFunvals);  


      //Test for any that can terminate.
      canTerminate = cmaes_TestForTermination(&evo);
      if (canTerminate)
      {
	printf("id:%d can terminate for reason:%s\n",id,cmaes_TestForTermination(&evo));
      }
      MPI_Allreduce(&canTerminate,&canTerminateBuffer,1,MPI_INT,MPI_MAX,MPI_COMM_WORLD);//Get the max, if any are >0, then someone has terminated.
      canTerminate = canTerminateBuffer;//Reset so the loop will exit.

      /* read instructions for printing output or changing termination conditions */ 
//      cmaes_ReadSignals(&evo, "signals.par");   
//      fflush(stdout); /* useful in MinGW */
    }
//  printf("Stop:\n%s\n",  cmaes_TestForTermination(&evo)); /* print termination reason */

//  cmaes_WriteToFile(&evo, "all", "allcmaes.dat");         /* write final results */


  elapsed_time += MPI_Wtime();



  /* get best estimator for the optimum, xmean */
  xfinal = cmaes_GetNew(&evo, "xmean"); /* "xbestever" might be used as well */
  bestValue = fitfun(xfinal, (int) cmaes_Get(&evo, "dim"));
  printf("Proccesor:%d has last mean of:%lf elapsedTime:%lf\n",id,bestValue,elapsed_time);
  for (i=0;i<6*numberDipoles;i++)
  {
    printf("(%d:%d:%lf)\n",id,i,xfinal[i]);
  }

//  cmaes_exit(&evo); /* release memory */ 
  /* do something with final solution and finally release memory */
  free(xfinal); 
  free(sendCnts);
  free(sdispls);
  free(recvCnts);
  free(rdispls);
  free(sendCntsPop);
  free(sdisplsPop);
  free(recvCntsPop);
  free(rdisplsPop);

  MPI_Finalize();

//}

  return 0;
}
Example #5
0
/* the optimization loop */
int main(int argn, char **args) {
  cmaes_t evo; /* an CMA-ES type struct or "object" */
  boundary_transformation_t boundaries;
  double *arFunvals, *x_in_bounds, *const*pop;
  double lowerBounds[] = {1.0, DBL_MAX / -1e2}; /* last number is recycled for all remaining coordinates */
  double upperBounds[] = {3, 2e22};
  int nb_bounds = 1; /* numbers used from lower and upperBounds */
  unsigned long dimension;
  int i; 

  /* initialize boundaries, be sure that initialSigma is smaller than upper minus lower bound */
  boundary_transformation_init(&boundaries, lowerBounds, upperBounds, nb_bounds);
  /* Initialize everything into the struct evo, 0 means default */
  arFunvals = cmaes_init(&evo, 0, NULL, NULL, 0, 0, "cmaes_initials.par");
  dimension = (unsigned long)cmaes_Get(&evo, "dimension");

  printf("%s\n", cmaes_SayHello(&evo));

  x_in_bounds = cmaes_NewDouble(dimension); /* calloc another vector */
  cmaes_ReadSignals(&evo, "cmaes_signals.par");  /* write header and initial values */

  /* Iterate until stop criterion holds */
  while(!cmaes_TestForTermination(&evo))
    { 
      /* generate lambda new search points, sample population */
      pop = cmaes_SamplePopulation(&evo); /* do not change content of pop */

      /* transform into bounds and evaluate the new search points */
      for (i = 0; i < cmaes_Get(&evo, "lambda"); ++i) {
        boundary_transformation(&boundaries, pop[i], x_in_bounds, dimension);
        /* this loop can be omitted if is_feasible is invariably true */
        while(!is_feasible(x_in_bounds, dimension)) { /* is_feasible needs to be user-defined, in case, and can change/repair x */
            cmaes_ReSampleSingle(&evo, i); 
            boundary_transformation(&boundaries, pop[i], x_in_bounds, dimension);
        }
        arFunvals[i] = fitfun(x_in_bounds, dimension); /* evaluate */
      }

      /* update the search distribution used for cmaes_SampleDistribution() */
      cmaes_UpdateDistribution(&evo, arFunvals);  /* assumes that pop[i] has not been modified */

      /* read instructions for printing output or changing termination conditions */ 
      cmaes_ReadSignals(&evo, "cmaes_signals.par");
      fflush(stdout); /* useful in MinGW */
    }
  printf("Stop:\n%s\n",  cmaes_TestForTermination(&evo)); /* print termination reason */
  cmaes_WriteToFile(&evo, "all", "allcmaes.dat");         /* write final results */

  /* get best estimator for the optimum, xmean */
  boundary_transformation(&boundaries,
		  (double const *) cmaes_GetPtr(&evo, "xmean"), /* "xbestever" might be used as well */
		  x_in_bounds, dimension);

  /* do something with final solution x_in_bounds */

  /* ... */

  /* and finally release memory */
  cmaes_exit(&evo); /* release memory */
  boundary_transformation_exit(&boundaries); /* release memory */
  free(x_in_bounds);

  return 0;
}