% RDPSO code
% This code is written by Wael T. El Sayed, last update was on 12
% March 2018
% %-------------------------------------------------------------------------------------
% Please, If you will use this code, then cite the following papers:
%--------------
% 1- Paper no.1
%--------------
% Wael T. Elsayed, Yasser G. Hegazy, Mohamed S. El-bages, and Fahmy M. Bendary
% Improved Random Drift Particle Swarm Optimization With Self-Adaptive
% Mechanism for Solving the Power Economic Dispatch Problem,
% IEEE Transactions on Industrial Informatics, vol. 13, no. 3, p. 1017 - 1026, 2017.
%-------------
% 2- Paper no.2
%-------------
% J. Sun, V. Palade, X.-J. Wu, W. Fang, and Z. Wang, Solving the power
% economic dispatch problem with generator constraints by random drift
% particle swarm optimization, IEEE Transactions on Industrial Informatics,
% vol. 10, no. 1, pp. 222232, 2014.
clc
clear all
% initialize the limits here.
Lb = [-100 -100 -100 -100 -100]; %lower limits.
Ub = [100 100 100 100 100]; %upper limits.
% initialize the popoulation size.
n = 10; % Size of the population.
ndim = 5; % Dimension of the problem.
%initialize the algorithm control parameters.
NofRuns=2; % NofRuns is the required number of runs.
Nofiter = 500; % Maximum number of iterations.
% The range of thermal coefficient parameter see the paper no. 2 above page 226
% second column
alphamax=0.9;
alphamin=0.3;
% Acceleration coefficients
c1=2;
c2=2;
% Initialization to the results matrix.
% results=[global cost per run,x1,x2,...,xn, time per run]
results=zeros(NofRuns,ndim+2);
jn=0; % Initialization to the loop variable for the runs.
while jn<NofRuns %loop for the number of runs
jn=jn+1;
tic;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Initialize the population within the limits for the current run.
pop=zeros(n,ndim);
for i=1:ndim
pop(:,i)=Lb(i)+(Ub(i)-Lb(i))*rand(n,1);
end
% Evaluate the total population at the first iteration.
cost=Sphere(pop');
%Initialize the local best equal to the initial population.
localsolution = pop; % location of the local best.
localcost = cost; % cost of local best.
% Initialize the global solution.
[globalcost,indx] = min(cost);
globalsolution=pop(indx,:);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Start iterations
iter = 0; %Initialization to the loop for the iterations
while iter < Nofiter
iter = iter + 1;
alpha=alphamax-((alphamax-alphamin)/Nofiter)*iter;
r1 = rand(n,ndim); % random numbers
r2 = rand(n,ndim); % random numbers
% The following to evaluate equation (15) in paper no. 1.
phi= c1*r1./(c1*r1+c2*r2);
% The following to evaluate equation (17) in paper no. 1.
Cmean=sum(localsolution,1);
Cmeant=(1/n)*repmat(Cmean,n,1);
% The following to evaluate equation (14) in paper no. 1.
localfocus=(phi.*localsolution)+(1-phi).*(repmat(globalsolution,n,1));
% Update the position and the velocity
% The following to evaluate equation (19) in paper no. 1.
vel = alpha*abs(Cmeant-pop).*normrnd(0,1,n,ndim)+1.5*(localfocus-pop);
% The following to evaluate equation (13) in paper no. 1.
pop = pop + vel;
% Evaluate the total population at the current iteration.
cost=Sphere(pop');
% The following uses equations (10) and (11) in paper no. 1 to update the local or
% personal best positions for the particles and the global best
% position.
bettercost = cost < localcost;
localcost = localcost.*not(bettercost) + cost.*bettercost;
localsolution(bettercost,:) = pop(bettercost,:);
% Update the global solution per each iteration
[temp, t] = min(localcost);
if temp<globalcost
globalsolution=pop(t,:); indx=t; globalcost=temp;
end
end %End of the iterations loop
% Handeling the final results
% The results will be available in the workspace in the matrix results,
% where results=[global cost per run,x1,x2,...,xn, time per run]
time=toc;
if NofRuns==1
results (1,1)=globalcost;
results (1,2:(ndim+1))=globalsolution;
results (1,(ndim+2))=time;
else
results (jn,1)=globalcost;
results (jn,2:(ndim+1))=globalsolution;
results (jn,(ndim+2))=time;
end
toc
jn
end % End of no of runs loop
评论0