Posts Tagged ‘PSO Algorithm matlab code’

PSO Algorithm to Maximize the Function F (X, Y, Z)

This is a matlab project for Engineering student.

*Initialize the values for initial position a, b, c, d, e

**Initialize the next positions decided by the individual swarms as a’, b’, c’
d’ and e’

*** Global decision regarding the next position is computed as follows.
Compute f (a’, b, c, d, e), f (a, b’, c, d, e), f (a, b, c’, d, e), f (a, b, c, d’, e)
and f (a, b, c, d, e’). Find minimum among the computed values. If f (a’,
b, c, d, e) is minimum among all, the global position decided regarding
the next position is a’. Similarly If f (a, b’, c, d, e) is minimum among all,
b’ is decided as the global position regarding the next position to be
shifted and so on. Let the selected global position is represented ad
‘global’

****Next value for a is computed as the linear combination of ‘a’ , (a’-a) and
(global-a) (ie)

• nexta = a+ C1 * RAND * (a’ –a) + C2 * RAND * (global –a )
• nextb = b+ C1 * RAND * (b’ –b) + C2 * RAND * (global –b)
• nextc = c+ C1 * RAND * (c’ –c) + C2 * RAND * (global –c )
• nextd = d+ C1 * RAND * (d’ –d) + C2 * RAND * (global –d )
• nexte = e+ C1 * RAND * (e’ –e) + C2 * RAND * (global –e )

*****Change the current value for a, b, c, d and e as nexta, nextb, nextc, nextd
and nexte

******If f (nexta, b, c, d, e) is less than f (a’, b, c, d, e) then update the value for
a’ as nexta, otherwise a’ is not changed.

If f (a, nextb, c, d, e) is less than f (a, b’, c, d, e) then update the value for
b’ as nextb, otherwise b’ is not changed

If f (a, b, nextc, d, e) is less than f (a, b, c’, d, e) then update the value
for c’ as nextc, otherwise c’ is not changed

If f (a, b, c, nextd, e) is less than f (a, b, c, d’, e) then update the value
for d’ as nextd, otherwise d’ is not changed

If f (a, b, c, d, nexte) is less than f (a, b, c, d, e’) then update the value
for e’ as nexte, otherwise e’ is not changed

*******Repeat the steps 3 to 6 for much iteration to reach the final decision.
The values for ‘c1’,’c2’ are decided based on the weightage given to
individual decision and global decision respectively.
Let Δa(t) is the change in the value for updating the value for ‘a’ in t
iteration, then nexta at (t+1)th iteration can be computed using the following
formula. This is considered as the velocity for updating the position of the
swarm in every iteration.

nexta (t+1) =a (t) +Δa(t+1)

where
Δa(t+1) = c1 * rand * (a’ –a ) + c2 * rand * ( gl obal –a ) +w(t)* Δa(t)

Decision taken in the previous iteration is also used for deciding the next
position to be shifted by the swarm. But as iteration increases, the
contribution of the previous decision is decreases and finally reaches zero in
the final iteration.

%%%%%%%%%%%%%%%matlab_code%%%%%%%%%%%%%%%%

**** psogv .m

function [value]=psogv(fun,range,ITER)
%psogv.m
%Particle swarm algorithm for maximizing the function fun with two variables x
%and y.
%Syntax
%[value]=psogv(fun,range,ITER)
%example
%fun=’f1′
%create the function fun.m
%function [res]=fun(x,y)
%res=sin(x)+cos(x);
%range=[-pi pi;-pi pi];
%ITER is the total number of Iteration
error=[];
vel1=[];
vel2=[];
%Intialize the swarm position
swarm=[];
x(1)=rand*range(1,2)+range(1,1);
y(1)=rand*range(2,2)+range(2,1);
x(2)=rand*range(1,2)+range(1,1);
y(2)=rand*range(2,2)+range(2,1);
%Intialize weight
w=1;
c1=2;
c2=2;
%Initialize the velocity
v1=0;%velocity for x
v2=0;%velocity for y
for i=1:1:ITER
[p,q]=min([f1(fun,x(2),y(1)) f1(fun,x(1),y(2))]);
if (q==1)
capture=x(2);
else
capture=y(2);
end

v1=w*v1+c1*rand*(x(2)-x(1))+c2*rand*(capture-x(1));
v2=w*v2+c1*rand*(y(2)-y(1))+c2*rand*(capture-y(1));
vel1=[vel1 v1];
vel2=[vel2 v2];
%updating x(1) and y(1)
x(1)=x(1)+v1;
y(1)=y(1)+v2;
%updating x(2) and y(2)
if((f1(fun,x(2),y(1)))<=(f1(fun,x(1),y(1))))
x(2)=x(2);
else
x(2)=x(1);
end;
if((f1(fun,x(1),y(2)))<=(f1(fun,x(1),y(1))))
y(2)=y(2);
else
y(2)=y(1);
end
error=[error f1(fun,x(2),y(2))];
w=w-w*i/ITER;
swarm=[swarm;x(2) y(2)];
subplot(3,1,3)
plot(error,’-‘)
title(‘Error(vs) Iteration’);
subplot(3,1,1)
plot(swarm(:,1),’-‘)
title(‘x (vs) Iteration’);
subplot(3,1,2)
plot(swarm(:,2),’-‘)
title(‘y (vs) Iteration’);
pause(0.2)
end
value=[x(2);y(2)];

***** f1.m

function [res]=f1(fun,x,y);
s=strcat(fun,'(x,y)’);
res=eval(s);

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

refference: E.S. Gopi . Algorithm Collections for Digital Signal Processing
Applications Using Matlab.

All Rights Reserved
© 2007 Springer