function f=standard_pso()
%######################
%function initialization
%######################
global c
global weight
global func_num
global fes_num
global run_num
global particles_num
global dims_num
global localratio;

global initial_flag;
initial_flag=0;
no_bound=0;%compute FES when particle run out the space bound

if initial_flag==0
    if     func_num==1  Max_x_L=-100;Max_x_R=100; %[-100,100]
    elseif func_num==2  Max_x_L=-100;Max_x_R=100; %[-100,100]
    elseif func_num==3  Max_x_L=-100;Max_x_R=100; %[-100,100]
    elseif func_num==4  Max_x_L=-100;Max_x_R=100; %[-100,100]
    elseif func_num==5  Max_x_L=-100;Max_x_R=100; %[no bound],initial[-100,100];
    elseif func_num==6  Max_x_L=-100;Max_x_R=100; %[-100,100]
    elseif func_num==7  Max_x_L=0;Max_x_R=600;no_bound=1; %[-600,600];initialization:[0,600] 
    elseif func_num==8  Max_x_L=-32;Max_x_R=32; %[-32,32]
    elseif func_num==9  Max_x_L=-5;Max_x_R=5; %[-5,5]
    elseif func_num==10 Max_x_L=-5;Max_x_R=5; %[-5,5]
    elseif func_num==11 Max_x_L=-0.5;Max_x_R=0.5; %[-0.5,0.5]
    elseif func_num==12 Max_x_L=-pi;Max_x_R=pi; %[-pi,pi]
    elseif func_num==13 Max_x_L=-3;Max_x_R=1; %[-3,1] 
    elseif func_num==14 Max_x_L=-100;Max_x_R=100; %[-100,100]
    elseif func_num==15 Max_x_L=-5;Max_x_R=5; %[-5,5]
    elseif func_num==16 Max_x_L=-5;Max_x_R=5;  %[-5,5]
    elseif func_num==17 Max_x_L=-5;Max_x_R=5;  %[-5,5]        
    elseif func_num==18 Max_x_L=-5;Max_x_R=5;  %[-5,5]    
    elseif func_num==19 Max_x_L=-5;Max_x_R=5;  %[-5,5]    
    elseif func_num==20 Max_x_L=-5;Max_x_R=5;  %[-5,5]  
    elseif func_num==21 MMax_x_L=-5;Max_x_R=5;  %[-5,5]    
    elseif func_num==22 Max_x_L=-5;Max_x_R=5;  %[-5,5]  
    elseif func_num==23 Max_x_L=-5;Max_x_R=5;  %[-5,5]   
    elseif func_num==24 Max_x_L=-5;Max_x_R=5;  %[-5,5]  
    elseif func_num==25 Max_x_L=2;Max_x_R=5;no_bound=1; %[-5,5];initialization:[2,5]    
    end    
end

%######################
%PSO initialization
%######################
%c = 1.496172;
%weight=0.72984;

%######################
%PSO algorithm
%######################
for count_run = 1:run_num   %each run
    %######################
    %particle initialization
    %######################  
    %count_run
    for count_particle = 1:particles_num
      for count_dims = 1:dims_num
            particle_position(count_particle,count_dims) = (Max_x_R-Max_x_L)*rand+Max_x_L;
            if rand>0.5  %set the initial position of each particle
                particle_position(count_particle,count_dims) = -particle_position(count_particle,count_dims);
            end
            particle_velocity(count_particle,count_dims) = rand*Max_x_R;
            if rand>0.5  %set the velocity of each particle
                particle_velocity(count_particle,count_dims) = -particle_velocity(count_particle,count_dims);
            end
            p_best_position(count_particle,count_dims) = particle_position(count_particle,count_dims);
            g_best_position(count_particle,count_dims) = particle_position(count_particle,count_dims);
      end
      p_best_val(count_particle)=Inf;%set the personal best value of each particle as infinity
      g_best_val(count_particle)=Inf;%set the global best value of each particle as infinity
    end

    %######################
    %PSO evolving
    %######################
    out_bound = 0;
    for count_fes = 1:fes_num        
       % count_run
      %  count_fes  %###################### %###################### %###################### %######################print
                
        %######################
        %update the value and position of pbest and gbest
        %######################
        for count_particle = 1:particles_num        
            %######################
            %fitness evaluation,no fes when out of bound
            %######################
            
            if ( (no_bound == 1) || (out_bound == 0) ) 
                x=particle_position(count_particle,:);
                current_fitness(count_particle)=benchmark_func(x,func_num);     
            end
            
           
           
            %######################
            %update the value and position of pbest and gbest
            %######################            
            if current_fitness(count_particle) < p_best_val(count_particle)
                p_best_val(count_particle) = current_fitness(count_particle);
                p_best_position(count_particle,:) = particle_position(count_particle,:);
                
                if count_particle == 1
                    if p_best_val(count_particle) < p_best_val(particles_num)
                        g_best_val(particles_num) = p_best_val(count_particle);
                        g_best_position(particles_num,:) =  p_best_position(count_particle,:);
                    elseif p_best_val(count_particle) < p_best_val(count_particle+1)               
                        g_best_val(count_particle+1) = p_best_val(count_particle);
                        g_best_position(count_particle+1,:) =  p_best_position(count_particle,:);
                    end
                elseif count_particle == particles_num
                    if p_best_val(count_particle) < p_best_val(1)
                        g_best_val(1) = p_best_val(count_particle);
                        g_best_position(1,:) =  p_best_position(count_particle,:);
                    elseif p_best_val(count_particle) < p_best_val(count_particle-1)               
                        g_best_val(count_particle-1) = p_best_val(count_particle);
                        g_best_position(count_particle-1,:) =  p_best_position(count_particle,:);
                    end
                else
                    if p_best_val(count_particle) < p_best_val(count_particle-1)
                        g_best_val(count_particle-1) = p_best_val(count_particle);
                        g_best_position(count_particle-1,:) =  p_best_position(count_particle,:);
                    elseif p_best_val(count_particle) < p_best_val(count_particle+1)               
                        g_best_val(count_particle+1) = p_best_val(count_particle);
                        g_best_position(count_particle+1,:) =  p_best_position(count_particle,:);
                    end
                end
            end 
 %           pbestposition(count_fes,count_particle,:)=p_best_position(count_particle,:);
 %           gbestposition(count_fes,count_particle,:)=g_best_position(count_particle,:);
        end
        
    %    best_swarm(count_fes)=min(g_best_val);

        
       % best_swarm(count_fes)%#####################################################################################################################################print
 
        %######################
        %update the velocity and position of each particle
        %######################
        for count_particle = 1:particles_num %对每一个粒子
            out_bound = 0;                   %intialize the status of particle position
            for count_dims = 1:dims_num      %对每一维  
                a=(p_best_position(count_particle,count_dims)-particle_position(count_particle,count_dims));%记录每一代调整值的变化
                b=(g_best_position(count_particle,count_dims)-particle_position(count_particle,count_dims));
                
%               a(count_particle,count_dims)=c1*rand*c;                                                     %记录每一代调整值的变化
%               b(count_particle,count_dims)=c1*rand*d;

            
             CV(count_particle,count_dims,count_fes)=particle_velocity(count_particle,count_dims);%save
%            CP(count_particle,count_dims,count_fes)=particle_position(count_particle,count_dims);%当前位置

             DP(count_particle,count_dims,count_fes)=a;%记录每一代的d1, 矩阵5*3
             DG(count_particle,count_dims,count_fes)=b;%记录每一代d2, 矩阵5*3
             
%            BP(count_particle,count_fes)=p_best_val(count_particle);%记录每一代的个体最优位置, 矩阵5*3
%            BG(count_particle,count_fes)=g_best_val(count_particle);%记录每一代的全局最优位置, 矩阵5*3
           
%            W(count_particle,count_dims,count)=weight;%记录每一代惯性权重, 矩阵5*3
            
             particle_velocity(count_particle,count_dims) = weight*particle_velocity(count_particle,count_dims) + c*rand*a + c*rand*b;
             
%            速度更新，限制最大速度
            
%               if particle_velocity(count_particle,count_dims) > Max_v
%                       particle_velocity(count_particle,count_dims) = Max_v;
%                else if particle_velocity(count_particle,count_dims) < (-Max_v)
%                       particle_velocity(count_particle,count_dims) = -Max_v;
%                    end
%               end       
                
                particle_position(count_particle,count_dims) = particle_position(count_particle,count_dims) + particle_velocity(count_particle,count_dims);  
                
                
                if ( (particle_position(count_particle,count_dims) > Max_x_R) || (particle_position(count_particle,count_dims) < Max_x_L) )
                    out_bound = 1;
                end
            end  %for dims
 
%           performance(count)=K(count); %每一代的全局极小值（第二列）
%           A(:,:,count)=a;%记录每一代的a, 矩阵5*3
%           B(:,:,count)=b;%记录每一代的b, 矩阵5*3
   
           
        end  %for particle
end  %for fes_num
%######################
%saving all data
%######################
filename = sprintf('data_local_fun%d_%d.mat',func_num,count_run)
%save(filename,'CV','DP','DG','best_swarm','fes_num','run_num','func_num','-mat');
save(filename,'CV','DP','DG','-mat');
end  %for num_run

%plot(1:max_iterations,K);

%xlabel('iterations');
%ylabel('value');
%title('performance');

%save datafor_rosenbrock_without_V.mat A B DP DG W CV NV CP NP performance max_iterations -mat

%g_best 
%p_best_val(g_best_index) 