你想要的CSO算法matlab實現

算法思想


與其餘PSO的關鍵區別:算法

  1. 沒有pbest和gbest,由競爭機制驅動粒子更新。
  2. 不須要記住歷史優秀粒子的位置,只學習當前粒子羣中的winner粒子。

算法流程
在這裏插入圖片描述
算法細節:函數

  1. n(n爲偶數)個粒子構成的種羣,構成n/2個競爭對。學習

  2. 成對競爭的粒子中,Fitness高者爲Winner,低者爲Loser。測試

  3. Loser的velocity更新策略:
    包括:stability component、cognitive component、social component
    在這裏插入圖片描述lua

  4. position更新策略:
    在這裏插入圖片描述spa

  5. Xmean能夠是整個種羣的平均位置也能夠是預約義的領域中局部粒子平均位置。code

Matlab代碼

clc;
clear;

%參數設置
Vnum = 1;
N = 50;
XBound = [0 20];
VBound = [-1 1];
iterations = 100;
lambda = 0;

%初始粒子羣

P = initializeParticles(N,Vnum,XBound,VBound);
newP = zeros(N,2*Vnum);

%初始評估
[Fitness] = EvaluatePartical(P(:,1:Vnum),1);

%初始種羣
figure
x=linspace(0,20,1000);
y=x .* sin(x) .* cos(2 * x) - 2 * x .* sin(3 * x);
plot(x,y);
hold on 
x = P(:,1:Vnum);
y = Fitness;
plot(x,y,'ro');
hold off

for i = 1:iterations
    %隨機構建競爭對
    index = randperm(N);
    P = P(index,:);Fitness = Fitness(index);
    Xmean = Cal_Xmean(P(:,1:Vnum));
    %競爭
    for j = 1:2:N-1
        F1 = Fitness(j);
        F2 = Fitness(j+1); 
        if(F1>=F2)
            Pwin = P(j,:);
            Plose = P(j+1,:);
        else
            Pwin = P(j+1,:);
            Plose = P(j,:);
        end
        newP(j,:) = Pwin;
        %更新Xlose
        Plose = UpdateVandP(Plose,Pwin(1:Vnum),Xmean,Vnum,lambda,XBound,VBound);
        newP(j+1,:) = Plose;
    end
    P = newP;
    [Fitness] = EvaluatePartical(P(:,1:Vnum),1);
end

[~,index] = max(Fitness);
disp(P(index,1:Vnum));
%末代
figure
x=linspace(0,20,1000);
y=x .* sin(x) .* cos(2 * x) - 2 * x .* sin(3 * x);
plot(x,y);
hold on 
X = P(:,1:Vnum);
Y = X .* sin(X) .* cos(2 * X) - 2 * X .* sin(3 * X);
plot(X,Y,'ro');
hold off

function [pop] = initializeParticles(N,Vnum,XBound,VBound)
%INITIALIZEPARTICLES 初始種羣函數
    X = rand(N,Vnum)*(XBound(2)-XBound(1))+XBound(1);
    V = rand(N,Vnum)*(VBound(2)-VBound(1))+VBound(1);
    pop = [X V];
end

function [Fitness] = EvaluatePartical(X,type)
%EVALUATEPARTICAL 評估粒子函數
    f = @(x)x .* sin(x) .* cos(2 * x) - 2 * x .* sin(3 * x);
    if type == 1
        [N] = size(X,1);
        Fitness = zeros(N,1);

        for i = 1:N
            x = X(i,:);
            Fitness(i) = f(x); 
        end
        
    elseif type==2
        Fitness = f(X);
    end
end

function [newP] = UpdateVandP(Plose,Xwin,Xmean,Vnum,lambda,XBound,VBound)
%UPDATEVANDP 更新粒子函數
      Xmax = zeros(1,Vnum) + XBound(2);
      Xmin = zeros(1,Vnum) + XBound(1);
      Vmax = zeros(1,Vnum) + VBound(2);
      Vmin = zeros(1,Vnum) + VBound(1);
      
      Xlose = Plose(1:Vnum);Vlose = Plose(Vnum+1:2*Vnum);
      r = rand(3,Vnum);
      Vlose = r(1).*Vlose+r(2).*(Xwin-Xlose)+lambda*r(3).*(Xmean-Xlose);
      
      Vlose(Vlose>Vmax) = Vmax(Vlose>Vmax);
      Vlose(Vlose<Vmin) = Vmin(Vlose<Vmin);
      
      Xlose = Xlose + Vlose;
      
      Xlose(Xlose>Xmax) = Xmax(Xlose>Xmax);
      Xlose(Xlose<Xmin) = Xmin(Xlose<Xmin);
      newP = [Xlose Vlose];      
end

function [Xmean] = Cal_Xmean(X)
%CAL_XMEAN 計算Xmean 
    Xmean = mean(X,1);
end

實驗結果

測試問題:f = xsin(x)cos(2x)-2xsin(3x)在[0,20]上的最大值。
在這裏插入圖片描述component

在這裏插入圖片描述

相關文章
相關標籤/搜索