|
23-基于BFO的临界水深优化和PID优化、改进的BFO算法、PSO算法优化的BFO算法优化的PID参数整定
百度网盘链接:https://pan.baidu.com/s/12Zs78YjBZGa_37u70dzY9w 提取码:o4yh
具体链接在halcom.cn论坛,联系人QQ:3283892722
该论坛是一个学习交流平台,我会逐一的和大家分享学习。
欢迎大家录制视频,并提交给我,我来设置视频,你可在论坛进行打赏分享。
视频专用播放器:http://halcom.cn/forum.php?mod=viewthread&tid=258&extra=page%3D1
代码如下:
- function fitness = PID_Fun(Kpid)
- ts=0.001;
- sys = tf( [1.6], [1,1.5,1.6] );
- dsys = c2d(sys, ts, 'z');
- [num,den] = tfdata( dsys, 'v' );
- u_1 = 0; u_2=0;
- y_1 = 0; y_2=0;
- error_1 = 0;
- x = [0,0,0]';
- for k=1:300
- timef(k) = k*ts;
- r(k) = 1;
- u(k) = Kpid(1)*x(1)+Kpid(2)*x(2)+Kpid(3)*x(3);
- yout(k) = -den(2)*y_1 -den(3)*y_2 + num(2)*u_1 + num(3)*u_2;
- error(k) = r(k) - yout(k);
- % PID参数返回值
- u_2 = u_1;
- u_1 = u(k);
- y_2 = y_1;
- y_1 = yout(k);
-
- x(1) = error(k); % Kp
- x(2) = x(2) + error(k)*ts; % KI
- x(3) = (error(k)-error_1)/ts; % KD
-
- error_1 = error(k);
- end
- fitness = sum( abs(error) ) + 300*min( abs( error ) );
复制代码 主函数如下:- clc,clear,close all
- warning off
- format longG
- % BFO优化算法 参数
- sizepop = 10; % 种群数量
- Nc = 10; % 趋化次数
- Ns = 2; % 游动次数
- Nre = 2; % 复制次数
- Ned = 2; % 驱散(迁移)次数
- Sr = ceil( sizepop/2 ); % 复制(分裂)次数
- Ped = 0.25; % 细菌驱散(迁移)概率
- wmax = 0.9;
- wmin = 0.1;
- %% PID参数优化问题
- nvar = 3; % 3个未知量
- popmin = -5; popmax = 15; % x
- Cmin = -1; % 最小步长
- Cmax = 1; % 最大步长
- C(:,:) = 0.1*ones(sizepop,nvar); % 翻转选定方向后,单个细菌前进的步长
- Vmin = -1;
- Vmax = 1;
- c1 = 1.49;
- c2 = 1.49;
- % 目标函数
- fun = @(x)PID_Fun(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); % 步长
- V(i,:) = Vmin + (Vmax-Vmin).*rand(1,nvar); % 速度
- end
- %% 记录一组最优值
- [bestfitness,bestindex]=min(fitness);
- zbest=pop(bestindex,:); % 全局最佳
- fitnesszbest=bestfitness; % 全局最佳适应度值
- gbest = pop;
- fitnessgbest = fitness;
- NcSizepop = 0; % 记录最优适应度值(函数值)
- %% 迭代寻优
- for i = 1:Ned % 驱散(迁移)次数
- for k = 1:Nre % 复制次数
-
- for m = 1:Nc % 趋化次数
- for j=1:sizepop % 种群
- favg = mean(fitness);
- fmin = min(fitness);
- if(fitness(j)<=favg)
- w=wmin+(wmax-wmin)*(fitness(j)-fmin)./(favg-fmin+eps);
- else
- w = wmax;
- end
-
- % 翻转
- V(j,:) = w*V(j,:)+c1*rand*(gbest(j,:)-pop(j,:))+c2*rand*(zbest-pop(j,:));
- for ij=1:nvar
- if V(j,ij)>Vmax
- V(j,ij)=Vmax;
- end
- if V(j,ij)<Vmin
- V(j,ij)=Vmin;
- end
- end
- pop(j,:) = pop(j,:) + w*V(j,:);
-
- % x
- for ij=1:nvar
- if pop(j,ij)>popmax
- pop(j,ij)=popmax;
- end
- if pop(j,ij)<popmin
- pop(j,ij)=popmin;
- end
- end
-
- % 更新当前适应度值
- fitness(j) = fun(pop(j,:));
- % 游动
- if(fitness(j)<fitnessgbest(j))
- gbest(j,:) = pop(j,:);
- fitnessgbest(j) = fitness(j);
- end
- if(fitness(j)<bestfitness)
- zbest = pop(j,:);
- bestfitness = fitness(j);
- end
-
- end % sizepop 种群数量
-
- % 记录最优适应度值
- NcSizepop = NcSizepop+1;
- fitness_iter(NcSizepop) = bestfitness;
- end % Nc 趋化次数
-
- % 复制操作
- [maxF,index] = 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))<bestfitness
- bestfitness = 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)<bestfitness
- bestfitness = fitness(j);
- zbest = pop(j,:);
- end
- end
- end
-
- end % Ned 驱散(迁移)次数
- %% 显示
- disp('最优解')
- disp(['最优解: ', num2str( zbest )])
- fprintf('\n')
- disp(['最优解对应的目标函数值: ', num2str( bestfitness )])
- %% 画图适应度
- figure('color',[1,1,1])
- plot(fitness_iter,'ro-','linewidth',2)
- % loglog(fitness_iter,'ro-','linewidth',2)
- axis tight
- grid on
- %% 响应曲线
- [rint_BFO, yout_BFO] = PID_Fun_response(zbest);
- figure;hold on;
- plot(rint_BFO,'r-','linewidth',2);
- plot(yout_BFO,'b.-','linewidth',2);
- legend('阶跃控制信号','BFO算法优化的PID控制')
- axis([0,300,0,1.2])
- hold off
复制代码
参考文献:
【1】粒子群优化在临界水深计算中的应用
【2】基于人群搜索算法的PID控制器参数优化
|
|