From f56753e953bab1996958b88aab2e87f32063de3c Mon Sep 17 00:00:00 2001 From: TianZhendong <33273211+TianZhendong@users.noreply.github.com> Date: Mon, 8 Apr 2019 15:42:02 +0800 Subject: [PATCH] Create pso.m MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit pso-pid主程序 --- pso.m | 91 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 91 insertions(+) create mode 100644 pso.m diff --git a/pso.m b/pso.m new file mode 100644 index 0000000..8e25639 --- /dev/null +++ b/pso.m @@ -0,0 +1,91 @@ +%% 清空环境 +clear +clc +%% 参数设置 +w = 0.6; % 惯性因子 +c1 = 2; % 加速常数 +c2 = 2; % 加速常数 +%c1 = 2; % 加速常数 +%c2 = 2; % 加速常数 +Dim = 3; % 维数 +SwarmSize = 100; % 粒子群规模 +ObjFun = @PSO_PID; % 待优化函数句柄 +MaxIter = 100; % 最大迭代次数 +MinFit = 0.01; % 最小适应值 +Vmax = 1; +Vmin = -1; +Ub = [20 20 20]; +Lb = [-10 -10 -10]; + +%% 粒子群初始化 + Range = ones(SwarmSize,1)*(Ub-Lb); %产生随机向量 + Swarm = rand(SwarmSize,Dim).*Range + ones(SwarmSize,1)*Lb; % 初始化粒子群位置 + VStep = rand(SwarmSize,Dim); % 初始化速度 维度 + fSwarm = zeros(SwarmSize,1); %适应度值的初始化 :0 +for i=1:SwarmSize + i + fSwarm(i,:) = feval(ObjFun,Swarm(i,:)); % 粒子群的适应值 +end + +%% 个体极值和群体极值 +[bestf, bestindex]=min(fSwarm); +zbest=Swarm(bestindex,:); % 全局最佳 :位置 +gbest=Swarm; % 个体最佳 :个体最佳位置就是初始化位置 +fgbest=fSwarm; % 个体最佳适应值 :初始化适应度值 +fzbest=bestf; % 全局最佳适应值 :初始化中最优值 + +%% 迭代寻优 +iter = 0; +y_fitness = zeros(1,MaxIter); % 预先产生4个空矩阵 + +K_p = zeros(1,MaxIter); +K_i = zeros(1,MaxIter); +K_d = zeros(1,MaxIter); +while( (iter < MaxIter) && (fzbest > MinFit) ) + for j=1:SwarmSize + % 速度更新 + iter,j + VStep(j,:) = w*VStep(j,:) + c1*rand*(gbest(j,:) - Swarm(j,:)) + c2*rand*(zbest - Swarm(j,:)); + if VStep(j,:)>Vmax, VStep(j,:)=Vmax; end + if VStep(j,:)Ub(k), Swarm(j,k)=Ub(k); end + if Swarm(j,k)