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

📄 geneticalgorithm.txt

📁 本程序是用MATALAB编写的遗传算法远程序
💻 TXT
字号:
 f(x1,x2)=100*(x1^2-x2)^2+(1-x1)^2的   
    
  /***************************************************************/   
  *                   Simple   Genetic   Algorithm                               
  *                                   2002/7/18   
  *   
  *                 对于整体过程的说明   
  *         main   
  *                       GenerateInitialPopulation   //产生初始子代,本程序产生0,1   序列     
  *                       EvalutePopulation       
  *                                     |   
  *       |--------CalculateObjectValue       //计算目标函数值     
  *       |   
  *       |--------CalculateFitnessValue     //计算适应度   
  *       |     
  *       |--------FindBestAndWorstIndividual //计算最好与最差个体   
  *         
  *               以下循环到GENERATION代               
  *                       GenerateNextPopulation   
  *                                     |   
  *       |--------SelectOperator                       //选择操作   
  *                                     |   
  *                                     |--------CalculateFitnessValue         //单点交叉   
  *                                     |   
  *                                     |--------FindBestAndWorstIndividual   //变异操作   
  *                           
  *                       EvalutePopulation     //该函数同上   
  *                                                   .......   
  *                                                   .......   
  *                                                   .......   
  *                       PerformEvolution                 //遗传   
  *                       OutputTextReport                 //输出报告   
  *               循环结束           
  ****************************************************************   
  {$N+}                                       //打开8087,因为使用double   
  program   SGA_pascal;           //Simple   Genetic   Algorithm   
  const   
        PopSize=160;                   //Simple   Genetic   Algorithm   
        MAXIMIZATION=1;             //最大标志     
        MINIZATION=2;                 //最小标志   
        Cmax=100;   
        Cmin=0;   
        LENGTH1=10;                     //第一个变量的遗传变量的长度         
        LENGTH2=10;                     //第二个变量的遗传变量的长度     
        CHROMLENGTH=LENGTH1+LENGTH2;     //变量总长度     
        MaxGeneration=400;       //最大遗传代数     
        Pc=0.6;                             //交叉概率   
        Pm=0.001;                         //变异概率   
  type   
      individual   =record         //个体结构     
                              chrom:array[1..CHROMLENGTH]   of   char;   
                              value:double;   
                              fitness:double;   
                              end;   
    
  var   
      
      generation                         :integer;   
      best_index                         :integer;                   //记录最优秀个体     
      worst_index                       :integer;                   //记录最劣质个体   
      bestindividual                 :individual;             //当前代最优秀个体   
      worstindividual               :individual;             //当前代最差个体   
      currentbest                       :individual;             //现在得到的最优秀个体   
      population                         :array[1..PopSize]   of   individual;   
                                                                                      //种群空间       
      functionmode                     :integer;                   //最优化类型   
    
  procedure   init;                         //初始化   
    
  procedure   generateinitpopulation;                   //生成最先一代   
  var   i,j:integer;     
  begin   
      randomize;     
      for   i:=1   to   PopSize   do                                     //种群大小         
          for   j:=1   to   CHROMLENGTH   do                         //基因长度         
              if   random(10)<5     
                    then   population[i].chrom[j]:='0'       //以相同概率产生'0','1'   
                    else   population[i].chrom[j]:='1';       
    
        functionMode:=MAXIMIZATION;                             //最优化类型   
    
  end;   
    
  begin   
      generateinitpopulation;   
  end;   {end   of   init}   
    
    
  procedure   main;   
    
  procedure   EvaluatePopulation;   
    
  function   DecodeChromosome(s:string;strstart,strend:integer):longint;   
  //基因解码过程,对于本程序,是将2进制基因转化成10进制数值   
  var   
        decimal       :longint;     
        temp             :longint;   
        i           :integer;   
  begin   
      decimal:=0;   
      temp:=1;       //以左到右,权值升高   
      for   i:=strstart   to   strend   do   
          begin   
              if   copy(s,i,1)='1'   then   decimal:=decimal+temp;     //转化到十进制   
              temp:=temp*2;   
          end;   
      decodechromosome:=decimal;   
  end;   
    
  procedure   calculateobjectvalue;   
  //计算目标函数值,本程序目前计算为f(x1,x2)=100*(x1^2-x2)^2+(1-x1)^2   
  var   
        i:integer;   
        temp1,temp2:longint;   
        x1,x2:double;   
  begin     
      for   i:=1   to   PopSize   do   
          begin   
              temp1:=DecodeChromosome(population[i].chrom,1,LENGTH1);   
                                  //分别对两个基因解码   
              temp2:=DecodeChromosome(population[i].chrom,LENGTH1+1,LENGTH1+LENGTH2);   
              x1:=4.096*temp1/1023.0-2.048;   
                                  //从这里可以看出,本程序是采用均匀量化   
              x2:=4.096*temp2/1023.0-2.048;   
              population[i].value:=100*(x1*x1-x2)*(x1*x1-x2)+(1-x1)*(1-x1);   
          end;   
  end;   
    
  procedure   calculatefitnessvalue;   
  //计算适应度   
  var   
        i       :integer;   
        temp:double;   
  begin   
      for   i:=1   to   PopSize   do   
          begin   
              if   functionmode=MAXIMIZATION   
                              then     
                                  if   population[i].value+Cmin>0.0     
                                            then   
                                                temp:=Cmin+population[i].value   
                                            else   
                                                temp:=0.0   
                              else   
                                  if   functionmode=MINIZATION     
                                            then   
                                                if   population[i].value<Cmax     
                                                          then   
                                                              temp:=Cmax-population[i].value   
                                                          else   
                                                              temp:=0.0;   
              population[i].fitness:=temp;   
          end;   
  end;   
    
  procedure   findbestandworstindividual;   
  //找到当前种群中最优秀个体   
  var   
      i     :integer;   
      sum:double;   
  begin   
      sum:=0.0;   
      bestindividual:=population[1];   
      worstindividual:=population[1];   
      for   i:=2   to   PopSize   do   
          begin   
              if   population[i].fitness>bestindividual.fitness   
                                then   
                                    begin   
                                        bestindividual:=population[i];   
                                        best_index:=i;   
                                    end;   
              if   population[i].fitness<worstindividual.fitness     
                                then   
                                    begin   
                                        worstindividual:=population[i];   
                                        worst_index:=i;     
                                    end;                                                                                     
              sum:=sum+population[i].fitness;   
          end;   
    
      if   generation=1   
              then   
                  currentbest:=bestindividual   
              else   
                  if   bestindividual.fitness>currentbest.fitness   
                        then   
                            currentbest:=bestindividual;   
  end;   
    
  begin   
      calculateobjectvalue;   
      calculatefitnessvalue;   
      findbestandworstindividual;   
  end;   
    
  procedure   generatenextpopulation;   
    
  procedure   selectionoperator;   
  //通过比例选择产生新的基因   
  var   
      i,index:integer;   
      p,sum:double;   
      cfitness:array[1..PopSize]   of   double;   
      newpopulation:array[1..popsize]   of   individual;   
  begin       
      sum:=0.0;   
      for   i:=1   to   PopSize   do   
          sum:=sum+population[i].fitness;   
        
      for   i:=1   to   PopSize   do   
          cfitness[i]:=population[i].fitness/sum;   
    
        for   i:=2   to   PopSize   do   
          cfitness[i]:=cfitness[i-1]+cfitness[i];   
    
        for   i:=1   to   popsize   do   
          begin   
              p:=random(1000)   /   1000.0;   
              index:=0;   
              while   p>cfitness[index]   do   
                index:=index+1;   
              newpopulation[i]:=population[index];   
        end;   
    
        for   i:=1   to   popsize   do   
            population[i]:=newpopulation[i];   
    
  end;   
    
  procedure   crossoveroperator;   
  //单点交叉   
  var   
        i,j:integer;   
        index:array[1..PopSize]   of   integer;   
        point,temp:integer;   
        p:double;   
        ch:char;   
  begin   
          
      for   i:=1   to   PopSize   do   
                    index[i]:=i;   
      for   i:=1   to   PopSize   do   
          begin     
              point:=random(PopSize-i+1);   
              temp:=index[i];   
              index[i]:=index[point+i];   
              index[point+i]:=temp;   
          end;         
      //随机选择一对个体   
    
      for   i:=1   to   PopSize   do   
          begin   
              p:=random(1000)   /   1000.0;   
              if   p<Pc     
                  then   
                    begin   
                        point:=random(CHROMLENGTH-1)+1;   
                        for   j:=point   to   CHROMLENGTH   do   
                            begin   
                  ch:=population[index[i]].chrom[j];   
                  population[index[i]].chrom[j]:=population[index[i+1]].chrom[j];   
                  population[index[i+1]].chrom[j]:=ch;   
                            end;   
                    end;   
                i:=i+1;   
          end;   
          //单点交叉   
    
  end;   
    
  procedure   mutationoperator;   
  //基因变异   
  var   
      i,j:integer;   
      p:double;   

⌨️ 快捷键说明

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