⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 cpsoh1.m

📁 协同微粒群算法
💻 M
字号:
function PBEST = cpsoh3(num)
global Swarm
psoOptions = get_Options;
Obj = get_Obj(num);

LJmin = [0,-1.0000,-3.0000,-6.0000,-9.1038,-12.7120,-16.5053,-19.8214,-24.1133,-28.4225,-32.7659,-37.9676,-44.3268];
N = Obj.Dim / 3 ;
f2eval = 'LJcluster';

filename =  strcat('R0414_',int2str(N),'.txt');
fid = fopen(filename,'w');

total = 0; 
sum = 0;
for runs = 1 : 10
    [Swarm, PBEST] = initiate(psoOptions,Obj);
    opt = PBEST;
    for iter = 1 : psoOptions.Vars.Iterations               
        for j = 1 : Obj.Dim
            for i = 1 : psoOptions.Vars.PopSize
                fval1 = feval(f2eval, b(j,Swarm(j).individual(i).number));
                fval2 = feval(f2eval, b(j,Swarm(j).individual(i).best));
                fval3 = feval(f2eval, b(j,Swarm(j).P));
                if(fval1 < fval2)                    Swarm(j).individual(i).best = Swarm(j).individual(i).number;                end
                if(fval2 < fval3)                    Swarm(j).P = Swarm(j).individual(i).best;                end
                
                Swarm(j).individual(i).speed = psoOptions.SParams.Chi * ( Swarm(j).individual(i).speed + ... 
                    psoOptions.SParams.c1 * rand * ( Swarm(j).individual(i).best - Swarm(j).individual(i).number ) + ...
                psoOptions.SParams.c2 * rand * ( Swarm(j).P - Swarm(j).individual(i).number ) );                
                
                Swarm(j).individual(i).speed = min(psoOptions.SParams.Vmax, Swarm(j).individual(i).speed);
                Swarm(j).individual(i).speed = max( - psoOptions.SParams.Vmax, Swarm(j).individual(i).speed);
                
                Swarm(j).individual(i).number = Swarm(j).individual(i).number + Swarm(j).individual(i).speed;
                Swarm(j).individual(i).number = min(Obj.ub(j), Swarm(j).individual(i).number);
                Swarm(j).individual(i).number = max(Obj.lb(j), Swarm(j).individual(i).number);         
            end            
        end
        PBEST = feval(f2eval,[Swarm.P]);
        
        if( abs(PBEST-LJmin(N)) < psoOptions.Vars.ErrGoal )
            total = total + 1;
            sum = sum + iter * psoOptions.Vars.PopSize * Obj.Dim;
            break;
        elseif( (opt-PBEST) > psoOptions.Vars.GoalInprove )
            fprintf(1,'\n%d %g\n',iter,PBEST);
            opt = PBEST;
            for i = 1 : 2 * psoOptions.Vars.PopSize
                j = round(rand*(Obj.Dim-1)+1);
                ENDX(j,psoOptions.Vars.PopSize);
            end
        else
            fprintf(1,'%d ',iter);
            %             [x,fval] = simplex(f2eval,[Swarm.P]);
            %             if(fval <= PBEST)
            %                 for j = 1 : Obj.Dim
            %                     Swarm(j).P = x(j);
            %                 end
            %                 PBEST = fval;
            %             end
            for i = 1 : 4 * psoOptions.Vars.PopSize
                j = round(rand*(Obj.Dim-1)+1);
                ENDX(j,psoOptions.Vars.PopSize);
            end
        end
    end 
    fprintf(fid,'\n%d\t%g\n',iter,PBEST); 
%     fVal(runs,:) = PBEST;
%     History(runs,:) = iter;
end
% fprintf(1,'Total number is %d\nSum is %d\nAverage is %.1f\n',total,sum,sum/total);
fclose(fid);

function psoOptions = get_Options()
psoOptions = struct( ...
    ... %Variables of the PSO
    'Vars', struct(...    
    'PopSize',   10, ...     %Swarm Size 
    'Iterations',  100,...    %Maximum Iterations
    'GoalInprove', 1e-2,...
    'ErrGoal',     5e-5 ), ...  %Error goal
    ...  %Stratergy Parameters
    'SParams', struct(... 
    'c1',       2.05, ...      %Cognitive Acceleration
    'c2',       2.05, ...      %Social Acceleration
    'Vmax',     2*pi,...     %Maximum velocity step
    'Chi',      0.7298 ) ...      %Constriction factor
    );
return

function Obj = get_Obj(num)
N = 3 * num;
radius = (N/(4*pi*sqrt(2)))^(1/3); 
Obj = struct(...
    'Dim', N,...
    'ub',[],...
    'lb',zeros(1,N)...
    );
Obj.ub(1:3:N) = 2 * pi;
Obj.ub(2:3:N) = pi;
Obj.ub(3:3:N) = radius;
return

function [Swarm, PBEST] = initiate(psoOptions,Obj)
Swarm = struct(...
    'individual',struct(...
    'number',[], ...
    'speed',[], ...
    'best',[] ), ...
    'P',[] ... 
    );
X = ( ones(psoOptions.Vars.PopSize, 1) * (Obj.ub - Obj.lb) ) .* rand(psoOptions.Vars.PopSize, Obj.Dim) + ones(psoOptions.Vars.PopSize, 1) * Obj.lb;
Y = feval('LJcluster', X);
[PBEST, index] = min(Y);
for j = 1 : Obj.Dim
    for i = 1 : psoOptions.Vars.PopSize
        Swarm(j).individual(i).number = X(i,j);            
        Swarm(j).individual(i).speed = rand * psoOptions.SParams.Vmax;             
        Swarm(j).individual(i).best = X(i,j);    
    end
    Swarm(j).P = X(index,j);   
end
return

function fval = b(j,z)
global Swarm
fval = [Swarm.P];
fval(j) = z;
return

function ENDX(j,npop)
global Swarm
X = [Swarm(j).individual.number];
m = 5; 
nc = 20;
alpha = 0.434;
beta = 0.35 / sqrt(m-3) ;
index = randperm(npop);
p = X(index(1:m));
for i = 1 : nc
    c(i) = (p(1) + p(2)) / 2 + alpha^2 * randn * (p(2) - p(1)) + sum((beta^2 * randn(1, m-2)).*(p(3:m) - sum(p(3:m)) / (m-2)));
end
X = [X c];
i = round(rand*(size(X,1)-1)+1);
Swarm(j).individual(index(2)).number = X(i);
Swarm(j).individual(index(1)).number = Swarm(j).P;

function  [x,fval,exitflag,output] = simplex(f2eval,x0)
options=optimset('LargeScale','off');
options=optimset(options,'MaxFunEvals', 300);
[x,fval,exitflag,output] = fminsearch(f2eval,x0, options);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -