这是一个优化问题的目标函数,
function modeobjectfun(f1:single;f2:single;f3:single):single;//求解目标函数的模; f1为目标函数1,f2为目标函数2  f3为目标函数3
   begin
      Result:=Sqrt(f1*f1+f2*f2+f3*f3);
   end;这是我网上下载的一个遗传算法,请问如何接近程序里?
unit Unit1;{
计算
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         //输出报告
*        循环结束    
****************************************************************
}
interfaceuses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;const
  CPopSize = 160 ;          //Simple Genetic Algorithm
  //基因长度
  CLENGTH1 = 11 ;           //第一个变量的遗传变量的长度
  CLENGTH2 = 11 ;           //第二个变量的遗传变量的长度
  CCHROMLENGTH = CLENGTH1 + CLENGTH2 ;   //变量总长度  CMAXIMIZATION = 1 ;       //最大标志
  CMINIZATION = 2;         //最小标志  CCmax=100;
  CCmin = 0;  CPc=0.6;               //交叉概率
  CPm=0.05;             //变异概率
  CMaxGeneration=1500;    //最大遗传代数type
   individual = record     //个体结构
   chrom: array[1..CCHROMLENGTH] of char;
   value: double;
   fitness: double;
end;type TGIndividual = array[1..CPopSize] of individual;type
  TForm1 = class(TForm)
    Button2: TButton;
    ListBox1: TListBox;
    procedure Button2Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
    procedure generateinitpopulation(PopSize,CHROMLENGTH,MAXIMIZATION: integer;
        var functionmode: integer;var population: TGIndividual);
    procedure Init();
    function DecodeChromosome(s: string;strstart,strend: integer):longint;
    procedure calculateobjectvalue(PopSize,LENGTH1,LENGTH2: integer;
                var population: TGIndividual);
    procedure calculatefitnessvalue(PopSize,MAXIMIZATION,MINIZATION,CMIN,Cmax: integer;
                var functionmode: integer;var population: TGIndividual);
    procedure EvaluatePopulation;
    procedure generatenextpopulation;
    procedure selectionoperator(PopSize: integer;
                 var population: TGIndividual);
  //  procedure crossoveroperator(PopSize,Pc: integer;
    //             var population: TGIndividual);
    procedure crossoveroperator(PopSize,CHROMLENGTH: integer; Pc: double;
                var population: TGIndividual);    procedure findbestandworstindividual(PopSize: integer;var bestindividual,worstindividual,currentbest: individual;
                 var population: TGIndividual;var best_index,worst_index,generation: integer);
    procedure mutationoperator(PopSize,CHROMLENGTH: integer; pm: double;
               var population: TGIndividual);
    procedure performevolution(bestindividual: individual;var currentbest: individual;
                 best_index,worst_index: integer; var population: TGIndividual);
    procedure GeneticAlgorithmMain();
    procedure outputtextreport;
  end;var
  Form1: TForm1;
  g_functionmode: integer;//最优化类型
  g_population: TGIndividual;//种群空间
  g_bestindividual: individual;       //当前代最优秀个体
  g_worstindividual: individual;       //当前代最差个体
  g_generation: integer;
  g_currentbest: individual;       //现在得到的最优秀个体
  g_best_index: integer;          //记录最优秀个体
  g_worst_index: integer;          //记录最劣质个体
implementation{$R *.dfm}//生成最先一代
procedure TForm1.generateinitpopulation(PopSize,CHROMLENGTH,MAXIMIZATION: integer;
        var functionmode: integer;var population: TGIndividual);
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;               //最优化类型
   functionMode := CMINIZATION ;
end;

解决方案 »

  1.   

    接着上面
    procedure TForm1.Init();
    begin
      //生成新一代
      generateinitpopulation(CPopSize,CCHROMLENGTH,CMAXIMIZATION,g_functionmode,g_population);
    end;procedure TForm1.EvaluatePopulation;
    begin
      //计算每个个体的函数值
      calculateobjectvalue(CPopSize,CLENGTH1,CLENGTH2,g_population);
      //计算每个个体的适应度
      calculatefitnessvalue(CPopSize,CMAXIMIZATION,CMINIZATION,CCMIN,CCmax,
                   g_functionmode,g_population);
      //找到最好的个体和最差的个体
      findbestandworstindividual(CPopSize,g_bestindividual,g_worstindividual,g_currentbest,
                     g_population,g_best_index,g_worst_index,g_generation);
    end;procedure TForm1.generatenextpopulation;
    begin
      //按概率选择新的个体,产生新的一代
      mutationoperator(CPopSize,CCHROMLENGTH,Cpm,g_population);
      crossoveroperator(CPopSize,CCHROMLENGTH,CPc,g_population);
      selectionoperator(CPopSize,g_population);
    end;function TForm1.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 TForm1.calculateobjectvalue(PopSize,LENGTH1,LENGTH2: integer;
                    var population: TGIndividual);
    //计算目标函数值,本程序目前计算为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);
           population[i].value := 10.2 + (x1 * x1 - 10 * cos (2 * pi * x1 ))
                                  + ( x2 - 10 * cos (2 * pi * x2 ) );
        //   listbox1.Items.Add(floattostr(x1));
        //   listbox1.Items.Add(floattostr(x2));
         //  population[i].value := ( x1 + x2 ) * ( x1 + x2 ) - 4 * ( x1 + x2 ) + 8 ;
      //     listBox1.Items.Add(floattostr( x1 + x2 ));
         end;
    end;procedure TForm1.calculatefitnessvalue(PopSize,MAXIMIZATION,MINIZATION,CMIN,Cmax: integer;
        var functionmode: integer;var population: TGIndividual);
    //计算适应度
    var
        i: integer;
        temp: double;
    begin
       temp := 0 ;
       for i := 1 to PopSize do
       begin
           if functionmode = MAXIMIZATION then
           begin
             //常数Cmin := 0 
             if population[i].value + Cmin > 0.0  then
                temp := Cmin+population[i].value
             else
                temp := 0.0 ;
           end       else
           begin
            if functionmode = MINIZATION then
              if population[i].value < Cmax then
                temp := Cmax-population[i].value
              else
                temp := 0.0;
           end;
           
           population[i].fitness := temp;
       end;
    end;procedure TForm1.findbestandworstindividual(PopSize: integer;var bestindividual,worstindividual,currentbest: individual;
                     var population: TGIndividual;var best_index,worst_index,generation: integer);
    //找到当前种群中最优秀个体
    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;procedure TForm1.selectionoperator(PopSize: integer;
                  var population: TGIndividual);
    //通过比例选择产生新的基因
    var
       i,index,j: integer;
       p,sum: double;
       cfitness:array [1..CPopSize] of double;
       newpopulation: TGIndividual;
    begin  
       sum := 0.0;   //求得上一代适应度的总和
       for i := 1 to PopSize do
         sum := sum + population[i].fitness;   //求得上一代每个个体的的适应度占总适应度的百分比
       for i := 1 to PopSize do
       begin
         if sum = 0 then
           cfitness[i] := 0
         else
           cfitness[i] := population[i].fitness / sum;
       end;
       //使得每一个适应度为前面的累加  
       for i := 2 to PopSize do
         cfitness[i] := cfitness[i-1] + cfitness[i];    //按概率得到新的种群 
        for i := 1 to popsize do
        begin
           Randomize ;
           p := random(1000) / 1000.0;
           
           index := 1;
           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 TForm1.crossoveroperator(PopSize,CHROMLENGTH: integer; Pc: double;
                    var population: TGIndividual);
    //单点交叉
    var
        i,j:integer;
        index:array[1..CPopSize] of integer;
        point,temp:integer;
        p:double;
        ch:char;
    begin
       Randomize;   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;
       //随机选择一对个体,单点交叉
       i := 1 ;
       while i <= 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 + 2 ;
       end;
    end;procedure TForm1.mutationoperator(PopSize,CHROMLENGTH: integer; pm: double;
                   var population: TGIndividual);
    //基因变异
    var
       i,j: integer;
       p: double;
    begin
       for i := 1 to PopSize do
        for j := 1 to CHROMLENGTH do
          begin
            p:=random(1000) / 1000.0; //这里控制随机发生变异
            if p < Pm then
            begin
              if population[i].chrom[j] = '0' then
                population[i].chrom[j] := '1'
              else population[i].chrom[j] := '0';
            end;
          end;
    end;procedure TForm1.performevolution(bestindividual: individual;var currentbest: individual;
                     best_index,worst_index: integer; var population: TGIndividual);
    //进化
    begin
       if bestindividual.fitness>currentbest.fitness then
         currentbest:=population[best_index]
       else
         population[worst_index] := currentbest;
    end;procedure TForm1.GeneticAlgorithmMain();
    begin
      g_generation := 1;
      EvaluatePopulation;
      while g_generation < CMaxGeneration do
      begin
        g_generation := g_generation + 1;
        generatenextpopulation;
        EvaluatePopulation;
        performevolution(g_bestindividual,g_currentbest,g_best_index,g_worst_index,g_population);
      end;  outputtextreport;
    end;procedure TForm1.outputtextreport();
    var
       i,j:integer;
       str: string;
    begin
       for j:=1 to Cpopsize do
       begin
          str := 'NO.' + inttostr(j);
          Str := Str + '  ' + '基因1:';
          for i:=1 to Clength1 do
             Str := str + g_currentbest.chrom[i] + ' ' ;
          Str := Str + '  ' + '基因2:';
          for i:=clength1+1 to clength1+clength2 do
             Str := Str + g_currentbest.chrom[i] + ' ' ;
          Str := Str + '  ' + ' 适应度:';
          Str := Str + FloatToStr( g_currentbest.fitness );      Str := Str + '  ' + ' 函数值:';
          Str := Str + FloatToStr( g_currentbest.value );
          ListBox1.Items.Add(Str);
       end;
       ListBox1.Items.Add(inttostr(g_generation));
    end;
    procedure TForm1.Button2Click(Sender: TObject);
    begin
      ListBox1.Items.Add('begin');
      Init();
      GeneticAlgorithmMain();
      ListBox1.Items.Add('end');
    end;end.