sundaysweet 发表于 2009-5-19 15:13:01

请教 遗传算法求De Jong函数的最小值 问题

我按照书上的例子做的练习,结果运行有错误,不知道怎么去修改 ,请老师们帮忙看看,谢谢

程序如下:
NIND=40;                                           %个体数目
MAXGEN=300;                                       %最大遗传代数
NVAR=20;                                           %变量维数
PRECI=20;                                          %变量的二进制位数
GGAP=0.9;                                          %代沟
trace=zeros(MAXGEN,2);                           %寻优结果的初始值
FieldD=,);rep([-512;512],);rep(,)];      
Chrom = crtbp(NIND, NVAR*PRECI);                         %初始种群
gen=0;                                             %代计数器
ObjV = objfun1(bs2rv(Chrom,FieldD));            %计算初始种群个体的目标函数值
while gen<MAXGEN
    FitnV = ranking(ObjV);                        %分配适应度值
    SelCh = select('sus', Chrom, FitnV, GGAP);   %选择
    SelCh = recombin('xovsp', SelCh, 0.7);         %重组
    SelCh = mut(SelCh);                            %变异
    ObjVSel=objfun1(bs2rv(SelCh,FieldD));   %计算子代的目标函数值
    = reins(Chrom, SelCh, 1, 1, ObjV, ObjVSel);      %重插入子代的新种群
    gen=gen+1;                                          %代计数器增加
    %输出最优解及其对应的20个自变量的十进制值,Y为最优解,I为种群的序号
    trace(gen,1)=min(ObjV);                            %遗传算法性能跟踪
    trace(gen,2)=sum(ObjV)/length(ObjV);
end
plot(trace(:,1));hold on;
plot(trace(:,2),'-.');grid;
legend('解的变化','种群均值的变化');

程序运行后出现错误:
??? Error: File: G:\GA\遗传算法_matlab\gatbx\yichuan\OBJFUN1.M Line: 33 Column: 33
Illegal use of reserved keyword "switch".

Error in ==> test2 at 13
ObjV = objfun1(bs2rv(Chrom,FieldD));               %计算初始种群个体的目标函数值


objfun1程序是这样的:
function ObjVal = objfun1(Chrom,switch);

% Dimension of objective function
   Dim = 20;
   
% Compute population parameters
    = size(Chrom);

% Check size of Chrom and do the appropriate thing
   % if Chrom is [], then define size of boundary-matrix and values
   if Nind == 0
      % return text of title for graphic output
      if switch == 2
         ObjVal = ['DE JONG function 1-' int2str(Dim)];
      % return value of global minimum
      elseif switch == 3
         ObjVal = 0;
      % define size of boundary-matrix and values
      else   
         % lower and upper bound, identical for all n variables      
         ObjVal = 100*[-5.12; 5.12];
         ObjVal = ObjVal(1:2,ones(Dim,1));
      end
   % if Dim variables, compute values of function
   elseif Nvar == Dim
      % function 1, sum of xi^2 for i = 1 : Dim (Dim=30)
      % n = Dim, -5.12 <= xi <= 5.12
      % global minimum at (xi)=(0) ; fmin=0
      ObjVal = sum((Chrom .* Chrom)')';
      % ObjVal = diag(Chrom * Chrom');% both lines produce the same
   % otherwise error, wrong format of Chrom
   else
      error('size of matrix Chrom is not correct for function evaluation');
   end
怎么回事呢?难道是工具箱中的objfun1有问题吗?请问哪位老师有正确的版本,可以给我一份吗?我的邮箱:huangliqin527@163.com
页: [1]
查看完整版本: 请教 遗传算法求De Jong函数的最小值 问题