⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 nsgaorig.c

📁 原始非支配多目标遗传算法
💻 C
📖 第 1 页 / 共 5 页
字号:
static double oldrand[55];   /* Array of 55 random numbers */ static int jrand;                 /* current random number */ static double rndx2;    /* used with random normal deviate */ static int rndcalcflag; /* used with random normal deviate */ int small(float number){  if (fabs(number)<=EPSILON)     return TRUE;  else     return FALSE;}initrandomnormaldeviate()      /* initialization routine for randomnormaldeviate */ {   rndcalcflag = 1; } double noise(mu ,sigma)      /* normal noise with specified mean & std dev: mu & sigma */      double mu, sigma; {   double randomnormaldeviate();     return((randomnormaldeviate()*sigma) + mu); } double randomnormaldeviate()      /* random normal deviate after ACM algorithm 267 / Box-Muller Method */ {   double sqrt(), log(), sin(), cos();   float randomperc();    double t, rndx1;     if(rndcalcflag)     {       rndx1 = sqrt(- 2.0*log((double) randomperc()));       t = 6.2831853072 * (double) randomperc();       rndx2 = sin(t);       rndcalcflag = 0;       return(rndx1 * cos(t));     }   else     {       rndcalcflag = 1;       return(rndx2);     } } advance_random()      /* Create next batch of 55 random numbers */ {   int j1;   double new_random;     for(j1 = 0; j1 < 24; j1++)     {       new_random = oldrand[j1] - oldrand[j1+31];       if(new_random < 0.0) new_random = new_random + 1.0;       oldrand[j1] = new_random;     }   for(j1 = 24; j1 < 55; j1++)     {       new_random = oldrand [j1] - oldrand [j1-24];       if(new_random < 0.0) new_random = new_random + 1.0;       oldrand[j1] = new_random;     } } int flip(prob)      /* Flip a biased coin - true if heads */      float prob; {   float randomperc();     if(randomperc() <= prob)     return(1);   else     return(0); } randomize()      /* Get seed number for random and start it up */ {   int j1;     for(j1=0; j1<=54; j1++) oldrand[j1] = 0.0;   jrand=0;     warmup_random(seed);   initrandomnormaldeviate(); } float randomperc()      /* Fetch a single random number between 0.0 and 1.0 -  */     /* Subtractive Method . See Knuth, D. (1969), v. 2 for */      /* details.Name changed from random() to avoid library */     /* conflicts on some machines                          */ {   jrand++;   if(jrand >= 55)     {       jrand = 1;       advance_random();     }   return((float) oldrand[jrand]); } int rnd(low, high)      /* Pick a random integer between low and high */      int low,high; {   int i;   float randomperc();     if(low >= high)     i = low;   else     {       i = (randomperc() * (high - low + 1)) + low;       if(i > high) i = high;     }   return(i); } float rndreal(lo ,hi)      /* real random number between specified limits */      float lo, hi; {   return((randomperc() * (hi - lo)) + lo); } warmup_random(random_seed)      /* Get random off and running */      float random_seed; {   int j1, ii;   double new_random, prev_random;     oldrand[54] = random_seed;   new_random = 0.000000001;   prev_random = random_seed;   for(j1 = 1 ; j1 <= 54; j1++)     {       ii = (21*j1)%54;       oldrand[ii] = new_random;       new_random = prev_random-new_random;       if(new_random<0.0) new_random = new_random + 1.0;       prev_random = oldrand[ii];     }     advance_random();   advance_random();   advance_random();     jrand = 0; } /*----------------------------------------------------------*//* Files for tournament selection :                         *//* Source : sga.c (c) E.Goldberg                            *//*----------------------------------------------------------*/select_memory() {  /* allocates auxiliary memory for stochastic remainder selection*/    unsigned nbytes;  int j;    choices = NULL;  fraction = NULL;  nbytes = pop_size*sizeof(int);  if((choices = (int *) malloc(nbytes)) == NULL)    nomemory(stderr,"choices");  nbytes = pop_size*sizeof(float);  if((fraction = (float *) malloc(nbytes)) == NULL)    nomemory(stderr,"fraction");}select_free(){  /* frees auxiliary memory for stochastic remainder selection */  choices = NULL;  fraction = NULL;  free(choices);  free(fraction);}preselect()     /* preselection for stochastic remainder method */{  int j, jassign, k;  float expected;  int flip();    if(dum_avg == 0)    {      for(j = 0; j < pop_size; j++) choices[j] = j;    }  else    {      j = 0;      k = 0;      /* Assign whole numbers */      do        {	  expected = (float)((oldpop[j].dumfitness)/dum_avg);	  jassign = (int)expected;	  /* note that expected is automatically truncated */	  fraction[j] = expected - (float)jassign;	  while(jassign > 0)	    {	      jassign--;	      choices[k] = j;	      k++;	    }	  j++;        }      while(j < pop_size);            j = 0;      /* Assign fractional parts */      while(k < pop_size)	{	  if(j >= pop_size) j = 0;	  if(fraction[j] > 0.0)	    {	      /* A winner if true */	      if(flip(fraction[j]))		{		  choices[k] = j;		  fraction[j] = fraction[j] - 1.0;		  k++;		}	    }	  j++;	}    }  nremain = pop_size - 1;}int select()     /* selection using remainder method */{  int jpick, slect;  int rnd();    jpick = rnd(0, nremain);  slect = choices[jpick];  choices[jpick] = choices[nremain];  nremain--;  return(slect);}reset1()         /* Name changed from reset because of clash with lib. function - RBA */     /* Shuffles the tourneylist at random */ {   int i, rand1, rand2, temp_site;     for(i=0; i<pop_size; i++) tourneylist[i] = i;     for(i=0; i < pop_size; i++)     {       rand1= rnd(0,pop_size-1);      rand2=  rnd(0,pop_size-1);      temp_site = tourneylist[rand1];       tourneylist[rand1]=tourneylist[rand2];       tourneylist[rand2]=temp_site;     } }   /******************* APPLICATION ORIENTED ROUTINES ***************//**** Change these routines for your particular application ******/input_app_parameters()     /* Input your application dependent parameters here and put the	output in global variables */{}app_computation()      /* this routine should contain any application-dependent computations */      /* that should be performed before each GA cycle.	called by generate_new_pop    */ { } app_free()      /* application dependent free() calls, called by free_all() */ { } app_initialize()      /* application dependent initialization routine called by intialize() */ { } app_initreport()      /* Application-dependent initial report called by initreport() */ { } app_report()      /* Application-dependent report, called by report() */ { } app_statistics()      /* Application-dependent statistics calculations called by statistics() */ { } app_closure()     /* Application-dependent work which should be done before closure of	the main program. called by main() */{}/*====================================================================  OBJECTIVE FUNCTION : Change it for different applications    Given a sample application where the two objective functions are :  f1(x) = x*x;  f2(x) = (x-2)*(x-2)  and the aim is to minimize both of them.    NORMALIZE your fitness functions before use. (Divide them by their  maximum possible values)    The code is designed for minimizing the fitness functions.  To maximize a function, either use  o 1/(1+f(x)) or  o -f(x)  instead of f(x).  ===================================================================*/objective(person)     INDIVIDUAL *person;{  float a, a1, a2;  float penalty, g[10];  int i, nc;     if (person == NULL) error_ptr_null("person in objective()");    /* First problem */#ifdef f1  a=person->x[0];  person->fitness[0] = square(a);   person->fitness[1] = square((2.0-a));  nc = 0;#endif    /* Second problem */#ifdef f2  a=person->x[0];  person->fitness[0]=(a<=1.0) ? -a : ((a<=3) ? -2+a : ((a<=4) ? 4-a : -4+a));  person->fitness[1]=(a-5)*(a-5);   nc = 0;#endif    /* Third problem */#ifdef f3  a1 = person->x[0];  a2 = person->x[1];  person->fitness[0]=(a1-2)*(a1-2)+(a2-1)*(a2-1)+2;  person->fitness[1]=9*a1-(a2-1)*(a2-1);    nc = 2;  g[0] = -1.0*(a1*a1 + a2*a2 - 225.0);  g[1] = -1.0*(a1 - 3.0*a2 + 10.0);#endif	#ifdef book  a1 = person->x[0];  a2 = person->x[1];  person->fitness[0] = a1;  person->fitness[1] = (1.0+a2)/a1;  nc = 0;#endif  penalty = 0.0;  for (i=0; i<nc; i++)    if (g[i] < 0.0) penalty += PENALTY_COEFF * g[i] * g[i];    for (i=0; i<num_obj; i++)    person->fitness[i] = person->fitness[i] + minmax[i] * penalty;}/********************** END  OF  FILE **************************/

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -