基于遗传算法的PID参数整定研究
下面对二进制编码下遗传算法的PID参数整定代码进行详细讲解,并附上程序。
1.3.3基于遗传算法的PID参数整定代码
采用二进制编码方式,用长度为10位的二进制编码出分别表示三个决策变量kP,ki,kD。
最优指标的选取同十进制编码遗传算法的PID整定。遗传算法中使用的样本个数为Size=30,交叉概率和变异概率分别为:Pc =0.60,Mu =0.001-[1:1:Size]×0.001/Size。参数kP的取值范围为[0,20],ki,kD的取值范围为[0,1],w1,w2,w3,w4的取值同十进制编码遗传算法的PID整定。经过100代进化,获得的优化参数如下:
最优个体为BestS=[000011111100010111000000000000]。PID优化参数为:kP=19.7067,ki =0.2268,kD=0,性能指标J=24.6931,整定过程中代价函数J的变化如图12所示。采用整定后的二进制遗传算法优化PID阶跃响应如图13所示。
主函数 main.m
%GA(Generic Algorithm) Program to optimize Parameters of PID
clear all;
close all;
global rin yout timef
G=100;
Size=30;
CodeL=10;
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);
E=round(rand(Size,3*CodeL)); %Initial Code!
BsJ=0; %指标值
for kg=1:1:G
time(kg)=kg;
for s=1:1:Size
m=E(s,:);
y1=0;y2=0;y3=0;
m1=m(1:1:CodeL);
for i=1:1:CodeL
y1=y1+m1(i)*2^(i-1);
end
Kpid(s,1)=(MaxX(1)-MinX(1))*y1/1023+MinX(1);
m2=m(CodeL+1:1:2*CodeL);
for i=1:1:CodeL
y2=y2+m2(i)*2^(i-1);
end
Kpid(s,2)=(MaxX(2)-MinX(2))*y2/1023+MinX(2);
m3=m(2*CodeL+1:1:3*CodeL);
for i=1:1:CodeL
y3=y3+m3(i)*2^(i-1);
end
Kpid(s,3)=(MaxX(3)-MinX(3))*y3/1023+MinX(3);
%****** Step 1 : Evaluate BestJ ******
Kpidi=Kpid(s,:);
[Kpidi,BsJ]=chap5_3f(Kpidi,BsJ);
BsJi(s)=BsJ;
end
[OderJi,IndexJi]=sort(BsJi);
BestJ(kg)=OderJi(1);
BJ=BestJ(kg);
Ji=BsJi+1e-10;
fi=1./Ji;
% Cm=max(Ji);
% fi=Cm-Ji; %Avoiding deviding zero
[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)
Bestfi=Oderfi(Size); % Let Bestfi=max(fi)
BestS=E(Indexfi(Size),:); % Let BestS=E(m), m is the Indexfi belong to max(fi)
bfi(kg)=Bestfi; %每次中所选取最优的解
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
kk=1;
for i=1:1:Size
for j=1:1:fi_S(i) %Select and Reproduce
TempE(kk,:)=E(Indexfi(i),:);
kk=kk+1; %kk is used to reproduce
end
end
%************ Step 3 : Crossover Operation ************
pc=0.60;
n=ceil(20*rand);
for i=1:2:(Size-1)
temp=rand;
if pc>temp %Crossover Condition
for j=n:1:20
TempE(i,j)=E(i+1,j);
TempE(i+1,j)=E(i,j);
end
end
end
TempE(Size,:)=BestS;
E=TempE;
%************ Step 4: Mutation Operation **************
%pm=0.001;
pm=0.001-[1:1:Size]*(0.001)/Size; %Bigger fi, smaller pm
%pm=0.0; %No mutation
%pm=0.1; %Big mutation
for i=1:1:Size
for j=1:1:3*CodeL
temp=rand;
if pm>temp %Mutation Condition
if TempE(i,j)==0
TempE(i,j)=1;
else
TempE(i,j)=0;
end
end
end
end
%Guarantee TempE(Size,:) belong to the best individual
TempE(Size,:)=BestS;
E=TempE;
%*******************************************************
end
Bestfi
BestS
Kpidi
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;
程序运行结果:
图12代价函数J的变化过程
图13二进制遗传算法优化PID阶跃响应