声振论坛

 找回密码
 我要加入

QQ登录

只需一步,快速开始

查看: 1798|回复: 12

[综合讨论] [求助] 能不能举个关于遗传算法的例子,谢谢

[复制链接]
发表于 2006-10-10 16:53 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?我要加入

x
对于遗传算法,一直不是很清楚,参数好象很多
回复
分享到:

使用道具 举报

发表于 2006-10-10 17:08 | 显示全部楼层
先搜索一下,论坛已经有很多讨论和例子。
发表于 2006-10-10 19:41 | 显示全部楼层
function [x,endPop,bPop,traceInfo] = ga(bounds,eevalFN,eevalOps,startPop,opts,...
termFN,termOps,selectFN,selectOps,xOverFNs,xOverOps,mutFNs,mutOps)

n=nargin;
if n<2 | n==6 | n==10 | n==12
  disp('Insufficient arguements')
end
if n<3 %Default eevalation opts.
  eevalOps=[];
end
if n<5
  opts = [1e-6 1 0];
end
if isempty(opts)
  opts = [1e-6 1 0];
end

if any(eevalFN<48) %Not using a .m file
  if opts(2)==1 %Float ga
    e1str=['x=c1; c1(xZomeLength)=', eevalFN ';'];  
    e2str=['x=c2; c2(xZomeLength)=', eevalFN ';'];  
  else %Binary ga
    e1str=['x=b2f(endPop(j,:),bounds,bits); endPop(j,xZomeLength)=',...
        eevalFN ';'];
  end
else %Are using a .m file
  if opts(2)==1 %Float ga
    e1str=['[c1 c1(xZomeLength)]=' eevalFN '(c1,[gen eevalOps]);'];  
    e2str=['[c2 c2(xZomeLength)]=' eevalFN '(c2,[gen eevalOps]);'];  
  else %Binary ga
    e1str=['x=b2f(endPop(j,:),bounds,bits);[x v]=' eevalFN ...
        '(x,[gen eevalOps]); endPop(j,:)=[f2b(x,bounds,bits) v];'];  
  end
end


if n<6 %Default termination information
  termOps=[100];
  termFN='maxGenTerm';
end
if n<12 %Default muatation information
  if opts(2)==1 %Float GA
  mutFNs=['boundaryMutation multiNonUnifMutation nonUnifMutation unifMutation'];
    mutOps=[4 0 0;6 termOps(1) 3;4 termOps(1) 3;4 0 0];
  else %Binary GA
    mutFNs=['binaryMutation'];
    mutOps=[0.05];
  end
end
if n<10    %默认的交叉信息
  if opts(2)==1    %浮点编码
    xOverFNs=['arithXover heuristicXover simpleXover'];
    xOverOps=[2 0;2 3;2 0];
  else %Binary GA
    xOverFNs=['simpleXover'];
    xOverOps=[0.6];
  end
end
if n<9 %Default select opts only i.e. roullete wheel.
  selectOps=[];
end
if n<8    %Default select info
  selectFN=['normGeomSelect'];
  selectOps=[0.08];
end
if n<6    %默认的算法终止准则
  termOps=[100];
  termFN='maxGenTerm';
end
if n<4    %初始种群为空
  startPop=[];
end
if isempty(startPop)    %随机生成初始种群
  startPop=initializega(80,bounds,eevalFN,eevalOps,opts(1:2));
end

if opts(2)==0    %二进制编码
  bits=calcbits(bounds,opts(1));
end

xOverFNs=parse(xOverFNs);
mutFNs=parse(mutFNs);

xZomeLength  = size(startPop,2);         %Length of the xzome=numVars+fittness
numVar       = xZomeLength-1;    %变量数目
popSize      = size(startPop,1);            %种群中个体数目
endPop       = zeros(popSize,xZomeLength);    %次种群矩阵
c1           = zeros(1,xZomeLength);              %个体
c2           = zeros(1,xZomeLength);                 %个体
numXOvers    = size(xOverFNs,1);                         %交叉操作次数
numMuts      = size(mutFNs,1);                         %变异操作次数
epsilon      = opts(1);                                 %适应度门限值
oeval         = max(startPop(:,xZomeLength)); %初始种群中的最优值
bFoundIn     = 1;
done         = 0;
gen          = 1;
collectTrace = (nargout>3);
floatGA      = opts(2)==1;
display      = opts(3);

while(~done)
  [beval,bindx] = max(startPop(:,xZomeLength));     %当前种群的最优值
  best =  startPop(bindx,:);

  if collectTrace
    traceInfo(gen,1)=gen;                           %当前代
    traceInfo(gen,2)=startPop(bindx,xZomeLength);       %最优适应度
    traceInfo(gen,3)=mean(startPop(:,xZomeLength));     %平均适应度
    traceInfo(gen,4)=std(startPop(:,xZomeLength));
  end
  
  if ( (abs(beval - oeval)>epsilon) | (gen==1))
    if display
      fprintf(1,'\n%d %f\n',gen,beval);         
    end
    if floatGA
      bPop(bFoundIn,:)=[gen startPop(bindx,:)];
    else
      bPop(bFoundIn,:)=[gen b2f(startPop(bindx,1:numVar),bounds,bits)...
          startPop(bindx,xZomeLength)];
    end
    bFoundIn=bFoundIn+1;                    
    oeval=beval;                              
  else
    if display
      fprintf(1,'%d ',gen);                      
    end
  end
  
  endPop = feeval(selectFN,startPop,[gen selectOps]);    %选择操作
  
  if floatGA
    for i=1:numXOvers,
      for j=1:xOverOps(i,1),
        a = round(rand*(popSize-1)+1);    %一个父代个体
        b = round(rand*(popSize-1)+1);    %另一个父代个体
        xN=deblank(xOverFNs(i,:));   %交叉函数
        [c1 c2] = feeval(xN,endPop(a,:),endPop(b,:),bounds,[gen… xOverOps(i,:)]);
       
        if c1(1:numVar)==endPop(a,(1:numVar))
          c1(xZomeLength)=endPop(a,xZomeLength);
        elseif c1(1:numVar)==endPop(b,(1:numVar))
          c1(xZomeLength)=endPop(b,xZomeLength);
        else
          eeval(e1str);
        end
        if c2(1:numVar)==endPop(a,(1:numVar))
          c2(xZomeLength)=endPop(a,xZomeLength);
        elseif c2(1:numVar)==endPop(b,(1:numVar))
          c2(xZomeLength)=endPop(b,xZomeLength);
        else
          eeval(e2str);
        end      
       
        endPop(a,:)=c1;
        endPop(b,:)=c2;
      end
    end
  
    for i=1:numMuts,
      for j=1:mutOps(i,1),
        a = round(rand*(popSize-1)+1);
        c1 = feeval(deblank(mutFNs(i,:)),endPop(a,:),bounds,[gen mutOps(i,:)]);
        if c1(1:numVar)==endPop(a,(1:numVar))
          c1(xZomeLength)=endPop(a,xZomeLength);
        else
          eeval(e1str);
        end
        endPop(a,:)=c1;
      end
    end
   
  else    %遗传操作的统计模型
    for i=1:numXOvers,
      xN=deblank(xOverFNs(i,:));        
      cp=find(rand(popSize,1)<xOverOps(i,1)==1);
      if rem(size(cp,1),2) cp=cp(1:(size(cp,1)-1)); end
      cp=reshape(cp,size(cp,1)/2,2);
      for j=1:size(cp,1)
         a=cp(j,1); b=cp(j,2);
         [endPop(a,:) endPop(b,:)] = feeval(xN,endPop(a,:),endPop(b,:), bounds,[gen xOverOps(i,:)]);
      end
    end
    for i=1:numMuts
      mN=deblank(mutFNs(i,:));
      for j=1:popSize
           endPop(j,:) = feeval(mN,endPop(j,:),bounds,[gen mutOps(i,:)]);
           eeval(e1str);
      end
    end
  end
  
  gen=gen+1;
  done=feeval(termFN,[gen termOps],bPop,endPop); %
  startPop=endPop;                         %更新种群
  
  [beval,bindx] = min(startPop(:,xZomeLength));
  startPop(bindx,:) = best;                
end

[beval,bindx] = max(startPop(:,xZomeLength));
if display
  fprintf(1,'\n%d %f\n',gen,beval);          
end

x=startPop(bindx,:);
if opts(2)==0 %binary
    x=b2f(x,bounds,bits);
bPop(bFoundIn,:)=[gen b2f(startPop(bindx,1:numVar),bounds,bits), startPop(bindx,xZomeLength)];
else
  bPop(bFoundIn,:)=[gen startPop(bindx,:)];
end
if collectTrace
  traceInfo(gen,1)=gen;                
  traceInfo(gen,2)=startPop(bindx,xZomeLength); %Best fittness
  traceInfo(gen,3)=mean(startPop(:,xZomeLength)); %Avg fittness
end

评分

1

查看全部评分

发表于 2006-10-10 20:04 | 显示全部楼层
以上是遗传算法的源码,下面是实例求DeJong函数
f(x)=Σx(i)^2     i=1...n
求min f(x)        -512<=x(i)<=512
%二维DeJong函数图形
[x1,x2] = meshgrid(-512:4:512);
f=x1.^2+x2.^2;
mesh(x1,x2,f);
xlabel('x1');
ylabel('x2');
zlabel('f(x1,x2)');
%DeJong函数源码
function [eval]=dejong(sol)
numv = size(sol,2);
x=sol(1:numv);
eval=sum(x.^2);
%计算DeJong函数
function [sol,eval]=dejongmin(sol,options)
numv = size(sol,2)-1;
x=sol(1:numv);
eval=dejong(x);
eval=-eval;
%遗传算法求解DeJong函数源码
%维数n=3
%设置参数边界
bounds = ones(3,1)*[-512 512];
%遗传算法优化
[p,endPop,bestSols,trace]=ga(bounds,'dejongmin');

%性能跟踪
plot(trace(:,1),trace(:,3),'b-')
hold on
plot(trace(:,1),trace(:,2),'r-')
xlabel('Generation');
ylabel('Fittness');
legend('解的变化','种群平均值的变化');
 楼主| 发表于 2006-10-11 11:08 | 显示全部楼层
谢谢大侠!!!
 楼主| 发表于 2006-10-11 11:17 | 显示全部楼层
大侠:再请教一个问题

syms a b c s w
H0=(a*s^2+c)/(s^3+a*s^2+b*s+c);
H1=(s^3+b*s)/(s^3+a*s^2+b*s+c);
H0w=subs(H0,s,j*w);
H1w=subs(H1,s,j*(w-pi));
I=int(abs(H0w-H1w),w,0,pi);

怎样优化函数I,是其取得最小值0,并求得此时a,b,c 三个参数

谢谢!!
 楼主| 发表于 2006-10-11 11:18 | 显示全部楼层
应该可以用遗传算法,牛顿叠代好象也可以
 楼主| 发表于 2006-10-13 11:27 | 显示全部楼层
有没有人再回答一次,谢谢!!
 楼主| 发表于 2006-10-16 09:32 | 显示全部楼层
自己顶上去!!
 楼主| 发表于 2006-10-16 17:02 | 显示全部楼层
再顶!!
 楼主| 发表于 2006-10-17 11:56 | 显示全部楼层
我顶!!
 楼主| 发表于 2006-10-19 16:25 | 显示全部楼层
有人回答一下吗?
发表于 2006-10-19 19:33 | 显示全部楼层
原帖由 lovv 于 2006-10-13 11:27 发表
有没有人再回答一次,谢谢!!


前面不是已经给出回答了么?自己思考一下,了解一下遗传算法的使用过程再来发文。很显然,前面的信息加上论坛一些讨论 对你解决这个问题已经足够了。

请不要多次无意义的"顶"之类的灌水,否则你会受到警告的。
您需要登录后才可以回帖 登录 | 我要加入

本版积分规则

QQ|小黑屋|Archiver|手机版|联系我们|声振论坛

GMT+8, 2024-5-18 09:49 , Processed in 0.064917 second(s), 21 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表