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

📄 gac.txt

📁 一个用MATLAB编写的优化控制工具箱
💻 TXT
📖 第 1 页 / 共 3 页
字号:
}/**********************************************************************/void Crossover(Chromosome *child1, Chromosome *child2){  /* Crossover crosses the genes of child1 with the genes of child2     If Crossover creates an 'illegal' Chromosome, the 'nearest' 'legal'      Chromosome is created in its place */  int count;  int flag;  int site;  Chromosome temp;  temp = *child1;  /* Do Crossover on a gene by gene basis, instead of selecting sites */  if (cross_type == 0)    {      for(count=0;count<CHROM_LENGTH-1;count++)	if (Flip(0.5))	  {	    child1->gene[count] = child2->gene[count];	    child2->gene[count] = temp.gene[count];	  }    }  else    {      site=Rand_Int(1,CHROM_LENGTH-1);            for(count=0;count<site;count++)	{	  child1->gene[count] = child2->gene[count];	  child2->gene[count] = temp.gene[count];	}    }  /* MAKE CERTAIN CHILD1 IS VALID */  do     {      flag=Check_Chrom(*child1);      if (flag) Create_Chrom(child1,flag);    } while (flag);  /* MAKE CERTAIN CHILD2 IS VALID */  do     {      flag=Check_Chrom(*child2);      if (flag) Create_Chrom(child2,flag);    } while (flag);}/**************************************************************************/void Mutate(Chromosome *child, int site){  /* Mutate changes the gene #site.       This function *may* create an 'illegal' chromosome, so make certain     to check the chromosome after it is mutated. */  Chromosome temp;  int newgene;  temp = *child;    newgene=Rand_Int(Low_Gene[site],High_Gene[site]);  temp.gene[site] = newgene;  *child = temp;}/************************************************************************/int Check_Chrom(Chromosome chrom){  /* Check_Chrom checks chrom to see if it is a valid chromosome.     Check_Chrom returns 0 if chrom has all legal genes.     otherwise this function returns the number of the last bad trait     number it finds */  int count;  int flag;  Traits trait;  flag = 0;  trait = Decode_Chrom(chrom);    for(count=0;count<NUM_TRAITS;count++)    if ((trait.num[count] > High_Trait[count]) || 	(trait.num[count] < Low_Trait[count]))      flag = count+1;return (flag);}/********************************************************************/void Create_Chrom(Chromosome *chrom, int flag){  /* Create_Chrom takes chrom and creates a new & valid trait #flag     If flag = 0, then Create_Chrom creates an all new chromosome */    Chromosome temp_chrom;  Traits trait;  int length;  if (!flag)    do /* CREATE AN ALL NEW CHROMOSOME */      {	for(length=0;length<CHROM_LENGTH;length++)	  temp_chrom.gene[length]=Rand_Int(Low_Gene[length],High_Gene[length]);      } while (Check_Chrom(temp_chrom));   else    { /* MODIFY ONLY THE FAULTY TRAIT */      trait = Decode_Chrom(*chrom); /* Get Faulty Trait From Chromosome*/            /* Randomly create a new, valid, trait */      trait.num[flag-1] = Rand_Double(Low_Trait[flag-1],High_Trait[flag-1]);	      /* Encode Valid Trait Back Into Chromosome */      temp_chrom = Encode_Traits(trait);    }  *chrom = temp_chrom;}/**************************************************************************//*                FITNESS.C                                               *//**************************************************************************/Traits Decode_Chrom(Chromosome chrom){  /* Decode_Chrom takes the chromosome chrom and decodes it into      separate Traits */  int count,place, k,j;  double temp;  Traits trait;  /* Decode Traits: If (negative==1) then  */  /* The first gene of each trait is the sign of the trait.  The trait is     + if this gene is 5-9 and - if 0-4 */  for(k=0;k<NUM_TRAITS;k++)    {      temp = 0.0;      j=1;      for(count=Trait_Start[k]+negative;count<Trait_Start[k+1];count++)	{	  place = Decimal[k] - j;	  temp += chrom.gene[count]*Power(10,place);	  j++;	}      if (negative && (chrom.gene[Trait_Start[k]]<5)) temp = -temp;      trait.num[k]=temp + Offset_Trait[k];    }    return(trait); }/*************************************************************************/Chromosome Encode_Traits(Traits trait){  /* Encode_Traits recomposes the separate Traits into one chromosome */    int count;  int gene;  int k;  double temp;  double pow1;  double pow2;  Chromosome chrom;    /* Encode Traits */  for(k=0;k<NUM_TRAITS;k++)    {      temp = trait.num[k] - Offset_Trait[k];            /* If (negative == 1) account for +/- sign */      if (negative)	{	  if (temp<0.0) chrom.gene[Trait_Start[k]]=0;	  else chrom.gene[Trait_Start[k]]=9;	}      temp = fabs(temp);            pow1 = Power(10,1-Decimal[k]);      pow2 = Power(10,Decimal[k]-1);      /* Encode the rest of the trait */      for(count=Trait_Start[k]+negative;count<Trait_Start[k+1];count++)	{	  gene = (int) (temp * pow1 + ZERO);	  chrom.gene[count]= gene;	  temp = (temp - (double)gene * pow2) * 10.0;	}    }  return(chrom);}/***************************************************************************/double Power(int x, int y){  /* result = x^y      This only works for integer values, and is faster than the function     pow() in math.h      */  int k;  double result;  result = 1.0;  if (y<0)    {      for (k=0;k>y;k--)        result = result / x ;    }  if (y>0)    {      for (k=0;k<y;k++)        result = result * x ;    }  return(result);}/**************************************************************************/void Find_Fitness(Population *pop, Fit_params *params){  /* Find_Fitness determines the fitness value of every member of the */  /* population.  This function calls Decode_Chrom() and Get_Fitness() */  /* to accomplish this task */  int k;  double fitness, sumfitness;  double highfitness, lowfitness;  int high,low;  high=0;  low=0;  /* GET FITNESS OF THE FIRST CHROMOSOME */  pop->member[0].trait = Decode_Chrom(pop->member[0].chrom);  Get_Fitness(&(pop->member[0]), params);  sumfitness  =  pop->member[0].fitness;    /* ESTABLISH FIRST GUESS AT HIGH & LOW FITNESS */  highfitness = pop->member[0].fitness;  lowfitness  = pop->member[0].fitness;    /* GET FITNESS OF THE REMAINDING CHROMOSOMES */  for(k=1;k<pop->size;k++)    {      /* DECODE THE CHROMOSOME INTO INDIVIDUAL TRAITS */      pop->member[k].trait = Decode_Chrom(pop->member[k].chrom);            Get_Fitness(&(pop->member[k]), params);      fitness = pop->member[k].fitness;      sumfitness += fitness;            /* FIND BEST MEMBER */      if (fitness>highfitness)	{	  highfitness=fitness;	  high = k;	}            /* FIND WORST MEMBER */      if (fitness<lowfitness)	{	  lowfitness=fitness;	  low = k;	}    }  pop->bestmember = high;  pop->worstmember = low;  pop->sumfitness = sumfitness;}/**************************************************************************//*           RANDOM.C                                                     *//**************************************************************************/void Randomize(void){  /* Randomize() initializes the pseudo-random number generator      by seeding the generator with some function of time */  int stime;  long int ltime;    ltime = time(NULL);  stime = (unsigned) ltime/2;  srand(stime);}/**************************************************************************/double Rand_Num(void){  /* Rand_Num() returns a pseudo-random number between 0.0 and 1.0      by calling the function rand() from <stdlib.h>     If the GA is generating unusual numbers (all 0's, or hanging up),     you may need to modify the MaxRandom value according to the rand()      function on your computer system.     */    int MaxRandom =2147483647; /* = 2^31 - 1 */  /* int MaxRandom = 32767;*/  /*  = 2^15 -1 */     double number;  number = (double)rand() / (double)MaxRandom;  return (number);}/**************************************************************************/int Flip(double chance){  /* This function returns TRUE (1) with probability (chance) */  /* and returns FALSE (0) with probability (1-chance). */  /* 0.0 <= chance <= 1.0 */    return (chance > Rand_Num() ? 1 : 0);}/*************************************************************************/int Rand_Int(int high, int low){  /* Rand_Int returns an integer between high and low */  /* with equal probability for all numbers (including high & low) */    int temp;    /* MAKE CERTAIN LOW <= HIGH */  if (low>high)    {      temp = low;      low  = high;      high = temp;    }  return( (int)(Rand_Num()*(high-low+1)) + low ) ;}/*************************************************************************/double Rand_Double(double high, double low){  /* Rand_Double returns a double  between high and low */  /* with equal probability for all numbers */  double temp;  /* MAKE CERTAIN LOW <= HIGH */  if (low>high)    {      temp = low;      low  = high;      high = temp;    }  return( (double)(Rand_Num()*(high-low)) + low ) ;}/**************************** END ****************************************/

⌨️ 快捷键说明

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