变邻域含变惯性权重策略的自适应离散粒子群算法

“变邻域 + 变惯性权重” 的自适应离散粒子群算法(VDAPSO),并用于 TSP(旅行商问题) 离散优化。

核心思想:


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);
        
        % 更新 pbest
        if newCost < fpbest(i)
            pbest{i} = newSeq;  fpbest(i) = newCost;
            patience = 0;
        else
            patience = patience + 1;
        end
    end
    
    % 4. 更新 gbest
    [minCost, idx] = min(fpbest);
    if minCost < bestCost
        bestCost = minCost;  gbest = pbest{idx};
    end
    trace(it) = bestCost;
    
    % 5. 变邻域策略
    if patience >= patMax
        nbIdx = 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 op
    case '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.2
    s = 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-1
    d = d + norm(cities(route(i),:) - cities(route(i+1),:));
end
d = d + norm(cities(route(n),:) - cities(route(1),:));
end

参考代码 变邻域含变惯性权重策略的自适应离散粒子群算法 www.youwenfan.com/contentcnf/46605.html

7 运行结果(30 城市)

 

专注于matlab/simulink,电子电路,编程