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

📄 gac.txt

📁 一个用MATLAB编写的优化控制工具箱
💻 TXT
📖 第 1 页 / 共 3 页
字号:
void Randomize(void);double Rand_Double(double high, double low);/**************************************************************************/void main(void){  Population pop;  Fit_params params;  FILE *File;  int pop_size;  /* Initialize Random Number Generator */  Randomize();  /* Remove previous output file */  if (output)    {      File = fopen("stats.out","w");      fclose(File);    }  File = fopen("best.out","w");  fclose(File);  /**** CREATE FIRST GENERATION ****/  pop_size = MAX_POPULATION;  pop = First_Generation(pop_size);  params.example  = 0.0;  /* Initialize fitness function parameters */  Find_Fitness(&pop, &params);  Statistics(&pop);    /***** CREATE NEW GENERATIONS ****/  while (pop.count<max_generation)    {      params.example = 0.0;      Next_Generation(&pop, pop_size);      Find_Fitness(&pop, &params);      Statistics(&pop);    }  printf("\nGA complete.\n");  Save_Generation(&pop);}/************************************************************************/void Statistics(Population *pop){  /* Statistics prints to file all the pertinent information about */  /* the current generation */    FILE *File;  int i;  int x;  int j;  if (output)    {      File = fopen("stats.out","a");      fprintf(File,"\nGeneration %d",pop->count);      fprintf(File,"\nMember  Chromosome       Traits      ");      fprintf(File,"    Fitness  Parents");             for(i=0;i<pop->size;i++)	{	  fprintf(File,"\n%3d    ",i);	  	  for (x=0;x<CHROM_LENGTH;x++)	    {  	      for (j=1;j<NUM_TRAITS;j++)		if (x==Trait_Start[j]) fprintf(File," ");	      fprintf(File,"%d",pop->member[i].chrom.gene[x]);	    }	  fprintf(File," ");	  for (x=0;x<NUM_TRAITS;x++)	    fprintf(File,"  %8.5lf",pop->member[i].trait.num[x]);	  fprintf(File,"  %9.5lf ",pop->member[i].fitness);	  fprintf(File,"%3d %3d",pop->member[i].parents[0],		  pop->member[i].parents[1]);	}      fprintf(File,"\nBest: %5.4lf ",	      pop->member[pop->bestmember].fitness);      fprintf(File,"(#%d)",pop->bestmember);            fprintf(File,"\nAverage: %5.4lf",	      pop->sumfitness/(double)pop->size);      fprintf(File,"\nWorst: %5.4lf",	      pop->member[pop->worstmember].fitness);      fprintf(File,"\nCrossovers: %d Mutations: %d",pop->crossovers, 	      pop->mutations);      fprintf(File,"\n");      fclose(File);    }    /* WRITE BEST MEMBER TO FILE BEST.OUT */  File = fopen ("best.out","a");  if (pop->count==1)    fprintf(File,"\nGeneration :  Fitness   :    Chromosome    :   Traits\n");    fprintf(File," %3d  ",pop->count);  fprintf(File,"%15.10lf  ",pop->member[pop->bestmember].fitness);  for (x=0;x<CHROM_LENGTH;x++)    {      for (j=1;j<NUM_TRAITS;j++)	if (x==Trait_Start[j]) fprintf(File," ");       fprintf(File,"%d",pop->member[pop->bestmember]	      .chrom.gene[x]);     }  fprintf(File,"  ");  for (x=0;x<NUM_TRAITS;x++)    fprintf(File," %8.5lf",pop->member[pop->bestmember]	    .trait.num[x]);    fprintf(File,"\n");  fclose(File);}/****************************************************************************/void Save_Generation(Population *pop){  /* Save all the data in the pop structure to a file called      "generation.data" */    FILE *File;  int x,i;    File = fopen("generation.data","w");    fprintf(File,"%20.15lf ",pop->sumfitness);  fprintf(File,"%d ",pop->bestmember);  fprintf(File,"%d ",pop->worstmember);  fprintf(File,"%d ",pop->size);  fprintf(File,"%d ",pop->count);  fprintf(File,"%d ",pop->crossovers);  fprintf(File,"%d\n",pop->mutations);    for (i=0;i<pop->size;i++)    {        for (x=0;x<CHROM_LENGTH;x++)	fprintf(File,"%d ",pop->member[i].chrom.gene[x]);      fprintf(File,"\n");            for (x=0;x<NUM_TRAITS;x++)	fprintf(File,"%15.10lf ",pop->member[i].trait.num[x]);            fprintf(File,"\n%15.10lf ",pop->member[i].fitness);      fprintf(File,"%d %d\n\n",pop->member[i].parents[0],	      pop->member[i].parents[1]);    }  fclose(File);}/***********************************************************************//*           REPRODUCTION.C                                            *//***********************************************************************/Population First_Generation(int pop_size){  /* First_Generation() creates a population of size pop_size. */  /* This is done by randomly creating individual chromosomes with */  /* Create_Chrom() */  Population pop;  Chromosome chrom;  int k;  /* Make Certain pop_size is a nice number */  if (pop_size<MIN_POPULATION) pop_size = MIN_POPULATION;  if (pop_size>MAX_POPULATION) pop_size = MAX_POPULATION;    for(k=0;k<pop_size;k++)    {      /* CREATE A VALID CHROMOSOME */      Create_Chrom(&chrom, 0);        pop.member[k].chrom = chrom;      pop.member[k].parents[0] = 0;      pop.member[k].parents[1] = 0;    }    pop.size = pop_size;  pop.count = 1;  pop.crossovers = 0;  pop.mutations = 0;  return(pop);}/**************************************************************************/void Next_Generation(Population *pop, int pop_size){  /* This function creates the next generation by selecting parents, creating     the offspring of those parents, and incrementing the generation counter.  */  Survivors parents;  Population nextpop;  /* Make certain pop_size is a valid number */  if (pop_size<MIN_POPULATION) pop_size = MIN_POPULATION;  if (pop_size>MAX_POPULATION) pop_size = MAX_POPULATION;  parents = Select_Parents(pop,pop_size);    nextpop = Make_Offspring(parents);  nextpop.count = pop->count + 1;  *pop = nextpop;}  /*************************************************************************/Survivors Select_Parents(Population *pop, int pop_size){  /* Select_Parents() takes members of pop, selects the ones who     will survive and reproduce, and places them into parents */    /* This function uses the 'Roulette-Wheel' selection method as described by     David E. Goldberg */  Survivors parents;  int k;  int member_count;  int count;  double pointer;  double sum;  /* DETERMINE NUMBER OF PARENTS TO CREATE*/  if (elitism)    count = pop_size - 1;  else    count = ((pop_size+1)/2)*2;  for(k=0;k<count;k++)    {        pointer=pop->sumfitness*Rand_Num();            /* DETERMINE WHICH MEMBER IS SELECTED BY ROULETTE WHEEL */      member_count = -1;      sum = 0.0;            while (sum<pointer)	{	  member_count++;	  	  /* ERROR DEBUGGING */	  if (member_count>pop->size)	    {	      printf("\n\nERROR: Negative Fitness Evaluation! ");	      member_count=0;	    }	  	  sum += pop->member[member_count].fitness; 	}      /* NOW ASSIGN SELECTED MEMBER TO BECOME A PARENT */      parents.chrom[k]=pop->member[member_count].chrom;      parents.number[k]=member_count;    }  /* AFTER ALL PARENTS HAVE BEEN SELECTED, */  /* ADD BEST INDIVIDUAL IF elitism IS ON */  if (elitism)    {      parents.chrom[pop_size-1] = 	pop->member[pop->bestmember].chrom;      parents.number[pop_size-1] = pop->bestmember;    }  parents.size=pop_size;    return(parents);}/***********************************************************************/Population Make_Offspring(Survivors parents){  /* Make_Offspring takes the parents chromosomes and creates a new     generation of individuals using Reproduce().     This function assumes parent[n] mates with parent[n+1], n=0,2,4, etc.     and there are an even number of parents (parents.size=even) */  int c,j;  Chromosome child1, child2;  Population next;  int crossovers = 0;  int mutations = 0;  int flag;  for(c=0;c<parents.size-elitism;c+=2)    {            Reproduce(parents.chrom[c],parents.chrom[c+1]		,&child1,&child2, &crossovers, &mutations);      next.member[c].chrom = child1;      next.member[c].parents[0]=parents.number[c];      next.member[c].parents[1]=parents.number[c+1];            next.member[c+1].chrom = child2;      next.member[c+1].parents[0] = parents.number[c];      next.member[c+1].parents[1] = parents.number[c+1];     }   if (elitism)    {      next.member[parents.size-1].chrom = parents.chrom[parents.size-1];      next.member[parents.size-1].parents[0]=parents.number[parents.size-1];      next.member[parents.size-1].parents[1]=parents.number[parents.size-1];    }  next.size=parents.size;  next.crossovers = crossovers;  next.mutations = mutations;    return(next);}  /**********************************************************************/void Reproduce(Chromosome parent1, Chromosome parent2,	       Chromosome *child1, Chromosome *child2,	       int *crossovers, int *mutations){  /* Reproduce is called by Make_Offspring() and creates child1 and     child2 from parent1 and parent2.  This function calls Crossover()     and Mutation() to provide variety in the next generation */  int site;  int flag;  *child1=parent1;  *child2=parent2;  /* CROSSOVER CHILD1 AND CHILD2 */    if (Flip(cross_prob))    {      Crossover(child1,child2);      *crossovers += 1;    }  /* MUTATE CHILD #1 */  for(site=0;site<CHROM_LENGTH;site++)    if (Flip(mutat_prob))      {	Mutate(child1,site);	*mutations += 1;      }    /* CHECK AND CORRECT ANY  'ILLEGAL' MUTATIONS */  do     {      flag=Check_Chrom(*child1);      if (flag) Create_Chrom(child1,flag);    } while (flag);  /* MUTATE CHILD #2 */  for(site=0;site<CHROM_LENGTH;site++)    if (Flip(mutat_prob))      {	Mutate(child2,site);	*mutations += 1;      }    /* CHECK AND CORRECT ANY  'ILLEGAL' MUTATIONS */  do     {      flag=Check_Chrom(*child2);      if (flag) Create_Chrom(child2,flag);    } while (flag);

⌨️ 快捷键说明

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