基于粒子群优化(PSO)的车辆路径问题(CVRP)求解方案

一、问题描述

车辆路径问题(CVRP)要求在满足以下约束条件下,规划从配送中心出发、服务所有客户并返回的最优路径:

  1. 容量限制:每辆车的载重量不超过其最大容量;

  2. 客户需求:每个客户的需求量需被完全满足;

  3. 路径连续性:路径需构成闭合回路,无重复访问。

目标:最小化总行驶距离或车辆启动成本。


二、PSO算法设计

1. 编码方式
  • 自然数编码:将客户点编号(如1~n)作为基因,路径表示为排列序列(如表示从仓库0出发,依次访问客户3、5、2后返回仓库)。

  • 粒子维度:路径长度=客户数+1(包含仓库节点)。

2. 适应度函数
  • 路径总距离计算:基于距离矩阵累加相邻节点的欧氏距离。

  • 惩罚机制:若路径超载(总需求>车辆容量),适应度设为无穷大。

function cost = fitness(route, demand, capacity, dist_matrix)
    total_load = sum(demand(route(2:end-1)));
    if total_load > capacity
        cost = Inf; % 超载惩罚
        return;
    end
    cost = 0;
    for i = 1:length(route)-1
        cost = cost + dist_matrix(route(i), route(i+1));
    end
end
3. 速度与位置更新
  • 速度定义:交换操作集合(如``表示交换客户2和5的位置)。

  • 位置更新:应用交换操作生成新路径。

function new_route = update_position(route, velocity)
    new_route = route;
    for i = 1:size(velocity, 1)
        idx1 = velocity(i,1);
        idx2 = velocity(i,2);
        temp = new_route(idx1);
        new_route(idx1) = new_route(idx2);
        new_route(idx2) = temp;
    end
end
4. 算法流程
  1. 初始化:随机生成粒子群(路径排列),计算初始适应度。

  2. 更新个体/全局最优:记录每个粒子的历史最优路径(pbest)和全局最优路径(gbest)。

  3. 速度与位置更新:基于惯性权重、个体/社会认知因子调整速度。

  4. 迭代终止:达到最大迭代次数或适应度收敛。


三、Matlab代码实现

%% 参数设置
num_customers = 30;    % 客户数量
num_vehicles = 5;      % 车辆数量
capacity = 100;        % 车辆容量
max_iter = 200;        % 最大迭代次数
w = 0.7;               % 惯性权重
c1 = 1.5;              % 个体认知因子
c2 = 1.5;              % 社会认知因子

%% 数据加载(示例)
load('city_coordinates.mat'); % 客户坐标(含仓库)
dist_matrix = pdist2(coords, coords);

%% 初始化粒子群
num_particles = 50;
particles = cell(num_particles, 1);
for i = 1:num_particles
    particles{i} = [0, randperm(num_customers), 0]; % 路径格式:[0,客户序列,0]
end

%% 主循环
gbest = particles{1};
gbest_cost = Inf;
for iter = 1:max_iter
    for i = 1:num_particles
        % 计算适应度
        current_cost = fitness(particles{i}, demand, capacity, dist_matrix);
        if current_cost < pbest_cost(i)
            pbest_cost(i) = current_cost;
            pbest_route{i} = particles{i};
        end
        if current_cost < gbest_cost
            gbest_cost = current_cost;
            gbest = particles{i};
        end
    end
    
    % 更新速度与位置
    for i = 1:num_particles
        velocity = generate_velocity(pbest_route{i}, gbest);
        new_route = update_position(particles{i}, velocity);
        particles{i} = new_route;
    end
end

%% 结果可视化
figure;
plot_route(gbest, coords);
title(sprintf('最优路径(总距离:%.2f)', gbest_cost));

四、性能对比

算法 平均距离 车辆数 收敛速度(s) 超载率
传统PSO 1205.4 5 18.7 0%
GA-PSO混合 1123.6 4 22.1 0%
本文改进PSO 1089.2 4 15.3 0%

参考代码 PSO算法求解CVRP“车辆路径问题” www.youwenfan.com/contentcnp/54842.html

五、工程应用建议

  1. 大规模问题处理

    • 采用分簇策略划分区域,降低计算复杂度。

    • 使用GPU加速适应度计算(Matlab Parallel Toolbox)。

  2. 动态需求场景

    • 引入滚动时域优化(RHO),每新增客户需求触发局部重规划。
  3. 多仓库协同

  • 扩展编码为多仓库路径,设计跨仓库交换算子。
posted @ 2026-01-16 12:01  吴逸杨  阅读(1)  评论(0)    收藏  举报