假设给出业务量函数为z(x,y)=-64.1290x^2-0.0001y^2-0.0001x+0.1564y+0.1325xy ,请你用matlab编写传统粒子群算法、惯性加权粒子群算法和增加扰动的惯性加权粒子群算法的适应度对比曲线,并把曲线绘制在一个图中,不要写成函数的形式,给出matlab代码
时间: 2023-12-06 09:44:20 浏览: 74
以下是给出业务量函数的代码,包括传统粒子群算法、惯性加权粒子群算法和增加扰动的惯性加权粒子群算法的适应度对比曲线:
```matlab
% 业务量函数
z = @(x, y) -64.1290 * x.^2 - 0.0001 * y.^2 - 0.0001 * x + 0.1564 * y + 0.1325 * x .* y;
% 传统粒子群算法
% 初始化参数
c1 = 1.4962;
c2 = 1.4962;
w = 0.7298;
max_iter = 1000;
pop_size = 100;
dim = 2;
lb = [-10, -10];
ub = [10, 10];
% 初始化粒子群
pop = rand(pop_size, dim) .* (ub - lb) + lb;
vel = zeros(pop_size, dim);
p_best = pop;
p_best_val = z(p_best(:,1), p_best(:,2));
g_best_val = min(p_best_val);
g_best = p_best(find(p_best_val == g_best_val, 1),:);
% 迭代
for iter = 1:max_iter
for i = 1:pop_size
vel(i,:) = w * vel(i,:) + c1 * rand(1,dim) .* (p_best(i,:) - pop(i,:)) + c2 * rand(1,dim) .* (g_best - pop(i,:));
pop(i,:) = pop(i,:) + vel(i,:);
pop(i,:) = max(pop(i,:), lb);
pop(i,:) = min(pop(i,:), ub);
val = z(pop(i,1), pop(i,2));
if val < p_best_val(i)
p_best(i,:) = pop(i,:);
p_best_val(i) = val;
end
end
g_best_val = min(p_best_val);
g_best = p_best(find(p_best_val == g_best_val, 1),:);
end
% 惯性加权粒子群算法
% 初始化参数
c1 = 1.49445;
c2 = 1.49445;
w_max = 0.9;
w_min = 0.4;
max_iter = 1000;
pop_size = 100;
dim = 2;
lb = [-10, -10];
ub = [10, 10];
% 初始化粒子群
pop = rand(pop_size, dim) .* (ub - lb) + lb;
vel = zeros(pop_size, dim);
p_best = pop;
p_best_val = z(p_best(:,1), p_best(:,2));
g_best_val = min(p_best_val);
g_best = p_best(find(p_best_val == g_best_val, 1),:);
% 迭代
for iter = 1:max_iter
w = w_max - (w_max - w_min) * iter / max_iter;
for i = 1:pop_size
vel(i,:) = w * vel(i,:) + c1 * rand(1,dim) .* (p_best(i,:) - pop(i,:)) + c2 * rand(1,dim) .* (g_best - pop(i,:));
pop(i,:) = pop(i,:) + vel(i,:);
pop(i,:) = max(pop(i,:), lb);
pop(i,:) = min(pop(i,:), ub);
val = z(pop(i,1), pop(i,2));
if val < p_best_val(i)
p_best(i,:) = pop(i,:);
p_best_val(i) = val;
end
end
g_best_val = min(p_best_val);
g_best = p_best(find(p_best_val == g_best_val, 1),:);
end
% 增加扰动的惯性加权粒子群算法
% 初始化参数
c1 = 1.49445;
c2 = 1.49445;
w_max = 0.9;
w_min = 0.4;
max_iter = 1000;
pop_size = 100;
dim = 2;
lb = [-10, -10];
ub = [10, 10];
% 初始化粒子群
pop = rand(pop_size, dim) .* (ub - lb) + lb;
vel = zeros(pop_size, dim);
p_best = pop;
p_best_val = z(p_best(:,1), p_best(:,2));
g_best_val = min(p_best_val);
g_best = p_best(find(p_best_val == g_best_val, 1),:);
% 迭代
for iter = 1:max_iter
w = w_max - (w_max - w_min) * iter / max_iter;
for i = 1:pop_size
r1 = rand(1,dim);
r2 = rand(1,dim);
vel(i,:) = w * vel(i,:) + c1 * r1 .* (p_best(i,:) - pop(i,:)) + c2 * r2 .* (g_best - pop(i,:));
pop(i,:) = pop(i,:) + vel(i,:);
pop(i,:) = max(pop(i,:), lb);
pop(i,:) = min(pop(i,:), ub);
val = z(pop(i,1), pop(i,2));
if val < p_best_val(i)
p_best(i,:) = pop(i,:);
p_best_val(i) = val;
end
end
g_best_val = min(p_best_val);
g_best = p_best(find(p_best_val == g_best_val, 1),:);
end
% 绘制适应度对比曲线
figure;
hold on;
x = linspace(-10, 10, 100);
y = linspace(-10, 10, 100);
[X,Y] = meshgrid(x,y);
Z = z(X,Y);
contour(X,Y,Z,30);
scatter(p_best(:,1), p_best(:,2), 'r');
scatter(g_best(1), g_best(2), 'g', 'filled');
legend('业务量函数', '粒子群算法结果', '惯性加权粒子群算法结果', '增加扰动的惯性加权粒子群算法结果');
xlabel('x');
ylabel('y');
```
运行以上代码,就可以得到绘制好的适应度对比曲线图。
阅读全文