完整的遗传算法函数Matlab程序

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

完整的遗传算法函数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 %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

相关文档
最新文档