码迷,mamicode.com
首页 > 编程语言 > 详细

你想要的CSO算法matlab实现

时间:2021-03-29 12:06:26      阅读:0      评论:0      收藏:0      [点我收藏+]

标签:初始   vma   href   lazy   pop   包括   containe   upd   vbo   

算法思想

技术图片
与其他PSO的关键区别:

  1. 没有pbest和gbest,由竞争机制驱动粒子更新。
  2. 不需要记住历史优秀粒子的位置,只学习当前粒子群中的winner粒子。

算法流程
技术图片
算法细节:

  1. n(n为偶数)个粒子构成的种群,构成n/2个竞争对。

  2. 成对竞争的粒子中,Fitness高者为Winner,低者为Loser。

  3. Loser的velocity更新策略:
    包括:stability component、cognitive component、social component
    技术图片

  4. position更新策略:
    技术图片

  5. Xmean可以是整个种群的平均位置也可以是预定义的领域中局部粒子平均位置。

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]上的最大值。
技术图片

技术图片

你想要的CSO算法matlab实现

标签:初始   vma   href   lazy   pop   包括   containe   upd   vbo   

原文地址:https://www.cnblogs.com/CHNFjishuZone/p/14584249.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!