⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 pid_yh.m

📁 遗传算法对PID参数的优化
💻 M
字号:
%PID_YH.m
%GA(Generic Algorithm)Program is to optimize PID Parameters
clear all;
close all;
global rin yout timef
Size=30;
CodeL=3;
MinX(1)=zeros(1);
MaxX(1)=40*ones(1);
MinX(2)=zeros(1);
MaxX(2)=1.0*ones(1);
MinX(3)=zeros(1);
MaxX(3)=30.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;
%******************Start Running*****************
for kg=1:1:G
    time(kg)=kg;
%****** Step 1:Evaluate BestJ *******
for i=1:1:Size
    Kpidi=Kpid(i,:);
    [Kpidi,BsJ]=PID_EVL(Kpidi,BsJ);
    BsJi(i)=BsJ;
end
[OderJi,IndexJi]=sort(BsJi);
BestJ(kg)=OderJi(1);
BestJ(kg);
Ji=BsJi+1e-10;      %Avoiding deviding zero
fi=1./Ji;
%   Cm=max(Ji);
%   fi=Cm-Ji;
[Oderfi,Indexfi]=sort(fi);      %Arranging fi small to bigger
Bestfi=Oderfi(Size);            %Let Bestfi=max(fi)
BestS=Kpid(Indexfi(Size),:);    %Let BestS=E(m),m is the Indexfi belong to max(fi)
%****** Step 2:Select and Reproduct Operation******
fi_sum=sum(fi);
fi_Size=(Oderfi/fi_sum)*Size;
fi_S=floor(fi_Size);
r=Size-sum(fi_S);
Rest=fi_Size-fi_S;
[RestValue,Index]=sort(Rest);
for i=Size:-1:Size-r+1
    fi_S(Index(i))=fi_S(Index(i))+1;
end
k=1;
for i=Size:-1:1
    for j=1:1:fi_S(i)
        TempE(k,:)=Kpid(Indexfi(i),:);
        k=k+1;
    end
end


%*********************step 3:Crossover Operation********************
Pc=0.90;
for i=1:2:(Size-1)
        temp=rand;
    if Pc>temp
         alfa=rand;
         TempE(i,:)=alfa*Kpid(i+1,:)+(1-alfa)*Kpid(i,:);
         TempE(i+1,:)=alfa*Kpid(i,:)+(1-alfa)*Kpid(i+1,:);
    end
end
TempE(Size,:)=BestS;
Kpid=TempE;
%*********************step 4:Mutation Operation********************
Pm=0.10-[1:1:Size]*(0.01)/Size;        %Bigger fi,smaller Pm
Pm_rand=rand(Size,CodeL);
Mean=(MaxX+MinX)/2;
Dif=(MaxX-MinX);
    for i=1:1:Size
        for j=1:1:CodeL
            if Pm(i)>Pm_rand(i,j)      %Mutation Operation
               TempE(i,j)=Mean(j)+Dif(j)*(rand-0.5);
            end
        end
    end 
    %Guarantee TempE(Size,:)belong to the best individual
TempE(Size,:)=BestS;
Kpid=TempE;
end
Best_J=BestJ(G)
figure(1);
plot(time,BestJ);
xlabel('Times');ylabel('Best J');
figure(2);
plot(timef,rin,'r',timef,yout,'b');
xlabel('Time(s)');ylabel('rin,yout');



⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -