《MATLAB智能算法30个案例》:第17章 基于PSO工具箱的函数寻优算法
1. 前言
《MATLAB智能算法30个案例分析》是2011年7月1日由北京航空航天大学出版社出版的图书,作者是郁磊、史峰、王辉、胡斐。本书案例是各位作者多年从事算法研究的经验总结。书中所有案例均因国内各大MATLAB技术论坛网友的切身需求而精心设计,其中不少案例所涉及的内容和求解方法在国内现已出版的MATLAB书籍中鲜有介绍。《MATLAB智能算法30个案例分析》采用案例形式,以智能算法为主线,讲解了遗传算法、免疫算法、退火算法、粒子群算法、鱼群算法、蚁群算法和神经网络算法等最常用的智能算法的MATLAB实现。
本书共给出30个案例,每个案例都是一个使用智能算法解决问题的具体实例,所有案例均由理论讲解、案例背景、MATLAB程序实现和扩展阅读四个部分组成,并配有完整的原创程序,使读者在掌握算法的同时更能快速提高使用算法求解实际问题的能力。《MATLAB智能算法30个案例分析》可作为本科毕业设计、研究生项目设计、博士低年级课题设计参考书籍,同时对广大科研人员也有很高的参考价值。
《MATLAB智能算法30个案例分析》与《MATLAB 神经网络43个案例分析》一样,都是由北京航空航天大学出版社出版,其中的智能算法应该是属于神经网络兴起之前的智能预测分类算法的热门领域,在数字信号处理,如图像和语音相关方面应用较为广泛。本系列文章结合MATLAB与实际案例进行仿真复现,有不少自己在研究生期间与工作后的学习中有过相关学习应用,这次复现仿真示例进行学习,希望可以温故知新,加强并提升自己在智能算法方面的理解与实践。下面开始进行仿真示例,主要以介绍各章节中源码应用示例为主,本文主要基于MATLAB2015b(32位)平台仿真实现,这是本书第十七章基于PSO工具箱的函数寻优算法实例,话不多说,开始!
2. MATLAB 仿真示例
打开MATLAB,点击“主页”,点击“打开”,找到示例文件
选中main.m,点击“打开”
main.m源码如下:
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%功能:基于粒子群工具箱的函数优化算法示例
%环境:Win7,Matlab2015b
%Modi: C.S
%时间:2022-07-08
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% 清空环境
clc
clear all
close all
tic
%% 参数初始化
x_range=[-50,50]; %参数x变化范围
y_range=[-50,50]; %参数y变化范围
range = [x_range;y_range]; %参数变化范围(组成矩阵)
Max_V = 0.2*(range(:,2)-range(:,1)); %最大速度取变化范围的10%~20%
n=2; %待优化函数的维数,此例子中仅x、y两个自变量,故为2
PSOparams= [25 2000 24 2 2 0.9 0.4 1500 1e-25 250 NaN 0 0];
%% 粒子群寻优
pso_Trelea_vectorized('Rosenbrock',n,Max_V,range,0,PSOparams) %调用PSO核心模块
toc
主要调用函数pso_Trelea_vectorized.m的源码如下:
% pso_Trelea_vectorized.m
% a generic particle swarm optimizer
% to find the minimum or maximum of any
% MISO matlab function
%
% Implements Common, Trelea type 1 and 2, and Clerc's class 1". It will
% also automatically try to track to a changing environment (with varied
% success - BKB 3/18/05)
%
% This vectorized version removes the for loop associated with particle
% number. It also *requires* that the cost function have a single input
% that represents all dimensions of search (i.e., for a function that has 2
% inputs then make a wrapper that passes a matrix of ps x 2 as a single
% variable)
%
% Usage:
% [optOUT]=PSO(functname,D)
% or:
% [optOUT,tr,te]=...
% PSO(functname,D,mv,VarRange,minmax,PSOparams,plotfcn,PSOseedValue)
%
% Inputs:
% functname - string of matlab function to optimize
% D - # of inputs to the function (dimension of problem)
%
% Optional Inputs:
% mv - max particle velocity, either a scalar or a vector of length D
% (this allows each component to have it's own max velocity),
% default = 4, set if not input or input as NaN
%
% VarRange - matrix of ranges for each input variable,
% default -100 to 100, of form:
% [ min1 max1
% min2 max2
% ...
% minD maxD ]
%
% minmax = 0, funct minimized (default)
% = 1, funct maximized
% = 2, funct is targeted to P(12) (minimizes distance to errgoal)
% PSOparams - PSO parameters
% P(1) - Epochs between updating display, default = 100. if 0,
% no display
% P(2) - Maximum number of iterations (epochs) to train, default = 2000.
% P(3) - population size, default = 24
%
% P(4) - acceleration const 1 (local best influence), default = 2
% P(5) - acceleration const 2 (global best influence), default = 2
% P(6) - Initial inertia weight, default = 0.9
% P(7) - Final inertia weight, default = 0.4
% P(8) - Epoch when inertial weight at final value, default = 1500
% P(9)- minimum global error gradient,
% if abs(Gbest(i+1)-Gbest(i)) < gradient over
% certain length of epochs, terminate run, default = 1e-25
% P(10)- epochs before error gradient criterion terminates run,
% default = 150, if the SSE does not change over 250 epochs
% then exit
% P(11)- error goal, if NaN then unconstrained min or max, default=NaN
% P(12)- type flag (which kind of PSO to use)
% 0 = Common PSO w/intertia (default)
% 1,2 = Trelea types 1,2
% 3 = Clerc's Constricted PSO, Type 1"
% P(13)- PSOseed, default=0
% = 0 for initial positions all random
% = 1 for initial particles as user input
%
% plotfcn - optional name of plotting function, default 'goplotpso',
% make your own and put here
%
% PSOseedValue - initial particle position, depends on P(13), must be
% set if P(13) is 1 or 2, not used for P(13)=0, needs to
% be nXm where n<=ps, and m<=D
% If n<ps and/or m<D then remaining values are set random
% on Varrange
% Outputs:
% optOUT - optimal inputs and associated min/max output of function, of form:
% [ bestin1
% bestin2
% ...
% bestinD
% bestOUT ]
%
% Optional Outputs:
% tr - Gbest at every iteration, traces flight of swarm
% te - epochs to train, returned as a vector 1:endepoch
%
% Example: out=pso_Trelea_vectorized('f6',2)
% Brian Birge
% Rev 3.3
% 2/18/06
function [OUT,varargout]=pso_Trelea_vectorized(functname,D,varargin)
rand('state',sum(100*clock));
if nargin < 2
error('Not enough arguments.');
end
% PSO PARAMETERS
if nargin == 2 % only specified functname and D
VRmin=ones(D,1)*-100;
VRmax=ones(D,1)*100;
VR=[VRmin,VRmax];
minmax = 0;
P = [];
mv = 4;
plotfcn='goplotpso';
elseif nargin == 3 % specified functname, D, and mv
VRmin=ones(D,1)*-100;
VRmax=ones(D,1)*100;
VR=[VRmin,VRmax];
minmax = 0;
mv=varargin{
1};
if isnan(mv)
mv=4;
end
P = [];
plotfcn='goplotpso';
elseif nargin == 4 % specified functname, D, mv, Varrange
mv=varargin{
1};
if isnan(mv)
mv=4;
end
VR=varargin{
2};
minmax = 0;
P = [];
plotfcn='goplotpso';
elseif nargin == 5 % Functname, D, mv, Varrange, and minmax
mv=varargin{
1};
if isnan(mv)
mv=4;
end
VR=varargin{
2};
minmax=varargin{
3};
P = [];
plotfcn='goplotpso';
elseif nargin == 6 % Functname, D, mv, Varrange, minmax, and psoparams
mv=varargin{
1};
if isnan(mv)
mv=4;
end
VR=varargin{
2};
minmax=varargin{
3};
P = varargin{
4}; % psoparams
plotfcn='goplotpso';
elseif nargin == 7 % Functname, D, mv, Varrange, minmax, and psoparams, plotfcn
mv=varargin{
1};
if isnan(mv)
mv=4;
end
VR=varargin{
2};
minmax=varargin{
3};
P = varargin{
4}; % psoparams
plotfcn = varargin{
5};
elseif nargin == 8 % Functname, D, mv, Varrange, minmax, and psoparams, plotfcn, PSOseedValue
mv=varargin{
1};
if isnan(mv)
mv=4;
end
VR=varargin{
2};
minmax=varargin{
3};
P = varargin{
4}; % psoparams
plotfcn = varargin{
5};
PSOseedValue = varargin{
6};
else
error('Wrong # of input arguments.');
end
% sets up default pso params
Pdef = [100 2000 24 2 2 0.9 0.4 1500 1e-25 250 NaN 0 0];
Plen = length(P);
P = [P,Pdef(Plen+1:end)];
df = P(1);
me = P(2);
ps = P(3);
ac1 = P(4);
ac2 = P(5);
iw1 = P(6);
iw2 = P(7);
iwe = P(8);
ergrd = P(9);
ergrdep = P(10);
errgoal = P(11);
trelea = P(12);
PSOseed = P(13);
% used with trainpso, for neural net training
if strcmp(functname,'pso_neteval')
net = evalin('caller','net');
Pd = evalin('caller','Pd');
Tl = evalin('caller','Tl');
Ai = evalin('caller','Ai');
Q = evalin('caller','Q');
TS = evalin('caller','TS');
end
% error checking
if ((minmax==2) & isnan(errgoal))
error('minmax= 2, errgoal= NaN: choose an error goal or set minmax to 0 or 1');
end
if ( (PSOseed==1) & ~exist('PSOseedValue') )
error('PSOseed flag set but no PSOseedValue was input');
end
if exist('PSOseedValue')
tmpsz=size(PSOseedValue);
if D < tmpsz(2)
error('PSOseedValue column size must be D or less');
end
if ps < tmpsz(1)
error('PSOseedValue row length must be # of particles or less');
end
end
% set plotting flag
if (P(1))~=0
plotflg=1;
else
plotflg=0;
end
% preallocate variables for speed up
tr = ones(1,me)*NaN;
% take care of setting max velocity and position params here
if length(mv)==1
velmaskmin = -mv*ones(ps,D); % min vel, psXD matrix
velmaskmax = mv*ones(ps,D); % max vel
elseif length(mv)==D
velmaskmin = repmat(forcerow(-mv),ps,1); % min vel
velmaskmax = repmat(forcerow( mv),ps,1); % max vel
else
error('Max vel must be either a scalar or same length as prob dimension D');
end
posmaskmin = repmat(VR(1:D,1)',ps,1); % min pos, psXD matrix
posmaskmax = repmat(VR(1:D,2)',ps,1); % max pos
posmaskmeth = 3; % 3=bounce method (see comments below inside epoch loop)
% PLOTTING
message = sprintf('PSO: %%g/%g iterations, GBest = %%20.20g.\n',me);
% INITIALIZE INITIALIZE INITIALIZE INITIALIZE INITIALIZE INITIALIZE
% initialize population of particles and their velocities at time zero,
% format of pos= (particle#, dimension)
% construct random population positions bounded by VR
pos(1:ps,1:D) = normmat(rand([ps,D]),VR',1);
if PSOseed == 1 % initial positions user input, see comments above
tmpsz = size(PSOseedValue);
pos(1:tmpsz(1),1:tmpsz(2)) = PSOseedValue;
end
% construct initial random velocities between -mv,mv
vel(1:ps,1:D) = normmat(rand([ps,D]),...
[forcecol(-mv),forcecol(mv)]',1);
% initial pbest positions vals
pbest = pos;
% VECTORIZE THIS, or at least vectorize cost funct call
out = feval(functname,pos); % returns column of cost values (1 for each particle)
%---------------------------
pbestval=out; % initially, pbest is same as pos
% assign initial gbest here also (gbest and gbestval)
if minmax==1
% this picks gbestval when we want to maximize the function
[gbestval,idx1] = max(pbestval);
elseif minmax==0
% this works for straight minimization
[gbestval,idx1] = min(pbestval);
elseif minmax==2
% this works when you know target but not direction you need to go
% good for a cost function that returns distance to target that can be either
% negative or positive (direction info)
[temp,idx1] = min((pbestval-ones(size(pbestval))*errgoal).^2);
gbestval = pbestval(idx1);
end
% preallocate a variable to keep track of gbest for all iters
bestpos = zeros(me,D+1)*NaN;
gbest = pbest(idx1,:); % this is gbest position
% used with trainpso, for neural net training
% assign gbest to net at each iteration, these interim assignments
% are for plotting mostly
if strcmp(functname,'pso_neteval')
net=setx(net,gbest);
end
%tr(1) = gbestval; % save for output
bestpos(1,1:D) = gbest;
% this part used for implementing Carlisle and Dozier's APSO idea
% slightly modified, this tracks the global best as the sentry whereas
% their's chooses a different point to act as sentry
% see "Tracking Changing Extremea with Adaptive Particle Swarm Optimizer",
% part of the WAC 2002 Proceedings, June 9-13, http://wacong.com
sentryval = gbestval;
sentry = gbest;
if (trelea == 3)
% calculate Clerc's constriction coefficient chi to use in his form
kappa = 1; % standard val = 1, change for more or less constriction
if ( (ac1+ac2) <=4 )
chi = kappa;
else
psi = ac1 + ac2;
chi_den = abs(2-psi-sqrt(psi^2 - 4*psi));
chi_num = 2*kappa;
chi = chi_num/chi_den;
end
end
% INITIALIZE END INITIALIZE END INITIALIZE END INITIALIZE END
rstflg = 0; % for dynamic environment checking
% start PSO iterative procedures
cnt = 0; % counter used for updating display according to df in the options
cnt2 = 0; % counter used for the stopping subroutine based on error convergence
iwt(1) = iw1;
for i=1:me % start epoch loop (iterations)
out = feval(functname,[pos;gbest]);
outbestval = out(end,:);
out = out(1:end-1,:);
tr(i+1) = gbestval; % keep track of global best val
te = i; % returns epoch number to calling program when done
bestpos(i,1:D+1) = [gbest,gbestval];
%assignin('base','bestpos',bestpos(i,1:D+1));
%------------------------------------------------------------------------
% this section does the plots during iterations
if plotflg==1
if (rem(i,df) == 0 ) | (i==me) | (i==1)
fprintf(message,i,gbestval);
cnt = cnt+1; % count how many times we display (useful for movies)
eval(plotfcn); % defined at top of script
end % end update display every df if statement
end % end plotflg if statement
% check for an error space that changes wrt time/iter
% threshold value that determines dynamic environment
% sees if the value of gbest changes more than some threshold value
% for the same location
chkdyn = 1;
rstflg = 0; % for dynamic environment checking
if chkdyn==1
threshld = 0.05; % percent current best is allowed to change, .05 = 5% etc
letiter = 5; % # of iterations before checking environment, leave at least 3 so PSO has time to converge
outorng = abs( 1- (outbestval/gbestval) ) >= threshld;
samepos = (max( sentry == gbest ));
if (outorng & samepos) & rem(i,letiter)==0
rstflg=1;
% disp('New Environment: reset pbest, gbest, and vel');
%% reset pbest and pbestval if warranted
% outpbestval = feval( functname,[pbest] );
% Poutorng = abs( 1-(outpbestval./pbestval) ) > threshld;
% pbestval = pbestval.*~Poutorng + outpbestval.*Poutorng;
% pbest = pbest.*repmat(~Poutorng,1,D) + pos.*repmat(Poutorng,1,D);
pbest = pos; % reset personal bests to current positions
pbestval = out;
vel = vel*10; % agitate particles a little (or a lot)
% recalculate best vals
if minmax == 1
[gbestval,idx1] = max(pbestval);
elseif minmax==0
[gbestval,idx1] = min(pbestval);
elseif minmax==2 % this section needs work
[temp,idx1] = min((pbestval-ones(size(pbestval))*errgoal).^2);
gbestval = pbestval(idx1);
end
gbest = pbest(idx1,:);
% used with trainpso, for neural net training
% assign gbest to net at each iteration, these interim assignments
% are for plotting mostly
if strcmp(functname,'pso_neteval')
net=setx(net,gbest);
end
end % end if outorng
sentryval = gbestval;
sentry = gbest;
end % end if chkdyn
% find particles where we have new pbest, depending on minmax choice
% then find gbest and gbestval
%[size(out),size(pbestval)]
if rstflg == 0
if minmax == 0
[tempi] = find(pbestval>=out); % new min pbestvals
pbestval(tempi,1) = out(tempi); % update pbestvals
pbest(tempi,:) = pos(tempi,:); % update pbest positions
[iterbestval,idx1] = min(pbestval);
if gbestval >= iterbestval
gbestval = iterbestval;
gbest = pbest(idx1,:);
% used with trainpso, for neural net training
% assign gbest to net at each iteration, these interim assignments
% are for plotting mostly
if strcmp(functname,'pso_neteval')
net=setx(net,gbest);
end
end
elseif minmax == 1
[tempi,dum] = find(pbestval<=out); % new max pbestvals
pbestval(tempi,1) = out(tempi,1); % update pbestvals
pbest(tempi,:) = pos(tempi,:); % update pbest positions
[iterbestval,idx1] = max(pbestval);
if gbestval <= iterbestval
gbestval = iterbestval;
gbest = pbest(idx1,:);
% used with trainpso, for neural net training
% assign gbest to net at each iteration, these interim assignments
% are for plotting mostly
if strcmp(functname,'pso_neteval')
net=setx(net,gbest);
end
end
elseif minmax == 2 % this won't work as it is, fix it later
egones = errgoal*ones(ps,1); % vector of errgoals
sqrerr2 = ((pbestval-egones).^2);
sqrerr1 = ((out-egones).^2);
[tempi,dum] = find(sqerr1 <= sqrerr2); % find particles closest to targ
pbestval(tempi,1) = out(tempi,1); % update pbestvals
pbest(tempi,:) = pos(tempi,:); % update pbest positions
sqrerr = ((pbestval-egones).^2); % need to do this to reflect new pbests
[temp,idx1] = min(sqrerr);
iterbestval = pbestval(idx1);
if (iterbestval-errgoal)^2 <= (gbestval-errgoal)^2
gbestval = iterbestval;
gbest = pbest(idx1,:);
% used with trainpso, for neural net training
% assign gbest to net at each iteration, these interim assignments
% are for plotting mostly
if strcmp(functname,'pso_neteval')
net=setx(net,gbest);
end
end
end
end
% % build a simple predictor 10th order, for gbest trajectory
% if i>500
% for dimcnt=1:D
% pred_coef = polyfit(i-250:i,(bestpos(i-250:i,dimcnt))',20);
% % pred_coef = polyfit(200:i,(bestpos(200:i,dimcnt))',20);
% gbest_pred(i,dimcnt) = polyval(pred_coef,i+1);
% end
% else
% gbest_pred(i,:) = zeros(size(gbest));
% end
%gbest_pred(i,:)=gbest;
%assignin('base','gbest_pred',gbest_pred);
% % convert to non-inertial frame
% gbestoffset = gbest - gbest_pred(i,:);
% gbest = gbest - gbestoffset;
% pos = pos + repmat(gbestoffset,ps,1);
% pbest = pbest + repmat(gbestoffset,ps,1);
%PSOPSOPSOPSOPSOPSOPSOPSOPSOPSOPSOPSOPSOPSOPSOPSOPSOPSOPSOPSOPSOPSOPSOPSOPSO
% get new velocities, positions (this is the heart of the PSO algorithm)
% each epoch get new set of random numbers
rannum1 = rand([ps,D]); % for Trelea and Clerc types
rannum2 = rand([ps,D]);
if trelea == 2
% from Trelea's paper, parameter set 2
vel = 0.729.*vel... % prev vel
+1.494.*rannum1.*(pbest-pos)... % independent
+1.494.*rannum2.*(repmat(gbest,ps,1)-pos); % social
elseif trelea == 1
% from Trelea's paper, parameter set 1
vel = 0.600.*vel... % prev vel
+1.700.*rannum1.*(pbest-pos)... % independent
+1.700.*rannum2.*(repmat(gbest,ps,1)-pos); % social
elseif trelea ==3
% Clerc's Type 1" PSO
vel = chi*(vel... % prev vel
+ac1.*rannum1.*(pbest-pos)... % independent
+ac2.*rannum2.*(repmat(gbest,ps,1)-pos)) ; % social
else
% common PSO algo with inertia wt
% get inertia weight, just a linear funct w.r.t. epoch parameter iwe
if i<=iwe
iwt(i) = ((iw2-iw1)/(iwe-1))*(i-1)+iw1;
else
iwt(i) = iw2;
end
% random number including acceleration constants
ac11 = rannum1.*ac1; % for common PSO w/inertia
ac22 = rannum2.*ac2;
vel = iwt(i).*vel... % prev vel
+ac11.*(pbest-pos)... % independent
+ac22.*(repmat(gbest,ps,1)-pos); % social
end
% limit velocities here using masking
vel = ( (vel <= velmaskmin).*velmaskmin ) + ( (vel > velmaskmin).*vel );
vel = ( (vel >= velmaskmax).*velmaskmax ) + ( (vel < velmaskmax).*vel );
% update new position (PSO algo)
pos = pos + vel;
% position masking, limits positions to desired search space
% method: 0) no position limiting, 1) saturation at limit,
% 2) wraparound at limit , 3) bounce off limit
minposmask_throwaway = pos <= posmaskmin; % these are psXD matrices
minposmask_keep = pos > posmaskmin;
maxposmask_throwaway = pos >= posmaskmax;
maxposmask_keep = pos < posmaskmax;
if posmaskmeth == 1
% this is the saturation method
pos = ( minposmask_throwaway.*posmaskmin ) + ( minposmask_keep.*pos );
pos = ( maxposmask_throwaway.*posmaskmax ) + ( maxposmask_keep.*pos );
elseif posmaskmeth == 2
% this is the wraparound method
pos = ( minposmask_throwaway.*posmaskmax ) + ( minposmask_keep.*pos );
pos = ( maxposmask_throwaway.*posmaskmin ) + ( maxposmask_keep.*pos );
elseif posmaskmeth == 3
% this is the bounce method, particles bounce off the boundaries with -vel
pos = ( minposmask_throwaway.*posmaskmin ) + ( minposmask_keep.*pos );
pos = ( maxposmask_throwaway.*posmaskmax ) + ( maxposmask_keep.*pos );
vel = (vel.*minposmask_keep) + (-vel.*minposmask_throwaway);
vel = (vel.*maxposmask_keep) + (-vel.*maxposmask_throwaway);
else
% no change, this is the original Eberhart, Kennedy method,
% it lets the particles grow beyond bounds if psoparams (P)
% especially Vmax, aren't set correctly, see the literature
end
%PSOPSOPSOPSOPSOPSOPSOPSOPSOPSOPSOPSOPSOPSOPSOPSOPSOPSOPSOPSOPSOPSOPSOPSOPSO
% check for stopping criterion based on speed of convergence to desired
% error
tmp1 = abs(tr(i) - gbestval);
if tmp1 > ergrd
cnt2 = 0;
elseif tmp1 <= ergrd
cnt2 = cnt2+1;
if cnt2 >= ergrdep
if plotflg == 1
fprintf(message,i,gbestval);
disp(' ');
disp(['--> Solution likely, GBest hasn''t changed by at least ',...
num2str(ergrd),' for ',...
num2str(cnt2),' epochs.']);
eval(plotfcn);
end
break
end
end
% this stops if using constrained optimization and goal is reached
if ~isnan(errgoal)
if ((gbestval<=errgoal) & (minmax==0)) | ((gbestval>=errgoal) & (minmax==1))
if plotflg == 1
fprintf(message,i,gbestval);
disp(' ');
disp(['--> Error Goal reached, successful termination!']);
eval(plotfcn);
end
break
end
% this is stopping criterion for constrained from both sides
if minmax == 2
if ((tr(i)<errgoal) & (gbestval>=errgoal)) | ((tr(i)>errgoal) ...
& (gbestval <= errgoal))
if plotflg == 1
fprintf(message,i,gbestval);
disp(' ');
disp(['--> Error Goal reached, successful termination!']);
eval(plotfcn);
end
break
end
end % end if minmax==2
end % end ~isnan if
% % convert back to inertial frame
% pos = pos - repmat(gbestoffset,ps,1);
% pbest = pbest - repmat(gbestoffset,ps,1);
% gbest = gbest + gbestoffset;
end % end epoch loop
%% clear temp outputs
% evalin('base','clear temp_pso_out temp_te temp_tr;');
% output & return
OUT=[gbest';gbestval];
varargout{
1}=[1:te];
varargout{
2}=[tr(find(~isnan(tr)))];
return
添加完毕,点击“运行”,开始仿真,输出仿真结果如下:
PSO: 1/2000 iterations, GBest = 14557.917529347756.
PSO: 25/2000 iterations, GBest = 6.7554589625538748.
PSO: 50/2000 iterations, GBest = 0.66685498154525125.
PSO: 75/2000 iterations, GBest = 0.66685498154525125.
PSO: 100/2000 iterations, GBest = 0.66685498154525125.
PSO: 125/2000 iterations, GBest = 0.027399163570997292.
PSO: 150/2000 iterations, GBest = 0.027399163570997292.
PSO: 175/2000 iterations, GBest = 0.027399163570997292.
PSO: 200/2000 iterations, GBest = 0.027399163570997292.
PSO: 225/2000 iterations, GBest = 0.027399163570997292.
PSO: 250/2000 iterations, GBest = 0.010083332663138598.
PSO: 275/2000 iterations, GBest = 0.0035370934201834356.
PSO: 300/2000 iterations, GBest = 0.0035370934201834356.
PSO: 325/2000 iterations, GBest = 0.0035370934201834356.
PSO: 350/2000 iterations, GBest = 0.0035370934201834356.
PSO: 375/2000 iterations, GBest = 0.0035370934201834356.
PSO: 400/2000 iterations, GBest = 0.0033699926129033778.
PSO: 425/2000 iterations, GBest = 0.00041153511245865034.
PSO: 450/2000 iterations, GBest = 0.0002577662261316969.
PSO: 475/2000 iterations, GBest = 9.5941608597482382e-05.
PSO: 500/2000 iterations, GBest = 8.5695331854417046e-05.
PSO: 525/2000 iterations, GBest = 8.0461121678601007e-05.
PSO: 550/2000 iterations, GBest = 7.9584455704824586e-05.
PSO: 575/2000 iterations, GBest = 1.6448144236641622e-05.
PSO: 600/2000 iterations, GBest = 3.3293473249952457e-06.
PSO: 625/2000 iterations, GBest = 3.3293473249952457e-06.
PSO: 650/2000 iterations, GBest = 2.6696033891367523e-06.
PSO: 675/2000 iterations, GBest = 2.4114068838282737e-06.
PSO: 700/2000 iterations, GBest = 3.1429015507272606e-07.
PSO: 725/2000 iterations, GBest = 1.443458702325107e-07.
PSO: 750/2000 iterations, GBest = 1.5873876557390419e-09.
PSO: 775/2000 iterations, GBest = 1.0871266483927119e-09.
PSO: 800/2000 iterations, GBest = 8.4964376409295975e-10.
PSO: 825/2000 iterations, GBest = 8.4964182001882613e-10.
PSO: 850/2000 iterations, GBest = 1.9113597287809508e-10.
PSO: 875/2000 iterations, GBest = 1.3581377936847206e-10.
PSO: 900/2000 iterations, GBest = 1.2899568298966395e-10.
PSO: 925/2000 iterations, GBest = 1.2409109920535079e-10.
PSO: 950/2000 iterations, GBest = 1.200640329727889e-10.
PSO: 975/2000 iterations, GBest = 1.0428179727992196e-10.
PSO: 1000/2000 iterations, GBest = 5.1433007964655653e-11.
PSO: 1025/2000 iterations, GBest = 3.1911322410110723e-11.
PSO: 1050/2000 iterations, GBest = 1.2343158136282748e-11.
PSO: 1075/2000 iterations, GBest = 4.40854493576204e-12.
PSO: 1100/2000 iterations, GBest = 2.5236425168320615e-13.
PSO: 1125/2000 iterations, GBest = 7.4272349154429918e-14.
PSO: 1150/2000 iterations, GBest = 1.2507238602483046e-14.
PSO: 1175/2000 iterations, GBest = 2.9747213208935302e-15.
PSO: 1200/2000 iterations, GBest = 2.0735246365969343e-16.
PSO: 1225/2000 iterations, GBest = 9.2903589418929135e-17.
PSO: 1250/2000 iterations, GBest = 4.7021406092407005e-17.
PSO: 1275/2000 iterations, GBest = 3.7186708593999176e-17.
PSO: 1300/2000 iterations, GBest = 1.5291800196894919e-17.
PSO: 1325/2000 iterations, GBest = 1.0415115324228361e-17.
PSO: 1350/2000 iterations, GBest = 7.454459907623785e-18.
PSO: 1375/2000 iterations, GBest = 3.2493093464253425e-19.
PSO: 1400/2000 iterations, GBest = 2.1270114159036042e-19.
PSO: 1425/2000 iterations, GBest = 2.1928297659461777e-21.
PSO: 1450/2000 iterations, GBest = 1.326029935770441e-21.
PSO: 1475/2000 iterations, GBest = 5.2734107209206169e-22.
PSO: 1500/2000 iterations, GBest = 2.2001347687242167e-22.
PSO: 1525/2000 iterations, GBest = 9.5000367000432235e-23.
PSO: 1550/2000 iterations, GBest = 1.7332001442112146e-24.
PSO: 1575/2000 iterations, GBest = 6.3756898232011882e-25.
PSO: 1600/2000 iterations, GBest = 4.8495697465004833e-25.
PSO: 1625/2000 iterations, GBest = 3.1242224784076626e-25.
PSO: 1650/2000 iterations, GBest = 2.7862579498357781e-26.
PSO: 1675/2000 iterations, GBest = 2.1299737479033082e-27.
PSO: 1700/2000 iterations, GBest = 6.2523389714587225e-28.
PSO: 1725/2000 iterations, GBest = 3.6595750431268501e-29.
PSO: 1750/2000 iterations, GBest = 1.2325951644078309e-29.
PSO: 1775/2000 iterations, GBest = 2.4158865222393487e-30.
PSO: 1800/2000 iterations, GBest = 2.0830858278492343e-30.
PSO: 1825/2000 iterations, GBest = 2.0830858278492343e-30.
PSO: 1850/2000 iterations, GBest = 4.9303806576313238e-32.
PSO: 1875/2000 iterations, GBest = 1.2325951644078309e-32.
PSO: 1885/2000 iterations, GBest = 1.2325951644078309e-32.
--> Solution likely, GBest hasn't changed by at least 1e-25 for 250 epochs.
ans =
1.0000
1.0000
0.0000
时间已过 7.858385 秒。
3. 小结
粒子群算法具有操作简单、算法搜索高效等优点,该算法对优化函数没有连续可微的要求,通用性较强,对多变量、非线性、不连续及不可微的问题求解有较大的优势。PSO工具箱由美国北卡罗来纳州立大学航空航天与机械系教授Brian Birge开发,该工具箱将PSO算法的核心部分封装起来,提供给用户的为算法的可调参数,用户只需要定义需要优化的函数,并设置好函数自变量的取值范围、每步迭代允许的最大变化量等,即可进行优化。对本章内容感兴趣或者想充分学习了解的,建议去研习书中第十七章节的内容。后期会对其中一些知识点在自己理解的基础上进行补充,欢迎大家一起学习交流。