基本原理
考虑每个迭代有
N
N
N个粒子(一般设置10 ~ 30个),每个粒子有
D
D
D个维度(视具体问题而定),则对每一个粒子的更新都应该是
D
D
D维度的:
{
x
i
=
(
x
i
1
,
x
i
2
,
⋯
x
i
D
)
,
i
=
1
,
2
,
⋯
,
N
v
i
=
(
v
i
1
,
v
i
2
,
⋯
v
i
D
)
,
i
=
1
,
2
,
⋯
,
N
\left\{ \begin{array}{l} {x_i} = \left( {{x_{i1}},{x_{i2}}, \cdots {x_{iD}}} \right),i = 1,2, \cdots ,N\\ {v_i} = \left( {{v_{i1}},{v_{i2}}, \cdots {v_{iD}}} \right),i = 1,2, \cdots ,N \end{array} \right.
{xi=(xi1,xi2,⋯xiD),i=1,2,⋯,Nvi=(vi1,vi2,⋯viD),i=1,2,⋯,N
速度的求解,以及新粒子的求解,如下所示:
{
v
i
(
t
+
1
)
=
w
v
i
(
t
)
+
c
1
r
1
(
t
)
[
p
i
(
t
)
−
x
i
(
t
)
]
+
c
2
r
2
(
t
)
[
p
g
(
t
)
−
x
i
(
t
)
]
x
i
(
t
+
1
)
=
x
i
(
t
)
+
v
i
(
t
+
1
)
\left\{ \begin{array}{l} {v_i}(t + 1) = w{v_i}(t) + {c_1}{r_1}(t)\left[ {{p_i}(t) - {x_i}(t)} \right] + {c_2}{r_2}(t)\left[ {{p_g}(t) - {x_i}(t)} \right]\\ {x_i}(t + 1) = {x_i}(t) + {v_i}(t + 1) \end{array} \right.
{vi(t+1)=wvi(t)+c1r1(t)[pi(t)−xi(t)]+c2r2(t)[pg(t)−xi(t)]xi(t+1)=xi(t)+vi(t+1)
其中, w w w为惯性权重,取值范围0.5 ~ 0.9; r 1 , r 2 r_1,r_2 r1,r2为0~1之间的随机数; c 1 c_1 c1为局部最优权重,取值范围0.1 ~ 2.0; c 2 c_2 c2为全局最优权重,取值范围0.1 ~ 2.0; p i p_i pi为局部最优解; p g p_g pg为全局最优解。
matlab源码
使用粒子群算法对Rosenbrock问题进行求解。Rosenbrock函数是一个用来测试最优化算法性能的非凸函数,由Howard Harry Rosenbrock在1960年提出,也称为Rosenbrock山谷函数、Rosenbrock香蕉函数、香蕉函数。Rosenbrock函数的定义为:
f
(
x
,
y
)
=
(
1
−
x
)
2
+
100
(
y
−
x
2
)
2
f\left( {x,y} \right) = {\left( {1 - x} \right)^2} + 100{\left( {y - {x^2}} \right)^2}
f(x,y)=(1−x)2+100(y−x2)2
matlab源码,自写代码
clc; clear;
% 参数设定
n_particles = 30; % 粒子数量
n_iterations = 100; % 迭代次数
dim = 2; % Rosenbrock函数的维度为2
c1 = 2; % 学习因子
c2 = 2; % 学习因子
w = 0.9; % 惯性权重
x_min = -5; % 搜索空间的最小值
x_max = 5; % 搜索空间的最大值
v_max = 0.1 * (x_max - x_min); % 最大速度
% 初始化粒子的位置和速度
particles = x_min + (x_max - x_min) .* rand(n_particles, dim);
velocities = -v_max + 2 * v_max .* rand(n_particles, dim);
p_best = particles; % 个体最优位置
p_best_score = inf(n_particles, 1); % 个体最优值
g_best = []; % 全局最优位置
g_best_score = inf; % 全局最优值
% 主循环
for iteration = 1:n_iterations
for i = 1:n_particles
% 计算粒子的适应度值
fitness = (1 - particles(i, 1))^2 + 100 * (particles(i, 2) - particles(i, 1)^2)^2;
% 更新个体最优
if fitness < p_best_score(i)
p_best(i, :) = particles(i, :);
p_best_score(i) = fitness;
end
% 更新全局最优
if fitness < g_best_score
g_best = particles(i, :);
g_best_score = fitness;
end
end
% 更新粒子的速度和位置
for i = 1:n_particles
velocities(i, :) = w * velocities(i, :) + c1 * rand() * (p_best(i, :) - particles(i, :)) ...
+ c2 * rand() * (g_best - particles(i, :));
velocities(i, :) = min(max(velocities(i, :), -v_max), v_max); % 限制速度
particles(i, :) = particles(i, :) + velocities(i, :);
particles(i, :) = min(max(particles(i, :), x_min), x_max); % 限制位置在搜索空间内
end
end
% 输出结果
disp('全局最优位置:');
disp(g_best);
disp('全局最优值:');
disp(g_best_score);
matlab源码,调用particleswarm
% Rosenbrock函数
fun = @(x)(1 - x(1))^2 + 100 * (x(2) - x(1)^2)^2;
% 选项设定
options = optimoptions('particleswarm','SwarmSize',30,'MaxIterations',100);
% 搜索空间的边界
lb = [-5, -5]; % 下界
ub = [5, 5]; % 上界
% 使用particleswarm函数找到最小值
[x,fval] = particleswarm(fun,2,lb,ub,options);
% [x,fval,exitflag,output] = particleswarm(fun,2,lb,ub,options);
% 输出结果
disp('全局最优位置:');
disp(x);
disp('全局最优值:');
disp(fval);
matlab源码,自写代码+画图展示
clc;clear;
% 参数设定
n_particles = 30; % 粒子数量
n_iterations = 100; % 迭代次数
dim = 2; % Rosenbrock函数的维度为2
c1 = 2; % 学习因子
c2 = 2; % 学习因子
w = 0.9; % 惯性权重
x_min = -5; % 搜索空间的最小值
x_max = 5; % 搜索空间的最大值
v_max = 0.1 * (x_max - x_min); % 最大速度
% 初始化粒子的位置和速度
particles = x_min + (x_max - x_min) .* rand(n_particles, dim);
velocities = -v_max + 2 * v_max .* rand(n_particles, dim);
p_best = particles; % 个体最优位置
p_best_score = inf(n_particles, 1); % 个体最优值
g_best = []; % 全局最优位置
g_best_score = inf; % 全局最优值
% 用于绘图的Rosenbrock函数网格
x_range = x_min:0.1:x_max;
y_range = x_min:0.1:x_max;
[X, Y] = meshgrid(x_range, y_range);
Z = (1 - X).^2 + 100 * (Y - X.^2).^2;
% 创建GIF文件
gif_filename = 'N5_opt_PSO_self4.gif';
% 主循环
for iteration = 1:n_iterations
fitness_values = zeros(n_particles, 1); % 用于存储每个粒子的适应度值
for i = 1:n_particles
% 计算粒子的适应度值
fitness = (1 - particles(i, 1))^2 + 100 * (particles(i, 2) - particles(i, 1)^2)^2;
fitness_values(i) = fitness; % 存储适应度值
% 更新个体最优
if fitness < p_best_score(i)
p_best(i, :) = particles(i, :);
p_best_score(i) = fitness;
end
% 更新全局最优
if fitness < g_best_score
g_best = particles(i, :);
g_best_score = fitness;
end
end
% 绘制Rosenbrock函数的三维图和粒子的位置
figure(1);
surf(X, Y, Z, 'EdgeColor', 'none', 'FaceAlpha', 0.5);
hold on;
scatter3(particles(:, 1), particles(:, 2), fitness_values, 'r*');
title(['Iteration ', num2str(iteration)]);
xlabel('x');
ylabel('y');
zlabel('Fitness');
hold off;
drawnow;
% 获取当前图形的帧
frame = getframe(1);
im = frame2im(frame);
[imind,cm] = rgb2ind(im,256);
% 写入GIF文件
if iteration == 1
imwrite(imind,cm,gif_filename,'gif', 'Loopcount',inf, 'DelayTime',0.2);
elseif iteration <= 5 || mod(iteration,5) == 0
imwrite(imind,cm,gif_filename,'gif','WriteMode','append', 'DelayTime',0.2);
end
% 更新粒子的速度和位置
for i = 1:n_particles
velocities(i, :) = w * velocities(i, :) + c1 * rand() * (p_best(i, :) - particles(i, :)) ...
+ c2 * rand() * (g_best - particles(i, :));
velocities(i, :) = min(max(velocities(i, :), -v_max), v_max); % 限制速度
particles(i, :) = particles(i, :) + velocities(i, :);
particles(i, :) = min(max(particles(i, :), x_min), x_max); % 限制位置在搜索空间内
end
end
imwrite(imind,cm,gif_filename,'gif','WriteMode','append', 'DelayTime',2); % 静止2秒
% 输出结果
disp('全局最优位置:');
disp(g_best);
disp('全局最优值:');
disp(g_best_score);