2020新算法SSA,优化PID控制器参数。 有详细中文注释,可更改传递函数。
部分代码如下:
% fobj = @YourCostFunction 设定适应度函数
% dim = number of your variables 设定维度
% Max_iteration = maximum number of generations 设定最大迭代次数
% SearchAgents_no = number of search agents 种群数量
% lb=[lb1,lb2,...,lbn] where lbn is the lower bound of variable n 变量下边界
% ub=[ub1,ub2,...,ubn] where ubn is the upper bound of variable n 变量上边界
% If all the variables have equal lower bound you can just
% define lb and ub as two single number numbers
% To run SSA: [Best_pos,Best_score,curve]=SSA(pop,Max_iter,lb,ub,dim,fobj)
%__________________________________________
clear all
clc
rng('default')
SearchAgents_no=50; % Number of search agents 种群数量
Max_iteration=100; % Maximum numbef of iterations 设定最大迭代次数
lb = -5; %下边界
ub = 5; %上边界
dim = 3; %维度pid3个参数
S = 1;% 1为单位阶跃响应,其他为正弦输入
fobj = @(X) PID_controller(X,S);%适应度函数
[Best_pos,Best_score,SSA_curve]=SSA(SearchAgents_no,Max_iteration,lb,ub,dim,fobj); %开始优化
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
figure
plot(SSA_curve,'Color','r','linewidth',2)
hold on
title('单位阶跃响应收敛曲线')
xlabel('迭代次数');
ylabel('最优适应度值');
axis tight
grid on
box on
legend('SSA')
[Bsu,rin,yout,error]=PID_controller(Best_pos,1);
figure('color',[1,1,1]),
plot(rin,'r--','Linewidth',2);
hold on;
plot(yout,'b-','Linewidth',2)
legend('rin','SSA-PID')
xlabel('时间');ylabel('PID控制输出量');
grid on;
title('单位阶跃响应')
disp(['PID值为:',num2str(Best_pos)]);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
S = 2;%正弦输入
fobj = @(X) PID_controller(X,S);%适应度函数
[Best_pos,Best_score,SSA_curve]=SSA(SearchAgents_no,Max_iteration,lb,ub,dim,fobj); %开始优化
figure
plot(SSA_curve,'Color','r','linewidth',2)
hold on
title('正弦输入响应收敛曲线')
xlabel('迭代次数');
ylabel('最优适应度值');
axis tight
grid on
box on
legend('SSA')
[Bsu,rin,yout,error]=PID_controller(Best_pos,S);
figure('color',[1,1,1]),
plot(rin,'r--','Linewidth',2);
hold on;
plot(yout,'b-','Linewidth',2)
legend('rin','SSA-PID')
xlabel('时间');ylabel('PID控制输出量');
grid on;
title('正弦输入响应')
disp(['PID值为:',num2str(Best_pos)]);
完整代码:
麻雀搜索算法(SSA)优化PID控制器参数_麻雀搜索算法pid-其它文档类资源-CSDN文库
结果图: