马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有账号?我要加入
x
%BP based PID
function BP_PID=BP_PID()
%本仿真程序是基于BP神经网络整定的PID控制。跟踪对象是正弦函数。采用的学习算法是带惯性项的BP算法。
%利用BP算法的自学习,调整PID的三个控制参数,BP的输入为四个,输出为PID参数
clc
clear all
close all
%%%%%%%%%%%%%%%%%%%%%%%%训练样本%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%P=-2:0.1:8; T=1+sin(P*pi/2);
%P=[1 2;-1 1;1 3];
%T=[1 1;1 1];
%%%%%%%%%%%%%%%%%%%%%%%BP算法参数%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Lr=0.28; %学习效率
max_epoch=6000; %最大迭代次数
goal=0.01; %期望目标值
alfa=0.04; %惯性系数
Layer_n=5; %隐含层数目
output_n=3; %输出层数目
input_n=4; %输入层数目
%%%%%%%%%%%%%%%%%%%%%%权值初始值%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%[input_n,sample]=size(P);[output_n]=size(T,1); %初始化隐含层和输出层的权重系数
%weight1=0.5*rand(Layer_n,input_n)-1;weight2=rand(output_n,Layer_n); %为防止等量调整,权重初始值为随机
%bias1=rand(Layer_n,1);bias2=rand(size(output_n,1),1);
% 初始值的影响非常大,正常情况最好先实现离线学习初始值,或者预测好初始值的情况
weight1=[-0.2846 0.2193 -0.5097 -1.0668;
-0.7484 -0.1210 -0.4708 0.0988;
-0.7176 0.8297 -1.6000 0.2049;
-0.0858 0.1925 -0.6346 0.0347;
0.4358 0.2369 -0.4564 -0.1324];
weight2=[1.0438 0.5478 0.8682 0.1446 0.1537;
0.1716 0.5811 1.1214 0.5067 0.7370;
1.0063 0.7428 1.0534 0.7824 0.6494];
bias1=0.5*zeros(size(weight1,1),1);bias2=0.5*zeros(size(weight2,1),1);
%%%%%%%%%%%%%%%%%%%%%%循环预先值%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
E0=0.5;ts=0.001;y_1=0;er_1=0 ;er_2=0;U_1=0;dU_1=0;
weight2_1=0;weight2_2=0;weight1_1=0 ;weight1_2=0;
%%%%%%%%%%%%%%%%%%%%%%%主仿真程序%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
for k=1:1:max_epoch
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%控制模型%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
time=k*ts; rin=sin(2*pi*time); %跟踪信号
a=1.2*(1-0.8*exp(-0.1*k)); yout=a*y_1/(1+y_1^2)+U_1; %模型
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%控制规律计算%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
er=rin-yout; %误差
EPID=[er-er_1;er;er-2*er_1+er_2]; %给增量式PID控制器的误差信号
input=[rin;yout;er;1]; %BP神经网络的输入 有点纳闷
[a1,da1]=fun1(weight1*input,bias1); %隐含层神经元计算
[a2,da2]=fun2(weight2*a1,bias2); KPID=a2.'; %输出层神经元计算
dU=KPID*EPID; U=U_1+dU; %控制规律
%%%%%%%%%%%%%%%%%%%%%%%%%本控制规律的核心——学习算法BP带惯性%%%%%%%%%%%%%%%%%%%%%
dyu=sign((yout-y_1)/(dU-dU_1+0.0001)); %模型近似部分————较BP算法是多得一项
Sout=da2.*(EPID*dyu*er); %输出层灵敏度
dweight2=alfa*(weight2_1-weight2_2)+Lr*Sout*a1.'; %输出层权值调整
weight2=weight2+dweight2+alfa*(weight2_1-weight2_2);
S1=a1.*(weight2.'*Sout); %隐含层灵敏度
dweight1=Lr*S1*input.';
weight1=weight1+dweight1+alfa*(weight1_1-weight1_2);
%for l=1:1:Out
% delta3(l)=error(k)*dyu(k)*epid(l)*dK(l);
%end
%for l=1:1:Out
% for i=1:1:H
% d_wo=xite*delta3(l)*Oh(i)+alfa*(wo_1-wo_2);
% end
%end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%循环程序参数更新%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
y_1=yout;er_1=er ;er_2=er_1;U_1=U;dU_1=dU;
weight2_2=weight2_1;weight2_1=weight2;
weight1_2=weight1_1;weight1_1=weight1_2;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%性能记录%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
TT(k)=k; EE(k)=er; T(k)=time; RR(k)=rin; YY(k)=yout;
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%性能比较%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%figure(1)
%plot(TT,EE,'r');grid on;
figure(2)
plot(T,RR,'x',T,YY,'r');grid on;
end
%%%%%%%%%%%%%%%%%%%%%%神经元传递函数%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function [F,dF]=fun1(n,b)
%tansif 函数
if nargin<2;b=0;end
if nargin==2;n=n+b(:,ones(1,size(n,2)));end %阀值等值的按列复制
% F= 2 ./ (1 + exp(-2*n)) - 1;
% df=@(X)4*exp(-2*X)./(exp(-2*X)+1).^2;
% dF=df(n);
%对称的sigmoid函数
F=(exp(n)-exp(-n))./(exp(n)+exp(-n));
df=@(X)1./cosh(n).^2;
dF=df(n);
end
function [F,dF]=fun2(n,b)
%purelin
if nargin<2;b=0;end %阀值等值的按列复制
if nargin==2;n=n+b(:,ones(1,size(n,2)));end
%F=n;
%df=@(X)ones(size(X));
%dF=df(n);
%非负的sigmoid 函数,因为PID的输出值为正
F=0.5*(1+tanh(n));
df=@(n)(2*exp(2.*n))./(exp(2.*n) + 1).^2;
dF=df(n);
end
|