|
蚁群算法ACO的PID参数整定:
代码参考链接:链接:https://pan.baidu.com/s/15nqkVj9-WuwI-2HQ1gB7pg 提取码:howq- clc,clear,close all
- warning off
- feature jit off
- load('U.mat')
- load('Y.mat')
- % 对Y进行抽样
- Y1 = Y(1:1000:end);
- % ACO 参数
- maxiter = 100; % 迭代次数
- sizepop = 10; % 种群数量
- popmin = [-1,-1,-1, 0.1, 0.1, 0.1 ]; % x_min
- popmax = [50,50,50, 15.0, 15.0, 15.0 ]; % x_max
- Rou = 0.8; % 信息素增量强度
- P0 = 0.1; % 转移概率
- nvar = 6; % nvar个自变量
- % 初始化种群
- for i=1:sizepop
- pop(i,:) = popmin + (popmax-popmin).*rand(1,nvar)*0.5;
- fitness(i) = BJ_Fun2( pop(i,:), Y1 );
- end
- % 记录一组最优值
- [bestfitness,bestindex]=min(fitness);
- zbest=pop(bestindex,:); % 全局最佳
- gbest=pop; % 个体最佳
- fitnessgbest=fitness; % 个体最佳适应度值
- fitnesszbest=bestfitness; % 全局最佳适应度值
- %% 迭代寻优
- for i=1:maxiter
- lamda = 1/i; % 信息素挥发因子
- [bestfit,flag] =min(fitness);
- for j=1:sizepop
- Pt(j) = (fitness(j)-bestfit)./bestfit;
- end
- for j=1:sizepop
- % 转移概率值
- if Pt(j)<P0
- pop(j,:) = pop(j,:) + (2*rand(1, nvar)-1)*lamda/2;
- else
- pop(j,:) = pop(j,:) + (popmax-popmin).*(rand(1, nvar)-0.5);
- end
-
- % 越界处理
- for k=1:nvar
- if pop(j,k)>popmax(k)
- pop(j,k)=popmax(k);
- end
- if pop(j,k)<popmin(k)
- pop(j,k)=popmin(k);
- end
- end
-
- % 适应度值
- fitness1 = BJ_Fun2(pop(j,:), Y1);
- fitness2 = BJ_Fun2(gbest(j,:), Y1);
- if fitness1<fitness2
- gbest(j,:) = pop(j,:);
- else
- pop(j,:) = gbest(j,:);
- end
-
- fitness(j) = (1-Rou)*fitness(j) + BJ_Fun2(pop(j,:), Y1);
-
- if fitness(j) < fitnesszbest
- fitnesszbest = fitness(j);
- fitnessgbest(j) = BJ_Fun2(pop(j,:), Y1);
- end
- end
- [fitness_iter(i),index]= min(fitnessgbest);
- end
- zbest = pop(index,:); % 最优个体
- save zbest.mat zbest
- %% 显示
- disp('最优解')
- disp(['最优解: ', num2str( zbest )])
- fprintf('\n')
- disp(['最优解对应的目标函数值: ', num2str( bestfitness )])
- disp('传递函数');
- sys = tf( zbest(4), [1, zbest(5), zbest(4)] )
- %% 画图显示
- figure('color',[1,1,1])
- plot(fitness_iter,'ro-','linewidth',2)
- grid on
- xlabel('迭代次数');
- ylabel('适应度函数值')
- %% 响应曲线
- Ygap = 100;
- [rint_BFO, yout_BFO] = BJ_Fun_response2(zbest, Y(1:Ygap:end));
- figure;hold on;
- plot(rint_BFO.*max(Y(1:Ygap:end)),'r-','linewidth',2);
- plot(yout_BFO.*max(Y(1:Ygap:end)),'b.-','linewidth',2);
- legend('阶跃控制信号','ACO算法优化的信号逼近')
- axis tight;
- hold off
复制代码 适应度函数如下:
- function fitness = BJ_Fun2(xx, Y1)
- ts=0.001;
- KP=xx(1); KI=xx(2); KD=xx(3);
- aa=xx(4); bb=xx(5); cc=xx(6);
- % sys = tf( [1.6], [1,1.5,1.6] );
- sys = tf( aa, [1, bb, aa] );
- 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:length(Y1)
- timef(k) = k*ts;
- r(k) = Y1(k)./(max(Y1)+eps);
- u(k) = KP*x(1)+KI*x(2)+KD*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 ) );
复制代码 响应度曲线:
- function [r, yout] = BJ_Fun_response2(xx, Y1)
- KP=xx(1); KI=xx(2); KD=xx(3);
- aa=xx(4); bb=xx(5); cc=xx(6);
- ts=0.001;
- % sys = tf( [1.6], [1,1.5,1.6] );
- sys = tf( aa, [1, bb, aa] );
- 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:length(Y1)
- timef(k) = k*ts;
- r(k) = Y1(k)./(max(Y1)+eps);
- u(k) = KP*x(1)+KI*x(2)+KD*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 ) );
复制代码
|
|