function [ fhistory2 fGbest2 ] = dms_pso( )
%UNTITLED2 Summary of this function goes here
% Detailed explanation goes here
% Estimate parameter
% Optimization.
% Written by Xu Xia
% 2013. 6. 5
clc; clear; close all;
%% Predefine some variables for PSO
sz = 30; % swarm size of pso
dim = 30; % dimension of particle's
numregion=10;
X=zeros(sz,dim); % position of particles
V=zeros(sz,dim); % velocity of particles
Pbest = zeros(sz, dim); % individual's best position
fPbest = zeros(sz, 1);
Gbest = zeros(1, dim); % global best position
fGbest = 0;
lBestFunreg=zeros(numregion,1);
lBestSreg=zeros(numregion,dim);
fhistory = [ ]; % the best fitness of history
Xhistory = [ ]; % the best position of history
f = zeros(sz, 1);
fname = 'Non_Rastrigin'; % objective function
%% Initialization for pso variables
xmin=-5.12;
xmax=5.12; % upper and lower limit of particle's position
X = rand(sz, dim);
for i = 1 : dim
X(:, i) = repmat(xmin, sz, 1) + (xmax - xmin)*X(:, i);
end
v_min = -4; v_max = 4;
V = v_min + (v_max - v_min)*rand(sz, dim);
c1= 1.49445; c2 = 1.49445;
w_max = 0.9; w_min = 0.4;
iter_max = 200000; % maximum number of iteration
max_FES=5;
%delay=0.02; %延迟时间
%% find global best , individual best and local best
for s=1:1:sz
tempval = feval(fname,X(s,:));
f(s)=tempval;
end
[fmin, ind] = min(f);
fGbest = fmin;
Gbest = X(ind, :);
fPbest = f;
Pbest = X;
fhistory = [fhistory ; fGbest];
Xhistory = [ Xhistory ; [0,Gbest] ];
%% pso main loop
mask_min = repmat(xmin, sz, dim);
mask_max = repmat(xmax, sz, dim);
%figure;
%plot(X(:,1),X(:,2),'r.');
%title('0','fontname','Times New Roman','Color','b','FontSize',16);
iter = 0;
FES=0;
%分六个子群
for region=1:1:numregion
startreg(region)=floor((region-1)*sz/numregion+1);
endreg(region)=floor(region*sz/numregion);
Sizereg(region)=endreg(region)-startreg(region)+1;
end
%求六个子群的localbest
for region=1:1:numregion
[BestFunreg,IndexBestreg]=min(f(startreg(region):endreg(region)));
%[BestFunreg,IndexBestreg]=min(pBestFun(startreg(region):endreg(region)));
IndexBestreg=IndexBestreg+startreg(region)-1;
%if lBestFunreg(region)>=BestFunreg,
lBestFunreg(region)=BestFunreg;
lBestSreg(region,:)=X(IndexBestreg,:);
%lBestSreg(region,:)=pBestS(IndexBestreg(1),:);
end
lbest=lbesttem(lBestSreg);
while iter<0.9*iter_max
%pause(delay);
%cla;
if mod(FES,max_FES)==0
t=randperm(length(X));
X=X(t,:);
end
%w = w_max - iter*(w_max - w_min)/iter_max;
w=0.729;
V = w*V + c1*rand(sz, dim).*(Pbest - X) + c2*rand(sz, dim).*(lbest - X);
chrows1 = V > v_max;
V(find(chrows1)) = v_max;
chrows2 = V < v_min;
V(find(chrows2)) = v_min;
X = X +V;
% check if X is out range of xrange
min_throw = X <= mask_min;
min_keep = X > mask_min;
max_throw = X >= mask_max;
max_keep = X < mask_max;
X = ( min_throw.*mask_min ) + ( min_keep.*X );
X = ( max_throw.*mask_max ) + ( max_keep.*X );
% calculate fitness
for s=1:1:sz
tempval = feval(fname,X(s,:));
f(s)=tempval;
end
%calculate pbest
chrows = f < fPbest;
fPbest(find(chrows)) = f(find(chrows));
Pbest(find(chrows), :) = X(find(chrows), :);
%calculate gbest
[fmin, ind] = min(fPbest);
if fmin < fGbest
fGbest = fmin;
Gbest = Pbest(ind, :);
end
fhistory = [fhistory; fGbest];
Xhistory = [Xhistory; [iter,Gbest]];
%plot(X(:,1),X(:,2),'r.');
%title(iter,'fontname','Times New Roman','Color','b','FontSize',16);
for region=1:1:numregion
[BestFunreg,IndexBestreg]=min(f(startreg(region):endreg(region)));
%[BestFunreg,IndexBestreg]=min(pBestFun(startreg(region):endreg(region)));
IndexBestreg=IndexBestreg+startreg(region)-1;
%if lBestFunreg(region)>=BestFunreg,
lBestFunreg(region)=BestFunreg;
lBestSreg(region,:)=X(IndexBestreg,:);
%lBestSreg(region,:)=pBestS(IndexBestreg(1),:);
end
lbest=lbesttem(lBestSreg);
iter = iter +1;
FES=FES+1;
end
while iter<iter_max
%pause(delay);
%cla;
%w = w_max - iter*(w_max - w_min)/iter_max;
w = 0.2;
V = w*V+ c1*rand(sz, dim).*(Pbest - X) + c2*rand(sz, dim).*(repmat(Gbest, sz, 1) - X);
% check if V is out range of [v_min v_max]
chrows1 = V > v_max;
V(find(chrows1)) = v_max;
chrows2 = V < v_min;
V(find(chrows2)) = v_min;
X = X +V;
% check if X is out range of xrange
min_throw = X <= mask_min;
min_keep = X > mask_min;
max_throw = X >= mask_max;
max_keep = X < mask_max;
X = ( min_throw.*mask_min ) + ( min_keep.*X );
X = ( max_throw.*mask_max ) + ( max_keep.*X );
for s=1:1:sz
tempval = feval(fname,X(s,:));
f(s)=tempval;
end
% update individual's best
chrows = f < fPbest;
fPbest(find(chrows)) = f(find(chrows));
Pbest(find(chrows), :) = X(find(chrows), :);
% update global best
[fmin, ind] = min(fPbest);
if fmin < fGbest
fGbest = fmin;
Gbest = Pbest(ind, :);
end
%plot(X(:,1),X(:,2),'r.');
%title(iter,'fontname','Times New Roman','Color','b','FontSize',16);
fhistory = [fhistory; fGbest];
Xhistory = [ Xhistory; [iter,Gbest]];
iter = iter +1;
end
% End of loop
fhistory2 = fhistory;
fGbest2 = fGbest;
%% output the fhistory result
%figure;
%plot(fhistory);
%title('最优个体适应度','fontsize',12);
%xlabel('进化代数','fontsize',12);
%ylabel('适应度','fontsize',12);
end
评论0