爱上纳木错 发表于 2009-9-5 13:45

function = 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 = ;
end
if isempty(opts)
opts = ;
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=['=' eevalFN '(c1,);'];
e2str=['=' eevalFN '(c2,);'];
else %Binary ga
e1str=['x=b2f(endPop(j,:),bounds,bits);=' eevalFN ...
'(x,); endPop(j,:)=;'];
end
end

if n<6 %Default termination information
termOps=;
termFN='maxGenTerm';
end
if n<12 %Default muatation information
if opts(2)==1 %Float GA
mutFNs=['boundaryMutation multiNonUnifMutation nonUnifMutation unifMutation'];
mutOps=;
else %Binary GA
mutFNs=['binaryMutation'];
mutOps=;
end
end
if n<10 %默认的交叉信息
if opts(2)==1 %浮点编码
xOverFNs=['arithXover heuristicXover simpleXover'];
xOverOps=;
else %Binary GA
xOverFNs=['simpleXover'];
xOverOps=;
end
end
if n<9 %Default select opts only i.e. roullete wheel.
selectOps=[];
end
if n<8 %Default select info
selectFN=['normGeomSelect'];
selectOps=;
end
if n<6 %默认的算法终止准则
termOps=;
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)
= 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,:)=;
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,); %选择操作

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,:)); %交叉函数
= feeval(xN,endPop(a,:),endPop(b,:),bounds,);

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,);
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);
= feeval(xN,endPop(a,:),endPop(b,:), bounds,);
end
end
for i=1:numMuts
mN=deblank(mutFNs(i,:));
for j=1:popSize
endPop(j,:) = feeval(mN,endPop(j,:),bounds,);
eeval(e1str);
end
end
end

gen=gen+1;
done=feeval(termFN,,bPop,endPop); %
startPop=endPop; %更新种群

= min(startPop(:,xZomeLength));
startPop(bindx,:) = best;
end
= 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,:)=;
else
bPop(bFoundIn,:)=;
end
if collectTrace
traceInfo(gen,1)=gen;
traceInfo(gen,2)=startPop(bindx,xZomeLength); %Best fittness
traceInfo(gen,3)=mean(startPop(:,xZomeLength)); %Avg fittness
end

dongleitian 发表于 2009-9-5 14:02

很好,谢了

laoli 发表于 2009-9-8 08:42

XIE XIE LE

lrh116 发表于 2009-9-8 15:39

网上很多 呀,,

单车王子 发表于 2009-9-8 16:40

好人啊
爱上那木槿

zsuzengli 发表于 2009-9-9 11:01

10楼的真不错!

alair009 发表于 2012-1-26 11:15

页: 1 [2]
查看完整版本: 200908网友求助《遗传算法的代码》