PSO CODE for more than one Equation
5 views (last 30 days)
Show older comments
Hello! I have done the given problem 198x + 199y + 187z +201w = 200(aproximately equal or equal) Subject to, (x,y,z,w=[0 1].) through PSO(Particle Swarm Optimization)... the whole code for solving one such equation is given below.... next my problem is to solve multiple equation through PSO... Now I have to optimize it for obj_fun having multiple values to calculate. suppose
{
162x + 163y + 161z +164w = 163;
155x + 157y + 157z +154w = 155;
158x + 159y + 157z +156w = 157;
201x + 200y + 203z +201w = 200;
108x + 107y + 107z +109w = 107;....... }
and so on .....suppose hunderds of such equations that i have to solve.. Code given below, is only for one equation... plz help me how to optimize the given code for multiple equations.... so the i could get the best particles for each equation....
Thanks in anticipatiion..........
{%Initialization of PSO parameters
wmax=0.9;
wmin=0.4;
itmax=200; %Maximum iteration number
c1=1.4;
c2=1.4;
for iter=1:itmax
W(iter)=wmax-((wmax-wmin)/itmax)*iter;
end
%**********************************************************
%Initialization of positions of agents
%Initialize Swarm Particles
a=0;
b=5;
N=20;
D=4;
abc(1:4,1)=0;
abc(1:4,2)=1;
lbound=abc(:,1);
ubound=abc(:,2);
for i=1:N
for j=1:D
x(i,j)=rand*(ubound(j)-lbound(j))+lbound(j);
end
end
%Initialization of velocities of agents
%Between -5 , +5, (which can also be started from zero)
m=0;
n=1;
V=m+(n-m)*rand(N,D,1);
%**********************************************************
%Function to be minimized.
for i=1:N;
F(i,1,1)=abs(200-((x(i,1,1)*198) + (x(i,2,1)*199) +(x(i,3,1)*187)+ (x(i,4,1)*201)));
end
%**********************************************************
[C,I]=min(abs(F(:,1,1)));
B(1,1,1)=C;
XX(1,1,1)=I;
gbest(1,1,1)=x(I,1,1);
gbest(1,2,1)=x(I,2,1);
gbest(1,3,1)=x(I,3,1);
gbest(1,4,1)=x(I,4,1);
%********************************************************
%Matrix composed of gbest vector
for p=1:N
for r=1:D
G(p,r,1)=gbest(1,r,1);
end
end
Fbest(1,1,1)=abs(200-((G(1,1,1)*198) + (G(1,2,1)*199) +(G(1,3,1)*187)+ (G(1,4,1)*201)));
for i=1:N;
pbest(i,:,1)=x(i,:,1);
end
V(:,:,2)=W(1)*V(:,:,1)+c1*rand*(pbest(:,:,1)-x(:,:,1))+c2*rand*(G(:,:,1)-x(:,:,1));
x(:,:,2)=x(:,:,1)+V(:,:,2);
Fb(1,1,1)=abs(200-((gbest(1,1,1)*198) + (gbest(1,2,1)*199) +(gbest(1,3,1)*187)+ (gbest(1,4,1)*201)));
%******************************************************
for j=2:itmax-1
% Calculation of new positions
for i=1:N;
F(i,1,j)=abs(200-((x(i,1,j)*198) + (x(i,2,j)*199) +(x(i,3,j)*187)+ (x(i,4,j)*201)));
end
[C,I]=min(abs(F(:,:,j)));
B(1,1,j)=C;
gbest(1,1,j)=x(I,1,j);
gbest(1,2,j)=x(I,2,j);
gbest(1,3,j)=x(I,3,j);
gbest(1,4,j)=x(I,4,j);
Fb(1,1,j)=abs(200-((gbest(1,1,j)*198) + (gbest(1,2,j)*199) +(gbest(1,3,j)*187)+ (gbest(1,4,j)*201)));
[C,I]=min(Fb(1,1,:));
if Fb(1,1,j)<=C
gbest(1,1,j)=gbest(1,1,j);
gbest(1,2,j)=gbest(1,2,j);
gbest(1,3,j)=gbest(1,3,j);
gbest(1,4,j)=gbest(1,4,j);
else
gbest(1,1,j)=gbest(1,1,I);
gbest(1,2,j)=gbest(1,2,I);
gbest(1,3,j)=gbest(1,3,I);
gbest(1,4,j)=gbest(1,4,I);
end
%Matrix composed of gbest vector
for p=1:N
for r=1:D
G(p,r,j)=gbest(1,r,j);
end
end
Fbest(1,1,j)=abs(200-((G(1,1,j)*198) + (G(1,2,j)*199) +(G(1,3,j)*187)+ (G(1,4,j)*201)));
for i=1:N;
[C,I]=min(F(i,1,:));
if F(i,1,j)<=C
pbest(i,:,j)=x(i,:,j);
else
pbest(i,:,j)=x(i,:,I);
end
end
V(:,:,j+1)=W(j)*V(:,:,j)+c1*rand*(pbest(:,:,j)-x(:,:,j))+c2*rand*(G(:,:,j)-x(:,:,j));
x(:,:,j+1)=x(:,:,j)+V(:,:,j+1);
end
4 Comments
Krishna Kumar
on 28 Jun 2011
Get code in FEX, or if you know PSO make a code yourselves and post your difficulties here. You can get some PSO codes over Internet easily.
Accepted Answer
Krishna Kumar
on 28 Jun 2011
Well if you need help in framing the problem, try this: [x y z w] is the parameter vector. Your objective function could be Obj_fn= abs(200-(198x + 199y + 187z +201w)); Use a PSO to minimize it.
5 Comments
Krishna Kumar
on 30 Jun 2011
I dont get you clearly. If you need separate solutions for each equation, you have to run it so many times.
Or if you need a single solution for all the eqns (which i think is not your case), you can do like this-
obj_fn=abs(200-(198x + 199y + 187z +201w));
this way you find the error for each equation.Since the values in all equations are in the same range, you can simply add the errors and keep that as objective function.
There are many other methods too, I think this would suffice.
More Answers (0)
See Also
Categories
Find more on Particle Swarm in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!