黑白简单像素图:求C代码:遗传算法求函数最大值f(x)=xsin(10Pix)+1.0

来源:百度文库 编辑:高考问答 时间:2024/04/30 02:09:24
小数点精确6位,函数区间为[-1,2]
定有高分相送!
谢谢各位了!

提示:用22位二进制数,省去编码!x=-1+t*(3/[pow(2,22)-1])其中t为0到[pow(2,22)-1]之间的随机数。

%输出参数:x:求的最优解
% endpop:最终的种群
% bpop:最优种群的一个搜索轨迹
% 输出参数:
% bounds:代表变量的上下界的矩阵
% eevalFN:适应度函数
% startPop:初始群体
% termFN:终止函数的名字
% termOps: 终止函数的参数
% selectFN:选择函数的名称
% selectOpts:选择参数。
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