www.gusucode.com > PSO_EagleStr 工具箱matlab源码 > PSO_EagleStr/pso_eagle.m

    
%% Developer: Hamza YAPICI
% ES-PSO algorithm for unconstrained optimization can be used for 
% benchmark functions. 
%Hamza Yap?c? and Nurettin ?etinkaya, 
%?An Improved Particle Swarm Optimization Algorithm Using Eagle Strategy for Power Loss Minimization,? 
%Mathematical Problems in Engineering, vol. 2017, 
%Article ID 1063045, 11 pages, 2017. https://doi.org/10.1155/2017/1063045.
%%
clear
ff = 'Rosen'; % Objective Function

popsize = 120; % Population size
npar = 20; % Dimension 
maxit = 1000; % Maximum number of iterations
c1 = 2; % acceleration coefficient
c2 = 2; % acceleration coefficient 

wmax=0.9; % max weight
wmin=0.4; % min weight


% lower bound (Lb) and upper bound (Ub) 

%Rosenbrock var: 20
%----------------
Lb=-30;
Ub=30;
%----------------
% %Griewank var: 20
% %----------------
% Lb=-600;
% Ub=600;
% %----------------
% %golden-price var: 2
% %----------------
% Lb=-2;
% Ub=2;
% %----------------
% %schwefel var: 30
% %----------------
% Lb=-500;
% Ub=500;
% %-------------------
% % Sphere var: 30
% % ----------------
% Lb=-100;
% Ub=100;
% % ----------------
% %six_hump var: 2
% %----------------
% Lb=-5;
% Ub=5;
% %----------------
% %hartman var: 3
% %----------------
% Lb=0;
% Ub=1;
% %----------------
% %Ackley var: 30
% %----------------
% Lb=-32;
% Ub=32;
% %----------------
% %levy var: 30
% %----------------
% Lb=-10;
% Ub=10;
% %----------------
% % easom var: 2
% % ----------------
% Lb=-100;
% Ub=100;
% % ----------------
% %colville var: 4
% %----------------
% Lb=-10;
% Ub=10;
% %----------------
% %trid 6 var: 6
% %----------------
% Lb=-36;
% Ub=36;
% %----------------
% %trid 10 var: 10
% %----------------
% Lb=-100;
% Ub=100;
% %----------------
% %shekel var: 4
% %----------------
% Lb=0;
% Ub=10;
% %----------------


% Initial swarm and velocities
par=Lb+(Ub-Lb).*rand(popsize,npar); % random population
% minpar=min(par);
% maxpar=max(par);
vel = rand(popsize,npar); % random velocities
% velmax = 0.2 * (maxpar - minpar);
% velmin = -velmax;
% Evaluate initial population
for ik=1:popsize
    cost(ik)=feval(ff,par(ik,:)); % calculates population cost using
% ff
end

minc(1)=min(cost); % minimum cost
meanc(1)=mean(cost); % mean cost
globalmin=minc(1); % Initial global minimum
% Initial local particle
localpar = par; 
localcost = cost; 
% Best particle
[globalcost,indx] = min(cost);
globalpar=par(indx,:);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Start iterations
iter = 0; 
while iter < maxit
    iter = iter + 1;
    p=.2;
    a=rand;
    if p<=a
        
        r1 = rand(popsize,npar); % random numbers
        r2 = rand(popsize,npar); % random numbers

        w=wmax-((wmax-wmin)/maxit)*iter; %inertia weight

        vel=(w*vel + c1 *r1.*(localpar-par) + c2*r2.*(ones(popsize,1)*globalpar-par));    
        par = par + vel; % update of position
        I=par<Lb;
        par(I)=Lb;
        J=par>Ub;
        par(J)=Ub;
        for ik=1:popsize
            c_x(ik)=feval(ff,par(ik,:)); % new cost

        end
    %         Update local pars
        for h=1:popsize
            if c_x(h)<localcost(h)
                localcost(h)=c_x(h);
                localpar(h,:)=par(h,:);
            else
                localcost(h)=localcost(h);
                localpar(h,:)=localpar(h,:);
            end

        end
    end
    % by Xin-She Yang and Suash Deb
    % Levy flight (if step=1, then random walk)
    beta=1.5;
    sig=(gamma(1+beta)*sin(pi*beta/2)/(gamma((1+beta)/2)*beta*2^((beta-1)/2)))^(1/beta);
    for j=1:popsize,
        s(j,:)=localpar(j,:);
        
        u=(randn)*sig;
        v=rand-rand;
        step=u./abs(v).^(1/beta);     
        stepsize=0.1*step.*(s(j,:)-globalpar);
        
        s(j,:)=s(j,:)+stepsize.*(randn);
       
    end
    I=s<Lb;
    s(I)=Lb;
    J=s>Ub;
    s(J)=Ub;
    for ik=1:popsize
        new_fit(ik)=feval(ff,s(ik,:)); % new cost

    end
    for h=1:popsize
        if new_fit(h)<localcost(h)
            localcost(h)=new_fit(h);
            localpar(h,:)=s(h,:);
        else
            localcost(h)=localcost(h);
            localpar(h,:)=localpar(h,:);
        end

    end
%   Update global par  
    [glob, i]=min(localcost);
    globalp=localpar(i,:);
 
    temp = min(localcost);
    if temp<globalcost
        globalcost=temp;
        globalpar=globalp;
    else 
        globalcost=globalcost;
        globalpar=globalpar;
    end

    
    [iter globalpar globalcost]; % print output each iteration
    minc(iter+1)=globalcost; % minimum for this iteration
    globalmin(iter+1)=globalcost; 
    meanc(iter+1)=mean(localcost); 
    disp(['Iteration : ' num2str(iter) ...
        '- Best Cost = ' num2str(minc(iter+1))]);
    
end


ans_1=globalcost
ans_2=globalpar;
meanc=mean(minc)
figure(3)
iters=0:length(minc)-1;
plot(iters,minc);
xlabel('iteration');ylabel('fit_val');
text(0,minc(1),'best');