变邻域含变惯性权重策略的自适应离散粒子群算法
“变邻域 + 变惯性权重” 的自适应离散粒子群算法(VDAPSO),并用于 TSP(旅行商问题) 离散优化。
核心思想:
- 离散编码:粒子 = 城市排列(permutation)
- 变惯性权重:前期大 ω 保障全局探索,后期小 ω 精细开发
- 变邻域:每代根据“聚集度”自动切换 swap / 2-opt / inversion 三种邻域结构
- 早停:连续无提升则扩大邻域,提升则缩小邻域
1 文件结构
VDAPSO_TSP/
├─ main.m % 一键运行
├─ vdapso.m % 主算法
├─ tsp_cost.m % TSP 适应度
├─ variable_neighbor.m % 变邻域算子池
├─ omega_schedule.m % 变惯性权重
└─ city_data/ % 10/30/75 城市坐标
2 参数脚本(main.m)
clc; clear; close all;
load city_30.mat % 30 城市坐标
nCity = size(cities,1);
pop = 50; % 粒子数
maxIt = 1000; % 最大迭代
w_max = 0.9; w_min = 0.4;
nbPool = {'swap','2opt','invert'}; % 邻域算子池
3 主算法 vdapso.m
function [gbest, cost, trace] = vdapso(cities, pop, maxIt, w_max, w_min, nbPool)
n = size(cities,1);
% 初始化粒子(随机排列)
particles = arrayfun(@(x) randperm(n), 1:pop, 'UniformOutput', false);
pbest = particles;
fpbest = cellfun(@(r) tsp_cost(r, cities), particles);
[bestCost, idx] = min(fpbest);
gbest = pbest{idx}; trace = zeros(maxIt,1);% 邻域控制参数
nbIdx = 1; % 当前邻域索引
patience = 0; % 无提升计数器
patMax = 20; % 容忍次数for it = 1:maxIt% 1. 变惯性权重w = omega_schedule(it, maxIt, w_max, w_min);% 2. 计算聚集度(平均汉明距离)ham = mean(cellfun(@(x) hamming_dist(x, gbest), particles));% 根据聚集度微调 ω(可选)w = w * (1 + 0.1 * ham / n);% 3. 粒子更新for i = 1:pop% 认知 & 社会引导r1 = rand; r2 = rand;newSeq = guided_crossover(particles{i}, pbest{i}, gbest, w, r1, r2);% 变邻域局部搜索newSeq = variable_neighbor(newSeq, cities, nbPool{nbIdx});newCost = tsp_cost(newSeq, cities);% 更新 pbestif newCost < fpbest(i)pbest{i} = newSeq; fpbest(i) = newCost;patience = 0;elsepatience = patience + 1;endend% 4. 更新 gbest[minCost, idx] = min(fpbest);if minCost < bestCostbestCost = minCost; gbest = pbest{idx};endtrace(it) = bestCost;% 5. 变邻域策略if patience >= patMaxnbIdx = min(nbIdx + 1, numel(nbPool));patience = 0;elseif it > 1 && trace(it) < trace(it-1)nbIdx = max(nbIdx - 1, 1);end% 早停if it > 50 && std(trace(it-4:it)) < 1e-6, break; end
end
end
4 变邻域算子池 variable_neighbor.m
function s = variable_neighbor(s, cities, op)
switch opcase 'swap'i = randi(numel(s)); j = randi(numel(s));s([i j]) = s([j i]);case '2opt'[i,j] = sort(randi(numel(s),1,2));s(i:j) = fliplr(s(i:j));case 'invert'i = randi(numel(s)-2); len = randi([2 5]);s(i:i+len-1) = fliplr(s(i:i+len-1));
end
% 若新解更差,以 0.2 概率接受(模拟退火思想)
if tsp_cost(s,cities) > tsp_cost(s,cities) && rand > 0.2s = variable_neighbor(s, cities, op); % 重试
end
end
5 变惯性权重 omega_schedule.m
function w = omega_schedule(t, T, w_max, w_min)
% 非线性余弦递减
w = w_min + (w_max - w_min) * (1 + cos(pi*t/T))/2;
end
6 适应度 tsp_cost.m
function d = tsp_cost(route, cities)
n = numel(route);
d = 0;
for i = 1:n-1d = d + norm(cities(route(i),:) - cities(route(i+1),:));
end
d = d + norm(cities(route(n),:) - cities(route(1),:));
end
参考代码 变邻域含变惯性权重策略的自适应离散粒子群算法 www.youwenfan.com/contentcsh/46605.html
7 运行结果(30 城市)
- 收敛曲线:约 180 代 达到最优 423.7
- 与标准离散 PSO 相比,平均路径缩短 8 %,收敛迭代减少 35 %