您好,欢迎访问三七文档
当前位置:首页 > 商业/管理/HR > 项目/工程管理 > 基于遗传算法的PID整定原理及matlab仿真程序
主程序:chap5_2.m%GA(GenericAlgorithm)ProgramtooptimizePIDParametersclearall;closeall;globalrinyouttimefSize=30;CodeL=3;MinX(1)=zeros(1);MaxX(1)=20*ones(1);MinX(2)=zeros(1);MaxX(2)=1.0*ones(1);MinX(3)=zeros(1);MaxX(3)=1.0*ones(1);Kpid(:,1)=MinX(1)+(MaxX(1)-MinX(1))*rand(Size,1);Kpid(:,2)=MinX(2)+(MaxX(2)-MinX(2))*rand(Size,1);Kpid(:,3)=MinX(3)+(MaxX(3)-MinX(3))*rand(Size,1);G=100;BsJ=0;%***************StartRunning***************forkg=1:1:Gtime(kg)=kg;%******Step1:EvaluateBestJ******fori=1:1:SizeKpidi=Kpid(i,:);[Kpidi,BsJ]=chap5_2f(Kpidi,BsJ);BsJi(i)=BsJ;end[OderJi,IndexJi]=sort(BsJi);BestJ(kg)=OderJi(1);BJ=BestJ(kg);Ji=BsJi+1e-10;%Avoidingdevidingzerofi=1./Ji;%Cm=max(Ji);%fi=Cm-Ji;[Oderfi,Indexfi]=sort(fi);%ArrangingfismalltobiggerBestfi=Oderfi(Size);%LetBestfi=max(fi)BestS=Kpid(Indexfi(Size),:);%LetBestS=E(m),mistheIndexfibelongtomax(fi)kgBJBestS%******Step2:SelectandReproductOperation******fi_sum=sum(fi);fi_Size=(Oderfi/fi_sum)*Size;fi_S=floor(fi_Size);%SelectingBiggerfivaluer=Size-sum(fi_S);Rest=fi_Size-fi_S;[RestValue,Index]=sort(Rest);fori=Size:-1:Size-r+1fi_S(Index(i))=fi_S(Index(i))+1;%AddingresttoequalSizeendk=1;fori=Size:-1:1%SelecttheSizethandReproducefirstlyforj=1:1:fi_S(i)TempE(k,:)=Kpid(Indexfi(i),:);%SelectandReproducek=k+1;%kisusedtoreproduceendend%************Step3:CrossoverOperation************Pc=0.90;fori=1:2:(Size-1)temp=rand;ifPctemp%CrossoverConditionalfa=rand;TempE(i,:)=alfa*Kpid(i+1,:)+(1-alfa)*Kpid(i,:);TempE(i+1,:)=alfa*Kpid(i,:)+(1-alfa)*Kpid(i+1,:);endendTempE(Size,:)=BestS;Kpid=TempE;%************Step4:MutationOperation**************Pm=0.10-[1:1:Size]*(0.01)/Size;%Biggerfi,smallerPmPm_rand=rand(Size,CodeL);Mean=(MaxX+MinX)/2;Dif=(MaxX-MinX);fori=1:1:Sizeforj=1:1:CodeLifPm(i)Pm_rand(i,j)%MutationConditionTempE(i,j)=Mean(j)+Dif(j)*(rand-0.5);endendend%GuaranteeTempE(Size,:)belongtothebestindividualTempE(Size,:)=BestS;Kpid=TempE;endBestfiBestSBest_J=BestJ(G)figure(1);plot(time,BestJ);xlabel(’Times’);ylabel(’BestJ’);figure(2);plot(timef,rin,’r’,timef,yout,’b’);xlabel(’Time(s)’);ylabel(’rin,yout’);子程序:chap5_2f.mfunction[Kpidi,BsJ]=pid_gaf(Kpidi,BsJ)globalrinyouttimefts=0.001;sys=tf(400,[1,50,0]);dsys=c2d(sys,ts,’z’);[num,den]=tfdata(dsys,’v’);rin=1.0;u_1=0.0;u_2=0.0;y_1=0.0;y_2=0.0;x=[0,0,0]’;B=0;error_1=0;tu=1;s=0;P=100;fork=1:1:Ptimef(k)=k*ts;r(k)=rin;u(k)=Kpidi(1)*x(1)+Kpidi(2)*x(2)+Kpidi(3)*x(3);ifu(k)=10u(k)=10;endifu(k)=-10u(k)=-10;endyout(k)=-den(2)*y_1-den(3)*y_2+num(2)*u_1+num(3)*u_2;error(k)=r(k)-yout(k);%------------ReturnofPIDparameters-------------u_2=u_1;u_1=u(k);y_2=y_1;y_1=yout(k);x(1)=error(k);%CalculatingPx(2)=(error(k)-error_1)/ts;%CalculatingDx(3)=x(3)+error(k)*ts;%CalculatingIerror_2=error_1;error_1=error(k);ifs==0ifyout(k)0.95&yout(k)1.05tu=timef(k);s=1;endendendfori=1:1:PJi(i)=0.999*abs(error(i))+0.01*u(i)^2*0.1;B=B+Ji(i);ifi1erry(i)=yout(i)-yout(i-1);iferry(i)0B=B+100*abs(erry(i));endendendBsJ=B+0.2*tu*10;
本文标题:基于遗传算法的PID整定原理及matlab仿真程序
链接地址:https://www.777doc.com/doc-5477398 .html