完整的遗传算法函数Matlab程序
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 Tfault 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 Tfault termination information termOps=[100];
termFN='maxGenTerm'; end
if n<12 Tfault 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 Tfault select opts only i.e. roullete wheel. selectOps=[]; end
if n<8 Tfault 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)
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
百度搜索“77cn”或“免费范文网”即可找到本站免费阅读全部范文。收藏本站方便下次阅读,免费范文网,提供经典小说综合文库完整的遗传算法函数Matlab程序在线全文阅读。
相关推荐: