|
蚁群算法ACO优化的控制系统参数逼近算法
参考链接:链接:https://pan.baidu.com/s/1ow0aKl3jcyvZrsFQAzghXA 提取码:ix7u
- clc,clear,close all
- warning off
- feature jit off
- load('U.mat')
- load('Y.mat')
- % zbest_tf = importfile1('zbest.mat');
- % 对Y进行抽样
- Y1 = Y(1:1000:end);
- % ACO 参数
- maxiter = 100; % 迭代次数
- sizepop = 30; % 种群数量
- popmin = [0.1,14,0.1]; % x_min
- popmax = [100,20,100]; % x_max
- Rou = 1.0; % 信息素增量强度
- P0 = 0.01; % 转移概率
- nvar = 3; % nvar个自变量
- % 初始化种群
- for i=1:sizepop
- pop(i,:) = popmin + (popmax-popmin).*rand(1,nvar)*0.5;
- fitness(i) = BJ_Fun4( pop(i,:), Y1 );
- end
- % 记录一组最优值
- [bestfitness,bestindex]=min(fitness);
- zbest=pop(bestindex,:); % 全局最佳
- gbest=pop; % 个体最佳
- fitnessgbest=fitness; % 个体最佳适应度值
- fitnesszbest=bestfitness; % 全局最佳适应度值
- %% 迭代寻优
- for i=1:maxiter
- disp(['当前迭代次数:', num2str(i)])
- 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));
- 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_Fun4(pop(j,:), Y1);
- fitness2 = BJ_Fun4(gbest(j,:), Y1);
- if fitness1<fitness2
- gbest(j,:) = pop(j,:);
- else
- pop(j,:) = gbest(j,:);
- end
-
- fitness(j) = (1-Rou)*fitness(j) + BJ_Fun4(pop(j,:), Y1);
-
- if fitness(j) < fitnesszbest
- fitnesszbest = fitness(j);
- fitnessgbest(j) = BJ_Fun4(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( min(fitness_iter) )])
- disp('传递函数');
- sys = tf( zbest(1), [1, zbest(2), zbest(1)] )
- %% 画图显示
- figure('color',[1,1,1])
- plot(fitness_iter,'ro-','linewidth',2)
- grid on
- xlabel('迭代次数');
- ylabel('适应度函数值');
- %% 响应曲线
- Ygap = 1000;
- [rint_BFO, yout_BFO] = BJ_Fun_response4(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
- %% 响应曲线
- Amp= 5000;
- Ygap = 1;
- [rint_BFO2, yout_BFO2] = BJ_Fun_response5( Y(1:Ygap:end), zbest);
- figure;hold on;
- plot(rint_BFO2.*Amp,'k-','linewidth',2);
- plot(Y(1:Ygap:end),'r-','linewidth',2);
- plot(yout_BFO2*Amp,'b.-','linewidth',2);
- legend('阶跃控制信号','实际采集信号','ACO算法优化的信号逼近')
- axis tight;
- hold off
复制代码 适应度函数如下:- function fitness = BJ_Fun4(xx, Y1)
- ts=1e-2;
- aa=xx(1); bb=xx(2); cc=xx(3);
- % 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' );
- for k=1:length(Y1)
- timef(k) = k*ts;
- r(k) = 1.0;
- % r(k) = Y1(k)./(max(Y1)+eps);
- % r(k) = stepfun(timef(k),0);
- u(k) = Y1(k)./(max(Y1)+eps);
- end
- rout = lsim(sys, r, timef);
- yout = lsim(sys, u, timef);
- error1 = r - rout';
- error2 = u - yout';
- fitness1 = sum( abs(error1) ) + 100*max( abs( error1 ) );
- fitness2 = sum( abs(error2) ) + 0*max( abs( error2 ) );
- fitness = fitness1+fitness2;
复制代码 响应度曲线:
- function [r, yout] = BJ_Fun_response4(xx, Y1)
- ts=1e-0;
- aa=xx(1); bb=xx(2); cc=xx(3);
- % 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' );
- for k=1:length(Y1)
- timef(k) = k*ts;
- % r(k) = 1.0;
- r(k) = Y1(k)./(max(Y1)+eps);
- % r(k) = stepfun(timef(k),0);
- end
- yout = lsim(sys, r, timef);
- % error = r - yout';
- % fitness = sum( abs(error) ) + 300*min( abs( error ) );
复制代码
|
|