[求助] 能不能举个关于遗传算法的例子,谢谢
对于遗传算法,一直不是很清楚,参数好象很多 先搜索一下,论坛已经有很多讨论和例子。 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 以上是遗传算法的源码,下面是实例求DeJong函数
f(x)=Σx(i)^2 i=1...n
求min f(x) -512<=x(i)<=512
%二维DeJong函数图形
= meshgrid(-512:4:512);
f=x1.^2+x2.^2;
mesh(x1,x2,f);
xlabel('x1');
ylabel('x2');
zlabel('f(x1,x2)');
%DeJong函数源码
function =dejong(sol)
numv = size(sol,2);
x=sol(1:numv);
eval=sum(x.^2);
%计算DeJong函数
function =dejongmin(sol,options)
numv = size(sol,2)-1;
x=sol(1:numv);
eval=dejong(x);
eval=-eval;
%遗传算法求解DeJong函数源码
%维数n=3
%设置参数边界
bounds = ones(3,1)*[-512 512];
%遗传算法优化
=ga(bounds,'dejongmin');
%性能跟踪
plot(trace(:,1),trace(:,3),'b-')
hold on
plot(trace(:,1),trace(:,2),'r-')
xlabel('Generation');
ylabel('Fittness');
legend('解的变化','种群平均值的变化'); 谢谢大侠!!! 大侠:再请教一个问题
syms a b c s w
H0=(a*s^2+c)/(s^3+a*s^2+b*s+c);
H1=(s^3+b*s)/(s^3+a*s^2+b*s+c);
H0w=subs(H0,s,j*w);
H1w=subs(H1,s,j*(w-pi));
I=int(abs(H0w-H1w),w,0,pi);
怎样优化函数I,是其取得最小值0,并求得此时a,b,c 三个参数
谢谢!! 应该可以用遗传算法,牛顿叠代好象也可以 有没有人再回答一次,谢谢!! 自己顶上去!! 再顶!! 我顶!! 有人回答一下吗? 原帖由 lovv 于 2006-10-13 11:27 发表
有没有人再回答一次,谢谢!!
前面不是已经给出回答了么?自己思考一下,了解一下遗传算法的使用过程再来发文。很显然,前面的信息加上论坛一些讨论 对你解决这个问题已经足够了。
请不要多次无意义的"顶"之类的灌水,否则你会受到警告的。
页:
[1]