%GA(Genetic Algorithm) Program to optimize PID Parameters
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))
for i=2:CodeL
E(i)=bitxor(E(i-1),E(i));
end
for i=2*CodeL+2:3*CodeL
E(i)=bitxor(E(i-1),E(i));
end
BsJ=0;
%***************start running**************
for kg=1:1:G
time(kg)=kg;
%***************************step1:RVALUATE BestJ*********
for i=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);
Kpidi=Kpid(s,:);
[Kpidi,BsJ]=pid_gaf(Kpidi,BsJ);
BsJi(s)=BsJ;
end
[OderJi,IndexJi]=sort(Bsfi);
BestJi=OderJi(1);
BJ=BestJ(kg);
Ji=BsJi+1e-10;
fi=1./Ji;
% Cm=max(Ji);
% fi=Cm-Ji;
[Oderfi,Indexfi]=sort(fi);
%Bestfi=Oderfi(Size);
%BestS=Kpid(Indexfi(Size),:);
max(fi)
Bestfi=Oderfi(Size);
BestS=E(Indexfi(Size),:);
kg
BJ
BestS
%*******************s 2: Select and reproduct operaton*********************
fi_sum=sum(fi);
fi_Size=(Oderfi/fi_sum)*Size;
fi_S=floor(fi_Size);
kk=1;
for i=1:1:Size
for j=1:1:fi_S(i)
TempE(kk,:)=E(Indexfi(i),:);
kk=kk+1;
end
end
%***************s 3:CROSSOVER OPERATION**************************
Pc=0.90;
n=ceil(20*rand);
for i=1:2:(Size-1)
temp=rand;
if Pc>temp
for j=n:1:20
TempE(i,j)=E(i+1,j);
TempE(i+1,j)=E(i,j);
end
end
TempE(Size,:)=BestS;
E=TempE;
%****************s 4:MUTATION OPERATION*******************
Pm=0.033;
Pm=0.001-[1:1:Size]*(0.01)/Size;
Pm=0.0;
Pm=0.1;
for i=1:1:Size
for j=1:1:3*CodeL
temp=rand;
if Pm>temp
if TempE(i,j)==0
TempE(i,j)=1;
else
TempE(i,j)=0
end
end
end
%Guarantee TempE(Size,:) belong to the best individual
TempE(Size,:)=BestS;
Kpid=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('Times(s)');ylabel('rin,yout');
%***********chap5_3f.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***************
u_2=u_1;u_1=u(k);
y_2=y_1;y_1=yout(k);
x(1)=error(k);
x(2)=(error(k)-error_1)/ts;
x(3)=x(3)+error(k)*ts;
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;