%%GeneticAlgorithm
function [x,endPop,bPop,traceInfo] = ga(bounds,evalFN,evalOps,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
evalOps=[];
end
if n<5
opts = [1e-6 1 0];
end
if isempty(opts)
opts = [1e-6 1 0];
end
if any(evalFN<48)
if opts(2)==1
e1str=['x=c1; c1(xZomeLength)=', evalFN ';'];
e2str=['x=c2; c2(xZomeLength)=', evalFN ';'];
else
e1str=['x=b2f(endPop(j,:),bounds,bits); endPop(j,xZomeLength)=',...
evalFN ';'];
end
else
if opts(2)==1
e1str=['[c1 c1(xZomeLength)]=' evalFN '(c1,[gen evalOps]);'];
e2str=['[c2 c2(xZomeLength)]=' evalFN '(c2,[gen evalOps]);'];
else
e1str=['x=b2f(endPop(j,:),bounds,bits);[x v]=' evalFN ...
'(x,[gen evalOps]); endPop(j,:)=[f2b(x,bounds,bits) v];'];
end
end
if n<6
termOps=[100];
termFN='maxGenTerm';
end
if n<12
if opts(2)==1
mutFNs=['boundaryMutation multiNonUnifMutation nonUnifMutation unifMutation'];
mutOps=[4 0 0;6 termOps(1) 3;4 termOps(1) 3;4 0 0];
else
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
xOverFNs=['simpleXover'];
xOverOps=[0.6];
end
end
if n<9
selectOps=[];
end
if n<8
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,evalFN,evalOps,opts(1:2));
end
if opts(2)==0
bits=calcbits(bounds,opts(1));
end
xZomeLength = size(startPop,2);
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);
oval = max(startPop(:,xZomeLength));
bFoundIn = 1;
done = 0;
gen = 1;
collectTrace = (nargout>3);
floatGA = opts(2)==1;
display = opts(3);
i=0;
while(~done)
[bval,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(bval - oval)>epsilon) | (gen==1))
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;
oval=bval;
if display
end
i=i+1;
if(i==80)
done=1;
i=0;
end
end
endPop = feval(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] = feval(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
eval(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
eval(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 = feval(deblank(mutFNs(i,:)),endPop(a,:),bounds,[gen mutOps(i,:)]);
if c1(1:numVar)==endPop(a,(1:numVar))
c1(xZomeLength)=endPop(a,xZomeLength);
else
eval(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,:)] = feval(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,:) = feval(mN,endPop(j,:),bounds,[gen mutOps(i,:)]);
eval(e1str);
end
end
gen=gen+1;
done=feval(termFN,[gen termOps],bPop,endPop);
startPop=endPop;
[bval,bindx] = min(startPop(:,xZomeLength));
startPop(bindx,:) = best;
end
[bval,bindx] = max(startPop(:,xZomeLength));
if display
end
x=startPop(bindx,:);
if opts(2)==0
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);
traceInfo(gen,3)=mean(startPop(:,xZomeLength));
end
pc.rar_DC algorithm_converter dc_converter genetic_genetic algor
版权申诉
151 浏览量
2022-09-20
20:44:53
上传
评论
收藏 4KB RAR 举报
局外狗
- 粉丝: 69
- 资源: 1万+