示例#1
0
文件: movpeaks.c 项目: andrejadd/ABC
/* initialize all variables at the beginning of the program */
void init_peaks ()
{
	movrandseed = 1;
	recent_change = 1; /* indicates that a change has just ocurred */
	offline_performance = 0.0;
	offline_error = 0.0;
	avg_error=0;    /* average error so far */ 
	current_error=0;/* error of the currently best individual */
	evals = 0;         /* number of evaluations so far */



  int i,j;
  double dummy;
  
  shift = (double *) calloc(geno_size, sizeof(double));
  coordinates = (double *) calloc(geno_size, sizeof(double));
  covered_peaks = (int *) calloc(number_of_peaks, sizeof(int));
  peak = (double **) calloc(number_of_peaks, sizeof(double*));
  prev_movement = (double **) calloc(number_of_peaks, sizeof(double*));
  for (i=0; i< number_of_peaks; i++){
    peak[i]= (double *) calloc(geno_size+2, sizeof(double));
    prev_movement[i] = (double *) calloc(geno_size, sizeof(double));
  }
  for (i=0; i< number_of_peaks; i++)
    for (j=0; j< geno_size; j++){
      peak[i][j] = 100.0*movrand();
      prev_movement[i][j] = movrand()-0.5;
    }
  if (standardheight <= 0.0)
    for (i=0; i< number_of_peaks; i++)
      peak[i][geno_size+1]= (maxheight-minheight)*movrand()+minheight;
  else
    for (i=0; i< number_of_peaks; i++)
      peak[i][geno_size+1]= standardheight;
  if (standardwidth <= 0.0)
    for (i=0; i< number_of_peaks; i++)
      peak[i][geno_size]= (maxwidth-minwidth)*movrand()+minwidth;
  else
    for (i=0; i< number_of_peaks; i++)
      peak[i][geno_size]= standardwidth;
  if(calculate_average_error){
    global_max = -100000.0;
    for (i=0;i<number_of_peaks; i++){
      for (j=0; j<geno_size; j++)
        coordinates[j]=peak[i][j];
      dummy = dummy_eval(coordinates);
      if (dummy>global_max)
        global_max = dummy;
    }
  }
}
/* initialize all variables at the beginning of the program */
void init_peaks ()
{
  int i,j;
  double dummy;
  
  shift = (double *) calloc(geno_size, sizeof(double));
  coordinates = (double *) calloc(geno_size, sizeof(double));
  covered_peaks = (int *) calloc(number_of_peaks, sizeof(int));
  peak = (double **) calloc(number_of_peaks, sizeof(double*));
  prev_movement = (double **) calloc(number_of_peaks, sizeof(double*));


  for (i=0; i< number_of_peaks; i++){
    peak[i]= (double *) calloc(geno_size+2, sizeof(double));
    prev_movement[i] = (double *) calloc(geno_size, sizeof(double));
  }

  for (i=0; i< number_of_peaks; i++)
    for (j=0; j< geno_size; j++){
// old      peak[i][j] = 100.0*movrand();
	  peak[i][j] = (maxcoordinate - mincoordinate) * movrand() + mincoordinate;
      prev_movement[i][j] = movrand()-0.5;
    }

  if (standardheight <= 0.0)
    for (i=0; i< number_of_peaks; i++)
      peak[i][geno_size+1]= (maxheight-minheight)*movrand()+minheight;
  else
    for (i=0; i< number_of_peaks; i++)
      peak[i][geno_size+1]= standardheight;


  if (standardwidth <= 0.0)
    for (i=0; i< number_of_peaks; i++)
      peak[i][geno_size]= (maxwidth-minwidth)*movrand()+minwidth;
  else
    for (i=0; i< number_of_peaks; i++)
      peak[i][geno_size]= standardwidth;


  if(calculate_average_error){
    global_max = -100000.0;
    for (i=0;i<number_of_peaks; i++){
      for (j=0; j<geno_size; j++)
        coordinates[j]=peak[i][j];
      dummy = dummy_eval(coordinates);
	  if (dummy>global_max)
        global_max = dummy;
		}
	}
}
/* this function produces normally distributed random values */ 
double movnrand() 
{ static int backup=0;
  static double x2;
  double x1, w;
 
  if (backup){
    backup = 0;
    return (x2);
  }  else {
    do
    { x1 = 2.0 * movrand() - 1.0;
      x2 = 2.0 * movrand() - 1.0;
      w = x1*x1 + x2*x2;
    } while ( w>=1.0 );
    w = sqrt( (-2.0*log(w) ) / w );
    x2 = w*x2;
    backup = 1;
    return (x1*w);
  }
}
/* whenever this function is called, the peaks are changed */ 
void change_peaks() 
{
  int i,j;
  double sum, sum2, offset, dummy;

  for(i=0; i<number_of_peaks; i++)
    {
      /* shift peak locations */
    sum = 0.0;
    for (j=0; j<geno_size; j++){
      shift[j]=movrand()-0.5;
      sum += shift[j]*shift[j];
      }
    if(sum>0.0)                 
      sum = vlength/sqrt(sum);
    else                           /* only in case of rounding errors */
      sum = 0.0;
    sum2=0.0;
    for (j=0; j<geno_size; j++){
      shift[j]=sum*(1.0-lambda)*shift[j]+lambda*prev_movement[i][j];
      sum2 += shift[j]*shift[j];
    }
    if(sum2>0.0)                 
      sum2 = vlength/sqrt(sum2);
    else                           /* only in case of rounding errors */
      sum2 = 0.0;


    for(j=0; j<geno_size; j++){
      shift[j]*=sum2;
      prev_movement[i][j]= shift[j];
      if (((peak[i][j]+prev_movement[i][j]) < mincoordinate) || ((peak[i][j]+prev_movement[i][j]) > mincoordinate)){
				dummy = (peak[i][j]+prev_movement[i][j]- mincoordinate)/(maxcoordinate-mincoordinate);
				if (((int)dummy % 2) == 0) {
					dummy = fabs(dummy-floor(dummy));
				}else {
					dummy=1-fabs(dummy-floor(dummy));
				}
				peak[i][j] = mincoordinate + (maxcoordinate - mincoordinate)* dummy;
			}
      else
        peak[i][j] += prev_movement[i][j];    
		}

    /* change peak width */
    j = geno_size;
    offset = movnrand()*width_severity;
    if ((peak[i][j]+offset) < minwidth)
      peak[i][j] = 2.0*minwidth-peak[i][j]-offset;
    else if ((peak[i][j]+offset) > maxwidth)
      peak[i][j]= 2.0*maxwidth-peak[i][j]-offset;
    else
      peak[i][j] += offset;
   /* change peak height */
    j++;
    offset = height_severity*movnrand();
    if ((peak[i][j]+offset) < minheight)
      peak[i][j] = 2.0*minheight-peak[i][j]-offset;
    else if ((peak[i][j]+offset) > maxheight)
      peak[i][j]= 2.0*maxheight-peak[i][j]-offset;
    else
      peak[i][j] += offset;    
  }
  if(calculate_average_error){
    global_max = -100000.0;
    for (i=0;i<number_of_peaks; i++){
      for (j=0; j<geno_size; j++)
        coordinates[j]=peak[i][j];
      dummy = dummy_eval(coordinates);
      if (dummy>global_max){
        global_max = dummy;
        maximum_peak = i;
      }
    }
  }
  recent_change = 1;
}