📄 gapid.m
字号:
a_1=subplot(3,1,1);
set(a_1,'XColor','white','YColor','white');
a_2=subplot(3,1,2);
set(a_2,'XColor','white','YColor','white');
pause(0.1);
%do something
%GA(Generic Algorithm) Program to optimize PID Parameters
%clear all;
%close all;
global rin yout timef bestpa bestf
G=maxgen;
if(G<=0)
G=1;
end
Size=popsize;
if(Size<=0)
G=1;
end
CodeL=lchrom;
if(CodeL<=0)
G=1;
end
MinX(1)=minkp;
MaxX(1)=maxkp;
MinX(2)=minkd;
MaxX(2)=maxkd;
MinX(3)=minki;
MaxX(3)=maxki;
E=round(rand(Size,3*CodeL)); %Initial Code!
BsJ=0;
bestpa=[1,0,0];bestf=0;
for kg=1:1:G
time(kg)=kg;
for s=1:1:Size
m=E(s,:);
y1=0;y2=0;y3=0;ym=2^CodeL-1;
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/ym+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/ym+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/ym+MinX(3);
%****** Step 1 : Evaluate BestJ ******
Kpidi=Kpid(s,:);
BsJ=pidobj(Kpidi,BsJ,ts,gs,differ);
BsJi(s)=BsJ;
end
[OderJi,IndexJi]=sort(BsJi);
BestJ(kg)=OderJi(1);
BJ=BestJ(kg);
Ji=BsJi+1e-10;
fi=1./Ji;
[Oderfi,Indexfi]=sort(fi); %Arranging fi small to bigger
Bestfi=Oderfi(Size); % Let Bestfi=max(fi)
if(bestf<Bestfi)
bestf=Bestfi;
bestpa=Kpid(Indexfi(Size),:);
end
BestS=E(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); %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=pcross;
n=ceil(CodeL*rand);
for i=1:2:(Size-1)
temp=rand; %Crossover Condition
if pc>temp
for j=n:1:CodeL
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
pm=pmutation;
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;
%*******************************************************
pause(0.01);
end
BsJ=pidobj(bestpa,BsJ,ts,gs,differ);
a_1=subplot(3,1,1);plot(time,BestJ);
set(a_1,'XColor','white','YColor','white');
xlabel('遗传代数');ylabel('误差变化');
a_2=subplot(3,1,2);plot(timef,rin,'r',timef,yout,'b');
set(a_2,'XColor','white','YColor','white');
xlabel('时间(周期)');ylabel('输入(红),输出(蓝)');
%ok
H=axes('unit','normalized','position',[0,0,1,1],'visible','off');
Kp=bestpa(1);
Kd=bestpa(2);
Ki=bestpa(3);
t_s3 = uicontrol( 'Style','text','Position',[0.05 0.3 0.15 0.04],'String','kp:',...
'BackgroundColor',[0.3 0.3 0.3], 'ForegroundColor','white');
t_s4 = uicontrol( 'Style','text','Position',[0.21 0.3 0.15 0.04],'String',num2str(Kp),...
'BackgroundColor',[0.3 0.3 0.3], 'ForegroundColor','white');
t_s5 = uicontrol( 'Style','text','Position',[0.37 0.3 0.15 0.04],'String','ki:',...
'BackgroundColor',[0.3 0.3 0.3], 'ForegroundColor','white');
t_s6 = uicontrol( 'Style','text','Position',[0.53 0.3 0.15 0.04],'String',num2str(Ki),...
'BackgroundColor',[0.3 0.3 0.3], 'ForegroundColor','white');
t_s7 = uicontrol( 'Style','text','Position',[0.69 0.3 0.15 0.04],'String','kd:',...
'BackgroundColor',[0.3 0.3 0.3], 'ForegroundColor','white');
t_s8 = uicontrol( 'Style','text','Position',[0.85 0.3 0.15 0.04],'String',num2str(Kd),...
'BackgroundColor',[0.3 0.3 0.3], 'ForegroundColor','white');
pidyl=imread('pidyl.jpg');
h=axes('position',[0.3 0.1 0.4 0.2]);
im_pid=image(pidyl);
set(h,'visible','off');
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -