Search All of the Math Forum:

Views expressed in these public forums are not endorsed by Drexel University or The Math Forum.

Topic: PSO_program_
Replies: 1   Last Post: Mar 14, 2013 2:50 AM

 Derek Goring Posts: 3,663 Registered: 12/7/04
Re: PSO_program_
Posted: Mar 14, 2013 2:50 AM

On Thursday, March 14, 2013 7:26:13 PM UTC+13, sunilkumar M wrote:
> function [v,f,d] = pso()
>
> %Initialization of the Variables
>
> popsize = 100; %population size
>
> maxitr = 1000; %maximum number of Iterations
>
> iter = 0; %iteration counter
>
> feval = 0; %Function evaluation counter
>
> c1 = 2; c2 = 2; % social parameter
>
>
>
> vmax=240; vmin= 50;%Assigning maximum and minmum values
>
> fmax=0.762; fmin=0.062;
>
> dmax=2.0; dmin=1.5;
>
> %Initialization of swarm and velocity
>
> v=vmin+(vmax-vmin)*rand (0,popsize);%Randomizing the cutting speed
>
> f=fmin+(fmax-fmin)* rand(0,popsize);%Randomizing the feed
>
> d=dmin+(dmax-dmin)*rand(0,popsize);%Randomizing the depth of cut
>
> sr = 0.205.*d +1.92.* f - 0.00097.* v; %Objective function
>
> bestpos=sr;%Initial best position
>
> vel=rand(0,popsize);%calulating Intial velocity
>
> %Evaluate swarm
>
> for i=1:popsize
>
> fv(i)=feval(sr,v(:,i));
>
> feval=feval+1;
>
> end
>
> %Initializing best position matrix and corresponding Function value
>
>
>
> bestpos=v;
>
> fbestpos=fv;
>
>
>
> %finding best particle in initial population
>
>
>
> [fbestpart,g] = min(fv);
>
> lastbpf=fbestpart;
>
>
>
> %swarm evolution loop
>
>
>
> while (iter< maxitr)
>
> iter = iter+1;
>
>
>
> %velocity update
>
>
>
> for i=1:popsize
>
> A(:,i)=bestpos(i,g); %A is an external archive
>
> end
>
> R1=rand(0,popsize);
>
> R2=rand(0,popsize);
>
> vel=c1*R1.*(bestpos-v)+c2*R2.*(A-v);
>
>
>
> %swarm update
>
>
>
> v= v + vel;
>
>
>
> %Evaluate the new swarm
>
>
>
> for i=1:popsize
>
> fv(i)=feval(sr,v(:,i));
>
> feval=feval+1;
>
> end
>
>
>
> %updating the best position for each particle
>
>
>
> changecolumns = fv<fbestpost;
>
> fbestpos=fbestpos.*(changecolumns)+fv.*changecolumns;
>
> bestpos(:,find(changecolumns))=v(:,find(:,changecolumns));
>
>
>
> %updating index g
>
> [fbestpart, g]= min(fbestpos);
>
>
>
> end
>
>
>
> %output Arguments
>
> v1min= v(:,g);
>
> fv1min = fbestpos(g);
>
>
>
> its giving error
>
> ??? In an assignment A(I) = B, the number of elements in B and
>
> I must be the same.
>
>
>
> Error in ==> pso at 21
>
> fv(i)=feval(sr,v(:,i));

This statement defines feval as a scalar:
> feval = 0; %Function evaluation counter
but this statement assumes it is a function:
> fv(i)=feval(sr,v(:,i));
on the other hand, by default, Matlab thinks feval is:
FEVAL(F,x1,...,xn) evaluates the function specified by a function
handle or function name, F, at the given arguments, x1,...,xn.

You need to sort out in your mind what feval is.
It cannot be a scalar AND a function.
And in any case, you should not use feval because it is a Matlab function name.