基于遗传算法的PID参数整定研究(八)

基于遗传算法的PID参数整定研究

下面对实数编码下遗传算法的PID参数整定代码进行详细讲解,并附上程序。

1.3.3基于遗传算法的PID参数整定代码

主函数 main.m

%GA(Generic Algorithm) Program to optimize PID Parameters
clear all;
close all;
global rin yout timef

Size=30;  % 种群大小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;    % 种群运行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]=chap_f(Kpidi,BsJ);

BsJi(i)=BsJ;
end
 
[OderJi,IndexJi]=sort(BsJi);
BestJ(kg)=OderJi(1);
BJ=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)
   
   kg   
   BJ
   BestS
%****** Step 2 : Select and Reproduct Operation******
   fi_sum=sum(fi);
   fi_Size=(Oderfi/fi_sum)*Size;
   
   fi_S=floor(fi_Size);                    % Selecting Bigger fi value
   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;     % Adding rest to equal Size
   end

   k=1;
   for i=Size:-1:1       % Select the Sizeth and Reproduce firstly  
      for j=1:1:fi_S(i)  
       TempE(k,:)=Kpid(Indexfi(i),:);      % Select and Reproduce 
         k=k+1;                            % k is used to reproduce
      end
   end
   
%************ Step 3 : Crossover Operation ************
    Pc=0.90;
    for i=1:2:(Size-1)
          temp=rand;
      if Pc>temp                      %Crossover Condition
          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 Condition
            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
Bestfi
BestS
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');

目标函数编写 chap_f.m

function [Kpidi,BsJ]=pid_gaf(Kpidi,BsJ)
global rin yout timef

ts=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;

for k=1:1:P
   timef(k)=k*ts;
   r(k)=rin;
   
   u(k)=Kpidi(1)*x(1)+Kpidi(2)*x(2)+Kpidi(3)*x(3); 
   
   if u(k)>=10
      u(k)=10;
   end
   if u(k)<=-10
      u(k)=-10;
   end   
   
   yout(k)=-den(2)*y_1-den(3)*y_2+num(2)*u_1+num(3)*u_2;
   error(k)=r(k)-yout(k);
%------------ Return of PID parameters -------------
   u_2=u_1;u_1=u(k);
   y_2=y_1;y_1=yout(k);
   
   x(1)=error(k);                % Calculating P
   x(2)=(error(k)-error_1)/ts;   % Calculating D
   x(3)=x(3)+error(k)*ts;        % Calculating I
   error_2=error_1;
   error_1=error(k);
   
if s==0
   if yout(k)>0.95&yout(k)<1.05
      tu=timef(k); % 上升时间
      s=1;
   end 
end
end

for i=1:1:P
   Ji(i)=0.999*abs(error(i))+0.01*u(i)^2*0.1;
   B=B+Ji(i);   
  if i>1   
   erry(i)=yout(i)-yout(i-1);
   if erry(i)<0
      B=B+100*abs(erry(i));
   end    
  end
end
BsJ=B+0.2*tu*10;

程序运行结果:
在这里插入图片描述

在这里插入图片描述
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/qq_42249050/article/details/106116705