你想要的CSO算法matlab实现
2021-06-07 15:04
标签:初始 vma href lazy pop 包括 containe upd vbo 算法流程 n(n为偶数)个粒子构成的种群,构成n/2个竞争对。 成对竞争的粒子中,Fitness高者为Winner,低者为Loser。 Loser的velocity更新策略: position更新策略: Xmean可以是整个种群的平均位置也可以是预定义的领域中局部粒子平均位置。 测试问题: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
算法思想
与其他PSO的关键区别:
算法细节:
包括:stability component、cognitive component、social componentMatlab代码
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
实验结果