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

📄 ga.c

📁 This a GA implementation using binary and real coded variables. Mixed variables can be used. Constra
💻 C
📖 第 1 页 / 共 4 页
字号:
    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);
}

double 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((double) oldrand[jrand]);
}

int rnd(low, high)
/* Pick a random integer between low and high */
int low,high;
{
    int i;
    double randomperc();

    if(low >= high)
        i = low;
    else
    {
        i = (randomperc() * (high - low + 1)) + low;
        if(i > high) i = high;
    }
    return(i);
}

double rndreal(lo ,hi)
/* real random number between specified limits */
double lo, hi;
{
    return((randomperc() * (hi - lo)) + lo);
}

warmup_random(random_seed)
/* Get random off and running */
double 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()
{
  unsigned nbytes;

  if(tourneysize > pop_size)
    {
      printf("FATAL: Tournament size (%d) > pop_size (%d)\n",
              tourneysize,pop_size);
      exit(-1);
    } ;
}


preselect_tour()
{
    reset1();
    tourneypos = 0;
}


int tour_select()
{
    int pick, winner, i;

    /* If remaining members not enough for a tournament, then reset list */
start_select :
    if((pop_size - tourneypos) < tourneysize)
    {
        reset1();
        tourneypos = 0;
    }

    /* Select tourneysize structures at random and conduct a tournament */
    winner=tourneylist[tourneypos];
/* Added by RBA */
    if( winner < 0 || winner > pop_size-1) {
                                             printf("\n Warning !! ERROR1");
                                             printf(" tourpos = %d",tourneypos);
                                             printf(" winner = %d",winner);
                                             preselect_tour();
                                             goto start_select; }
    for(i=1; i<tourneysize; i++)
    {
        pick=tourneylist[i+tourneypos];
/* Added by RBA */
        if (pick < 0 || pick > pop_size-1) { preselect_tour();
                                             printf("\n Warning !! ERROR2");
                                             goto start_select; }
	// case 1:
	if (oldpop[winner].penalty > oldpop[pick].penalty) winner = pick;
	else if ((oldpop[winner].penalty <= 0.0) && (oldpop[pick].penalty <= 0.0))
	  {
	      if(MINM * oldpop[pick].obj < MINM * oldpop[winner].obj) winner=pick;
	  }
    }
    
    /* Update tourneypos */
    tourneypos += tourneysize;
    return(winner);
}

double distanc(one,two)
     int one,two;
{
  int k;
  double sum;
  
  sum = 0.0;
  for (k=0; k<nvar_bin; k++) 
    sum += square((oldpop[one].xbin[k]-oldpop[two].xbin[k])/(xbin_upper[k]-xbin_lower[k]));
  for (k=0; k<nvar_real; k++) 
    sum += square((oldpop[one].xreal[k]-oldpop[two].xreal[k])/(xreal_upper[k]-xreal_lower[k]));

  return (sqrt(sum/(nvar_bin + nvar_real)));
};

int tour_select_constr() 
{ 
  int pick, winner, i, minus=0, rand_pick, rand_indv, flag, indv; 
  
  /* If remaining members not enough for a tournament, then reset list */ 
  start_select : 
    if((pop_size - tourneypos) < tourneysize) 
      { 
        reset1(); 
        tourneypos = 0; 
      } 
  
  /* Select tourneysize structures at random and conduct a tournament */ 
  winner = tourneylist[tourneypos]; 
  /* Added by RBA */ 
  if( winner < 0 || winner > pop_size-1) 
    {
      printf("\n Warning !! ERROR1"); 
      printf(" tourpos = %d",tourneypos); 
      printf(" winner = %d",winner); 
      preselect_tour(); 
      goto start_select; 
    } 
  for(i=1; i<tourneysize; i++) 
    { 
      pick = tourneylist[i+tourneypos];

      if((oldpop[winner].penalty>0.0) && (oldpop[pick].penalty<=0.0))
	winner = pick;
      else if((oldpop[winner].penalty>0.0)&&(oldpop[pick].penalty>0.0))
	{	
	  if(oldpop[pick].penalty < oldpop[winner].penalty) 
	    winner=pick;
	}
      else if((oldpop[winner].penalty<=0.0)&&(oldpop[pick].penalty<=0.0))
	{
	  if(distanc(winner,pick) < sigma_share) 
	    {
	      if (MINM * oldpop[pick].obj < MINM * oldpop[winner].obj) 
		winner=pick;
	    }
	  else
	    {
	      minus = -1;
	      for (indv = flag = 0; indv<critical_size && flag==0; indv++) 
		{
		  rand_indv = rnd(0,pop_size-1);
		  rand_pick = tourneylist[rand_indv];
		  if(oldpop[rand_pick].penalty <= 0.0) 
		    {
		      if(distanc(winner,rand_pick) < sigma_share) 
			{
			  flag = 1;
			  if (MINM * oldpop[rand_pick].obj < MINM * oldpop[winner].obj) 
			    winner=rand_pick;
			}
		    }
		}
	    }
	}
      if (pick < 0 || pick > pop_size-1) 
	{ 
	  preselect_tour(); 
	  printf("\n Warning !! ERROR2"); 
	  goto start_select; 
	} 
    } 
    /* Update tourneypos */ 
  tourneypos += tourneysize + minus; 
  return(winner); 
}

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  ( Supposed to be minimized) :
Change it for different applications
====================================================================*/
void objective(indv)
INDIVIDUAL *indv;
{
  int i;
  double term1,term2, term3, pi, your_func;
  double g[MAXCONSTR], gsum, x[2*MAXVECSIZE];
  
  // if (indv == NULL) error_ptr_null("x in objective()");
   
   for (i=0; i < nvar_bin; i++)
     x[i] = indv->xbin[i];
   for (i=nvar_bin; i < nvar_bin+nvar_real; i++)
     x[i] = indv->xreal[i-nvar_bin];

#ifdef prob1
   MINM  = 1; // for maximization use -1
   term1 = (x[0]*x[0]+x[1]-11.0)*(x[0]*x[0]+x[1]-11.0);
   term2 = (x[0]+x[1]*x[1]- 7.0)*(x[0]+x[1]*x[1]- 7.0);
   term3 = term1+term2;
   your_func = term3;

   nc = 1;
   // add normalized constraints here 
   g[0] = (square(x[0]-5.0) + square(x[1]))/26.0 - 1.0;
#endif

#ifdef can
   MINM = 1;
   pi = 4.0 * atan(1.0);
   term3 = pi * x[0] * x[0]/2.0 + pi * x[0] * x[1];
   your_func = term3;

   nc = 1;
   g[0] = (pi * x[0] * x[0] * x[1]/4.0 - 400.0)/400.0;
#endif
   
#ifdef prob2
   MINM = 1;
   term1 = 5*(x[0] + x[1] + x[2] + x[3]); 
   for (i = 0; i<4; i++) 
     term1 += -5*x[i]*x[i]; 
   for (i = 4; i<=12; i++) term1 += -x[i]; 
   your_func = term1;
   
   nc = 9; 
   g[0] = (10 - 2*(x[0] + x[1]) - x[9] - x[10] )/10; 
   g[1] = (10 - 2*(x[0] + x[2]) - x[9] - x[11] )/10; 
   g[2] = (10 - 2*(x[1] + x[2]) - x[10] - x[11] )/10; 
   g[3] = x[0]  - x[9]/8.0; 
   g[4] = x[1]  - x[10]/8.0; 
   g[5] = x[2]  - x[11]/8.0; 
   g[6] = x[3] + 0.5*x[4] - 0.5*x[9]; 
   g[7] = x[5] + 0.5*x[6] - 0.5*x[10]; 
   g[8] = x[7] + 0.5*x[8] - 0.5*x[11]; 
#endif

#ifdef yours // define `yours' in the beginning of the code 
   MINM = 1; // use -1 for maximization
   // Put your function here
   nc = 0; 
   // Put your constraints here
#endif

   indv->obj = your_func;
   for (i=0, gsum=0.0; i<nc; i++) 
     {
       indv->cons[i] = g[i];
       if (g[i] < 0.0) gsum += -1.0 * g[i];
     }
     indv->penalty = gsum;
}

/********************** END  OF  FILE **************************/

⌨️ 快捷键说明

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