24小时热门版块排行榜    

查看: 137  |  回复: 0
当前主题已经存档。

someday1985

新虫 (小有名气)

[交流] 【求助】请教关于一循环语句的问题

这是我做的遗传算法主程序, 我想让popuerr小于某个值,从而退出while循环,是不最后用break语句,该怎么写呢  

psize=20;pmutate=0.3;mutatec=0.001;
cyc=0;step=ones(1,psize)*0.001;
A=rand(1,psize)+1.5;
tower=rand(1,psize)+1.0;
Sigmag=rand(1,psize)+1.0;
tg=rand(1,psize)+2.0;                                                                                                                                 
b(:,:,:,1)=A;
b(:,:,:,2)=tower;
b(:,:,:,3)=Sigmag;
b(:,:,:,4)=tg;
d=[b(:,:,:,1),b(:,:,:,2),b(:,:,:,3),b(:,:,:,4)];
popu=d';
m=d';

for o=1:1:20
n=m(:,o);
i=0;
for t=0:0.1:12;
   i=i+1;
  A=n(1,;
  tower=n(2,;
  Sigmag=n(3,;
  tg=n(4,;
Z=((t-tg)/Sigmag)-(Sigmag/tower);
     x=(-1000:0.01:Z)';
        y=(1/sqrt(6.28))*exp(-x.*x/2);
        Vt=trapz(x,y);
        V2(i)=(A/tower)*exp((Sigmag^2)/(2*tower^2)-((t-tg)/tower));
    V2(i)=V2(i)*Vt;

end
V1=dot(A,tower,tg,Sigmag);
popuerr(o)=sumsqr(V2-V1)
end
popuerr

errflow=[];
while cyc<500
        cyc=cyc+1;
        errflow=[errflow,popuerr(1)];

        popufit=fitness(popuerr);       

   parent=select(popufit);

  son1=mate(popu(:,parent));
  son2=mutate(son1,pmutate,mutatec);

son=[son1,son2];
k=son';
m=k';
for o=1:1:40
n=m(:,o);
i=0;
for t=0:0.1:12;
   i=i+1;
  A=n(1,;
  tower=n(2,;
  Sigmag=n(3,;
  tg=n(4,;
Z=((t-tg)/Sigmag)-(Sigmag/tower);
     x=(-1000:0.01:Z)';
        y=(1/sqrt(6.28))*exp(-x.*x/2);
        Vt=trapz(x,y);
        V2(i)=(A/tower)*exp((Sigmag^2)/(2*tower^2)-((t-tg)/tower));
    V2(i)=V2(i)*Vt;
end
V1=dot(A,tower,tg,Sigmag);       
sonerr(o)=sumsqr(V2-V1);
end
sonerr
sonerr=sum(sonerr);
sonfit=fitness(sonerr);
popu=[popu,son];
popufit=[popufit,sonfit];
popuerr=[popuerr,sonerr];
offspr=compet(popufit,psize);
popu=popu(:,offspr);
popuerr=popuerr(offspr);


end

plot(errflow)
disp('The optimal individual is')
popu(:,1)

[ Last edited by someday1985 on 2010-4-13 at 22:50 ]
回复此楼

» 猜你喜欢

已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖
相关版块跳转 我要订阅楼主 someday1985 的主题更新
普通表情 高级回复 (可上传附件)
信息提示
请填处理意见