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

📄 ga.c

📁 本程序是由C语言编写的遗传算法源程序
💻 C
📖 第 1 页 / 共 2 页
字号:
/*  由两个父个体交叉产生两个子个体  */
{
   int  jcross1,jcross2,jtemp,i,j;
   int   temp[20];
   do
     {
         jcross1=rnd(0,14);
         jcross2=rnd(0,14);
         if(jcross1>jcross2)
           {jtemp=jcross1;
            jcross1=jcross2;
            jcross2=jtemp;
            }
     }
   while((jcross1==jcross2)||(jcross1==0)&&(jcross2==(n-1)));
   for(j=0;j<lchrom;j++)
      {
         *(child1++)=*(parent1++);
         *(child2++)=*(parent2++);
      }
   for(i=jcross1;i<=jcross2;i++)
      {
         temp[i]=*(child1+i);
         *(child1+i)=*(child2+i);
         *(child2+i)=temp[i];
       }
}



void    mutation(int  *child)                              /*变异操作*/
{
   int   temp;
   temp=*(child);
   do
     {*(child)=rnd(0,7);}
   while(*(child)==temp);
}


void    objfunc(struct   individual   *critter)          /*  计算适应度  */
{
   FILE  *infp;
   int  i,j;
   float   ct[8][15],cs[8][5],cf[8],d[15],td[8]={0};
   float   tc=0,sc=0,fc=0;
   double   cmax=200000;
/*  从文件中读取数据  */
   if((infp=fopen("data.txt","rb"))==NULL)
   {printf("can   not   open   file\n");
    exit(0);
   }
   for(i=0;i<m;i++)
      {
          for(j=0;j<n;j++)
          fscanf(infp,"%f ",&ct[i][j]);
       }
   for(i=0;i<m;i++)
   {
      for(j=0;j<5;j++)
      fscanf(infp,"%f ",&cs[i][j]);
    }
   for(i=0;i<m;i++)
      fscanf(infp,"%f ",&cf[i]);
   for(i=0;i<n;i++)
      fscanf(infp,"%f ",&d[i]);

   for(i=0;i<lchrom;i++)
      {
         j=(*critter).chrom[i];
         tc=tc+ct[j][i]*d[i];
         td[j]=td[j]+d[i];
       }
   for(j=0;j<m;j++)
     {  if(cs[j][3]>cs[j][4])
         {printf("error");
          exit(0);
          }
       if(td[j]<=cs[j][3])   sc=sc+(td[j]*cs[j][0]);
       else  if(cs[j][3]<td[j]<=cs[j][4])
               sc=sc+cs[j][0]*cs[j][3]+cs[j][1]*(td[j]-cs[j][3]);
        else
sc=sc+cs[j][0]*cs[j][3]+cs[j][1]*(cs[j][4]-cs[j][3])+cs[j][2]*(td[j]-cs[j][4]);
      }
   for(i=0;i<m;i++)
     {
       if(td[i]!=0)
        fc=fc+cf[i];
      }
   (*critter).totalcost=tc+sc+fc;
   (*critter).fitness=cmax-(*critter).totalcost;
   fclose(infp);
}


void   changeobj(struct   individual   *critter)       /*  适应度的尺度转换  */
{
   double  r=0.2;
   (*critter).cfitness=((*critter).fitness-min+r)/(max-min+r);
}


void    report()                                       /*  输出种群统计结果  */
{
   void   skip();
   int i;
   skip(outfp1,1);
   if((gen%20)==0)  skip(outfp1,1);
   if(gen==1||(gen%20==0))
   fprintf(outfp1,"第%d次的%d次迭代每代最好方案总成本\n",run,maxgen);
   fprintf(outfp1,"%d代中最好个体的总成本=%f  ",gen,bestfit.totalcost);
   
   
   fprintf(outfp2,"第%d次的%d次迭代每代最好方案的适应度\n",run,maxgen);
   fprintf(outfp2,"%d代中最好个体的适应度=%f  ",gen,bestfit.fitness);
   if((gen%20)==0)  skip(outfp2,1);
}

void   skip(FILE *fp,int skipcount)
{
   int  j;
   for(j=1;j<=skipcount;j++)   fprintf(fp,"\n");
}



void   advance_random()                     /*   产生55个随机数   */   
  {   
          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(float   prob)                         /*  以一定概率产生0或1  */
{
      float   randomperc();
      if(randomperc()<prob)
           return(1);
      else
           return(0);
}



  void   randomize()                         /*   设定随机数种子并初始化随机数发生器   */   
  {   
          float   randomseed;   
          int   j1;   
          for(j1=0;   j1<=54;   j1++)   
              oldrand[j1]=0.0;   
          jrand=0;   
          randomseed=0.5;   
          warmup_random(randomseed);   
  }   
    
  double   randomnormaldeviate()                   /*   产生随机标准差   */   
  {   
          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=rndx1*sin(t);   
                  rndcalcflag=0;   
                  return(rndx1*cos(t));   
          }   
          else   
          {   
                  rndcalcflag=1;   
                  return(rndx2);   
          }   
  }   
    
  float   randomperc()                         /*与库函数random()作用相同,   产生[0,1]之间一个随机数   */   
  {   
          jrand++;   
          if(jrand>=55)   
          {   
                  jrand=1;   
                  advance_random();   
          }   
          return((float)   oldrand[jrand]);   
  }   
    
  int   rnd(int low,int high)                       /*在整数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);   
  }   
    
    
  void   warmup_random(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;   
  } 


main()                                   /*  主程序  */
{  
   void     skip();
   struct  individual   *temp;
    FILE       *fopen();
    char       *malloc(); 
   int  i;
   if((outfp1=fopen("object.txt","w"))== NULL)
     {printf("can  not  open  object.txt  file \n");
          exit(0);
     }
   if((outfp2=fopen("fitness.txt","w"))==NULL)
     {printf("can  not  open  fitness.txt  file\n");
      exit(0);
      }
   if((outfp3=fopen("project.txt","w"))==NULL)
     {printf("can  not  open  project.txt  file\n");
      exit(0);
      }
   maxruns=10;
   for(run=1;run<=maxruns;run++)
   {
     initialize();
      for(gen=1;gen<=maxgen;gen++)
        {  
           /*产生新的下一代*/
           generation();
           /*计算新一代种群的适应度统计数据*/
           findbest(newpop);
            if(bestfit.fitness>finalresult.fitness)
             {
               finalresult.fitness=bestfit.fitness;
               finalresult.cfitness=bestfit.cfitness;
               finalresult.totalcost=bestfit.totalcost;
               finalresult.generation=bestfit.generation;
               for(i=0;i<lchrom;i++)
               finalresult.chrom[i]=bestfit.chrom[i];
             }
           printf("%d  %d:  %f   %f  ",run,gen,bestfit.totalcost,bestfit.fitness);
           for(i=0;i<lchrom;i++)
             printf("%d,",bestfit.chrom[i]);
           printf("\n");
           /*输出新一代统计数据*/
           report();
           temp=oldpop;
           oldpop=newpop;
           newpop=temp;
        }
     printf("gen=%d代最好结果的总成本为:totalcost=%f\n",finalresult.generation,finalresult.totalcost);
     printf("best  project: ");
     for(i=0;i<lchrom;i++)
     printf("%d,",(finalresult.chrom[i]+1));
     printf("\n");
     fprintf(outfp3,"%d  %d  %f  ",run,finalresult.generation,finalresult.totalcost);
     for(i=0;i<lchrom;i++)
       fprintf(outfp3,"%d,",(finalresult.chrom[i]+1));
     skip(outfp1,2);
     skip(outfp2,2);
     skip(outfp3,2);
     freeall();
  }
}







⌨️ 快捷键说明

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