www.pudn.com > IEEE33DG.rar > QPSO.asv, change:2015-04-17,size:4726b


function [fxmin,xmin,PBest,fPBest,history]=QPSO(psoOptions) 
 
%Initializations 
if nargin == 0 
    psoOptions = get_psoOptions; 
end 
 
% Initializing variables 
success = 0; % Success Flag 
iter = 0;   % Iterations' counter 
fevals = 0; % Function evaluations' counter 
 
% Using params--- 
% Initialize Swarm and Velocity 
SwarmSize = psoOptions.Vars.SwarmSize; 
Swarm = round(rand(SwarmSize, psoOptions.Vars.Dim).*repmat(psoOptions.Obj.ub-psoOptions.Obj.lb,SwarmSize,1) + repmat(psoOptions.Obj.lb,SwarmSize,1)); 
 
f2eval = psoOptions.Obj.f2eval; %The objective function to optimize. 
 
%Find initial function values. 
 
for i=1:SwarmSize 
    Swarm1=Swarm(i,:); 
    %guanck=kxjpanding(Swarm1);   %调用不可行解判断函数check_kxj判定解的可行性,若为不可行解,guanck=0,给fSwarm赋以较大的数10000 
    %if guanck==1               %若为可行解,则guanck=1,可以用优化函数进行潮流计算 
     fSwarm(i,1) = feval(f2eval,Swarm1); 
    %else 
     %fSwarm(i,1)=10000; 
    %end 
end 
 
% Initializing the Best positions matrix and 
% the corresponding function values 
PBest = Swarm;        %最优种群 
fPBest = fSwarm;      %最优的种群适应值 
 
% Finding best particle in initial population 
[fGBest, g] = min(fSwarm); 
lastbpf = fGBest; 
GBest = Swarm(g,:); %Used to keep track of the Best particle ever 
 
 
while( (success == 0) && (iter <= psoOptions.Vars.Iterations) ) 
    fGBest0=fGBest; 
    iter = iter+1; 
    beta=(1-psoOptions.SParams.w_beta)*(psoOptions.Vars.Iterations-iter)/psoOptions.Vars.Iterations+psoOptions.SParams.w_beta; 
     
    % Generate Random Numbers 
    R1 = rand(SwarmSize, psoOptions.Vars.Dim); 
    R2 = rand(SwarmSize, psoOptions.Vars.Dim)-0.5; 
    %mBest 
    mBest=sum(PBest,1)/SwarmSize; 
    mBest=repmat(mBest,SwarmSize,1); 
     
    LIP=R1.*PBest+(1.-R1).*repmat(GBest,SwarmSize,1); 
    R2=sign(R2); 
    val1=beta.*(mBest-Swarm); 
    val2=-log(R1); 
    %以下是关于十进制分层量子粒子群算法的修改程序段 
    %······································································ 
    Swarm=round(LIP+R2.*val1.*val2);            %将离散值四舍五入成整数 
    % Apply Xmax Operator for x > Xmax 
    Xmax_var=repmat(psoOptions.SParams.Xmax,SwarmSize,1);  %Judgement matrix 
    changeRows = Swarm > Xmax_var; 
    for i=1:psoOptions.Vars.Dim 
        Xrand(:,i)=randint(SwarmSize,1,[1,psoOptions.SParams.Xmax(1,i)]);%产生50X32随机整数矩阵,用于修改越限数,保持所有更新的数在开关矩阵上下限中 
    end                                                                  %为每个粒子产生随机整数,保持了种群的多样性 
    Xmax=Xrand; 
    Swarm(find(changeRows)) = Xmax(find(changeRows));       
    % Apply Xmax Operator for x < -Xmax 
    changeRows = Swarm < 1;                               %开关矩阵的下线是1 
    Swarm(find(changeRows)) = Xmax(find(changeRows));     %越过下限时,则随机产生上下限之间的一个随机的整数 
    %······································································ 
    for i=1:SwarmSize 
        Swarm1=Swarm(i,:); 
        %guanck=kxjpanding(Swarm1); 
           %if guanck==1 
              fSwarm(i,1) = feval(f2eval,Swarm1); 
           %else 
           %fSwarm(i,1)=10000; 
           %end 
     end 
     
    changeRows = fSwarm < fPBest;    %与当前的个体最值比较 
    fPBest(find(changeRows)) = fSwarm(find(changeRows));       %更新最优个体适应值(一列数,列数=种群数) 
    PBest(find(changeRows), :) = Swarm(find(changeRows), :);   %更新最优粒子群 
     
    [fGBest, g] = min(fPBest);   %从最优个体适应值中寻找到全局最优值  
    %Update Best. Only if fitness has improved. 
    if fGBest < lastbpf 
        [fBest, b] = min(fPBest); 
        GBest = PBest(b,:); 
    end 
    %用record矩阵记录更多详细结果信息(根据需要提取信息) 
    record(iter,1)=iter;        %迭代的次数 
    record(iter,2)=fGBest;      %最好的适应值函数的变化曲线 
    record(iter,3)=GBest(1);    %第一维参数每次迭代的取值     
    record(iter,4)=GBest(2);    %第二维参数每次迭代的取值 
    record(iter,5)=GBest(3);    %三 
    record(iter,6)=GBest(4);    %四  可得按此规律得到所有维数的值 
    record(iter,7)=GBest(5);    %四  可得按此规律得到所有维数的值 
     
    if abs(fGBest-130) <= psoOptions.Vars.ErrGoal      
        success = 1;     %判断全局最优值与上一次迭代时的全局最优之间的差距,满足精度,迭代终止 
    else 
        lastbpf = fGBest;     %记录最好的全局最优适应值 
    end 
end 
 
history=record; 
[fxmin, b] = min(fPBest);    %适应值函数的最优值 
xmin = PBest(b, :);          %输出优化出的最优适应值,粒子所有维的信息 
 
PBest;      %根据需要输出所有备选开关组合 
fPBest;     %输出的所有备选开关组合对应的适应函数值