基于细菌觅食算法的PID整定
基于细菌觅食算法的PID整定:百度网盘链接:https://pan.baidu.com/s/1khndoz4gj7EI2gabUldaeQ提取码:v8l0
具体链接在halcom.cn论坛,联系人QQ:3283892722
该论坛是一个学习交流平台,我会逐一的和大家分享学习。
欢迎大家录制视频,并提交给我,我来设置视频,你可在论坛进行打赏分享。
视频专用播放器:http://halcom.cn/forum.php?mod=viewthread&tid=258&extra=page%3D1
基于细菌觅食算法的PID整定
clc,clear,close all
warning off
format longG
% BFO优化算法 参数
Nc = 50; % 趋化次数
Ns = 4; % 游动次数
Nre = 4; % 复制次数
Ned = 2; % 驱散(迁移)次数
sizepop = 20; % 种群数量
Sr = ceil( sizepop/2 ); % 复制(分裂)次数
Ped = 0.25; % 细菌驱散(迁移)概率
nvar = 3; % 3个未知量
popmin = ; % x min
popmax = ; % x max
Cmin = [-2,-2,-2]; % 最小步长
Cmax = ; % 最大步长
C(:,:) = 0.001*ones(sizepop,nvar); % 翻转选定方向后,单个细菌前进的步长
%% 适应度函数,也就是我们的目标函数
% fun = @(x)PID_Fun_1(x);
fun = @(x)PID_Fun_2(x);
%% 初始化种群
for i=1:sizepop
pop(i,:) = popmin + (popmax-popmin).*rand(1,nvar); % 初始化个体
fitness(i) = fun(pop(i,:) ); % 适应度值
C(i,:) = Cmin + (Cmax-Cmin).*rand(1,nvar);% 步长
end
%% 记录一组最优值
=min(fitness);% 目标值越小越好
zbest=pop(bestindex,:); % 全局最佳
fitnesszbest=bestfitness; % 全局最佳适应度值
NcSizepop = 0; % 记录最优适应度值(函数值)
%% 迭代寻优
for i = 1:Ned % 驱散(迁移)次数
disp(['当前驱散(迁移)次数 i=', num2str(i), ' , 驱散(迁移)次数 Ned=', num2str(Ned)])
for k = 1:Nre % 复制次数
disp(['复制次数 k=', num2str(k), ' , 复制次数 Nre=', num2str(Nre)])
for m = 1:Nc % 趋化次数
disp(['趋化次数 m=', num2str(m), ' , 趋化次数 Nc=', num2str(Nc)])
for j=1:sizepop % 种群
% 翻转
delta = 2*rand(1,nvar)-0.5;
pop(j,:) = pop(j,:) + C(j,:).*delta./(sqrt( delta*delta' ));
% 取值范围约束
pop(j,:) = lb_ub(pop(j,:), popmin, popmax);
% 更新当前适应度值
fitness(j) = fun(pop(j,:));
% 适应度更新
% 比较 个体间比较
if fitness(j)<fitnesszbest
fitnesszbest = fitness(j);
zbest =pop(j,:);
end
end % sizepop种群数量
% 记录最优适应度值
NcSizepop = NcSizepop+1;
fitness_iter(NcSizepop) = fitnesszbest;
K_p(1,NcSizepop) = zbest(1);
K_i(1,NcSizepop) = zbest(2);
K_d(1,NcSizepop) = zbest(3);
end % Nc 趋化次数
% 复制操作
= sort(fitness,'descend');% 降序排列
for Nre2 = 1:Sr % 将最大适应度值的Sr个种群,进行更新
pop(index(Nre2),:) = popmin + (popmax-popmin).*rand(1,nvar);
fitness(index(Nre2)) = fun(pop(index(Nre2),:));
C(index(Nre2),:) = Cmin + (Cmax-Cmin).*rand(1,nvar);% 步长
% 比较 个体间比较
if fitness(index(Nre2))<fitnesszbest
fitnesszbest = fitness(index(Nre2));
zbest =pop(index(Nre2),:);
end
end
end % Nre复制操作
for j=1:sizepop % 种群
if Ped>rand
pop(j,:) = popmin + (popmax-popmin).*rand(1,nvar);
fitness(j) = fun(pop(j,:));
% 比较 个体间比较
if fitness(j)<fitnesszbest
fitnesszbest = fitness(j);
zbest =pop(j,:);
end
end
end
end % Ned 驱散(迁移)次数
%% 显示最优解
disp('最优解')
disp(zbest)
fprintf('\n')
disp(['最优解Kp=', num2str(zbest(1))])
disp(['最优解Ki=', num2str(zbest(2))])
disp(['最优解Kd=', num2str(zbest(3))])
figure('color',)
plot(fitness_iter,'ro-','linewidth',2)
% loglog(fitness_iter,'ro-','linewidth',2)
axis tight
grid on
figure('color',) % 绘制PID控制器参数变化曲线
plot(K_p,'b-','LineWidth',2); hold on
plot(K_i,'k-.','LineWidth',2)
plot(K_d,'r--','LineWidth',2)
title('Kp、Ki、Kd 优化曲线','fontsize',10);
xlabel('迭代次数','fontsize',10); ylabel('参数值','fontsize',10);
set(gca,'Fontsize',10);
legend('Kp','Ki','Kd',1);
grid on;hold off;
%% 响应曲线
% =PID_Fun_1_response(zbest);
=PID_Fun_2_response(zbest);
figure('color',)
plot(timef, r,'b-.','linewidth',2)
hold on;
plot(timef, yout,'r-o','linewidth',2)
hold off;
legend('输入信号','阶跃响应曲线')
xlabel('time(s)');
ylabel('Amp');PID整定对象:
function BsJ=PID_Fun_2(Kpidi)
ts=0.001;
sys=tf(,,'ioDelay',0.2);
dsys=c2d(sys,ts,'z');
=tfdata(dsys,'v');
u_1=0.0;
y_1=0.0;
x=';
B=0;
error_1=0;
tu=1;
s=0;
P=1000;
for k=1:1:P
timef(k)=k*ts;
r(k)=1;
u(k)=Kpidi(1)*x(1)+Kpidi(2)*x(3)+Kpidi(3)*x(2);
% if u(k)>=10
% u(k)=10;
% end
% if u(k)<=-10
% u(k)=-10;
% end
yout(k)=-den(2)*y_1+num(2)*u_1;
error(k)=r(k)-yout(k);
%-------------------Return of PID parameters----------------
u_1=u(k);
y_1=yout(k);
x(1)=error(k); % CalculatingP
x(2)=(error(k)-error_1)/ts; % CalculatingD
x(3)=x(3)+error(k)*ts; % CalculatingI
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;
% BsJ = abs(error(P));
BsJ = sum(abs(error));
4个对应的整定传递函数:
>> ts=0.001;
sys=tf(,)
sys =
1
-----
s + 1
Continuous-time transfer function.
>> sys=tf(,,'ioDelay',0.2)
sys =
1
exp(-0.2*s) * -----
s + 1
Continuous-time transfer function.
>> sys=tf(,)
sys =
1.6
-------------------
s^2 + 2.584 s + 1.6
Continuous-time transfer function.
>> sys=tf(,,'ioDelay',0.1)
sys =
1.6
exp(-0.1*s) * -------------------
s^2 + 2.584 s + 1.6
Continuous-time transfer function. 不错,很有用! PID_Fun_2_response(zbest)这个函数代码里没有,不是完整版吗
pineapple 发表于 2023-5-26 09:52
PID_Fun_2_response(zbest)这个函数代码里没有,不是完整版吗
function BsJ=PID_Fun_2(Kpidi)
这个函数不是提供了吗?
你修改以下输出即可
页:
[1]