1 Star 5 Fork 0

User1396529 / GA CVRP Optimize - 基于 MATLAB 通过遗传算法实现车辆路径优化问题求解

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
LGPL-3.0

基于 MATLAB 通过遗传算法实现车辆路径优化问题求解

2024年5月的实验课作业,作业要求使用遗传算法解决任意的交通领域实践问题。

CVRP 问题,即包含容量限制的车辆路径优化问题(Capacity Vehicle Routing Problem),是物流和运输领域中的一个经典优化问题。其目标是确定一组车辆从一个配送中心出发,访问一系列客户节点并返回配送中心的 最优路径方案。该问题的目的是在满足所有客户需求和车辆运载能力限制的同时,最小化总行驶距离或总成本。该问题可以被认为是 TSP 问题的一种变体,即增加了车辆容量限制的 TSP 问题。TSP 问题也可以被理解为无容量限制的 VRP 问题,即一辆车走完全局最短路即可满足所有节点的需求。

本项目基于 MATLAB,通过构建一个遗传算法求解器对一个 CVRP 问题进行了求解。

文件说明

项目主目录下包含若干个 .m 文件,其中 main.m 是算例项目,求解器主体是函数文件 GA_CVRP_optimize.mimagedata 文件夹分别存放输出的图片和数据资料。此外,代码包含详细的注释信息,不提供额外的文档。

示例代码及算例

算例来自于 MATLAB 官网文章 Capacitated Vehicle Routing Problem,坐标点及各个顾客的需求量根据原文中的方法随机生成。算例可视化如下图所示,详情请参照原文。

Capacitated vehicle routing is a combination of a knapsack problem and a traveling salesperson problem. The problem is for a vehicle (or set of vehicles) to visit a group of customers that are geographically distributed. The vehicle has a capacity constraint, where the capacity refers to a quantity that the vehicle delivers to each customer. The problem has a central depot, and the vehicle must return to the depot after each visit to a set of customers, or route. The problem is to visit the customers at minimal cost, where the cost is the total length of the route for visiting a group of customers.

The following figure shows four routes originating from a single point, the depot. These routes do not represent a minimal solution, because nodes 2 and 3 (at least) should be visited in the opposite order. The route containing nodes 2 and 3 has a self-intersection, which does not occur in an optimal tour.

算例可视化

算法使用顾客与仓库之间的 OD 矩阵,而不是真实坐标进行运算,保障了算法的兼容性和可拓展性。

算法设计简述

基因编码设计: 基因编码设计参考了博客 用遗传算法解决VRP问题,其基本逻辑为:首先随机生成所有顾客节点编号的随机排列,再在其中插入数量相当于运载工具数量减去 1 数量的 0,即,设此时共有 $K$ 辆车,则插入的 0 元素数量为 $K - 1$。每一段用 0 分割的节点序列即代表一辆车的访问节点次序。详情参见原文所述。

约束处理: 算法强调并实现了如下的模型约束:

  • 每个客户节点必须被访问一次。
  • 每辆车从配送中心出发并返回配送中心。
  • 每辆车的总负载不能超过其最大载重量。

适应度函数取值: 取总路程倒数为适应度函数值。对于超出模型约束(车辆数量约束和总载重量限制约束)的个体,强制令其适应度函数值为 0。

交叉和变异方法: 使用单点交叉和单点变异的方法。对于交叉、变异后产生不符合模型约束的非法个体的情形,不予交叉变异。 该策略通过在交叉、变异前提前对交叉编译产生的个体进行检验并根据检验结果进行判断来实现。

精英策略: 算法保存历次迭代中取得的最优适应度个体,并在未来次数迭代中使用该个体替换种群中适应度最低的个体,避免种群劣化。

算法迭代过程

设置迭代次数为 1000 次,种群大小为 50000。由于交叉变异过程受到模型约束的限制,为了保障实际的交叉变异发生率,应当适当增加交叉变异概率。这里取得交叉变异概率分别为 $P_c = 0.9, P_m = 0.09$。

求解器支持在算法迭代过程中实时动态绘制下降过程,绘制结果如下图所示。

遗传算法中最小化路程的下降过程

输出结果

算法的输出结果如下图所示,图中使用不同的颜色标记了不同车辆的访问路径。取得极小化路径长度为 732.00。

遗传算法的优化结果

开源地址

本人已将代码基于 LGPL-3.0 许可证开源至 Gitee 平台,仓库地址为 BOXonline_1396529/GA-CVRP-opt,可以在遵照许可证规范的前提下自行肆意修改取用。

GNU LESSER GENERAL PUBLIC LICENSE Version 3, 29 June 2007 Copyright (C) 2007 Free Software Foundation, Inc. <http://fsf.org/> Everyone is permitted to copy and distribute verbatim copies of this license document, but changing it is not allowed. This version of the GNU Lesser General Public License incorporates the terms and conditions of version 3 of the GNU General Public License, supplemented by the additional permissions listed below. 0. Additional Definitions. As used herein, "this License" refers to version 3 of the GNU Lesser General Public License, and the "GNU GPL" refers to version 3 of the GNU General Public License. "The Library" refers to a covered work governed by this License, other than an Application or a Combined Work as defined below. An "Application" is any work that makes use of an interface provided by the Library, but which is not otherwise based on the Library. Defining a subclass of a class defined by the Library is deemed a mode of using an interface provided by the Library. A "Combined Work" is a work produced by combining or linking an Application with the Library. The particular version of the Library with which the Combined Work was made is also called the "Linked Version". The "Minimal Corresponding Source" for a Combined Work means the Corresponding Source for the Combined Work, excluding any source code for portions of the Combined Work that, considered in isolation, are based on the Application, and not on the Linked Version. The "Corresponding Application Code" for a Combined Work means the object code and/or source code for the Application, including any data and utility programs needed for reproducing the Combined Work from the Application, but excluding the System Libraries of the Combined Work. 1. Exception to Section 3 of the GNU GPL. You may convey a covered work under sections 3 and 4 of this License without being bound by section 3 of the GNU GPL. 2. Conveying Modified Versions. If you modify a copy of the Library, and, in your modifications, a facility refers to a function or data to be supplied by an Application that uses the facility (other than as an argument passed when the facility is invoked), then you may convey a copy of the modified version: a) under this License, provided that you make a good faith effort to ensure that, in the event an Application does not supply the function or data, the facility still operates, and performs whatever part of its purpose remains meaningful, or b) under the GNU GPL, with none of the additional permissions of this License applicable to that copy. 3. Object Code Incorporating Material from Library Header Files. The object code form of an Application may incorporate material from a header file that is part of the Library. You may convey such object code under terms of your choice, provided that, if the incorporated material is not limited to numerical parameters, data structure layouts and accessors, or small macros, inline functions and templates (ten or fewer lines in length), you do both of the following: a) Give prominent notice with each copy of the object code that the Library is used in it and that the Library and its use are covered by this License. b) Accompany the object code with a copy of the GNU GPL and this license document. 4. Combined Works. You may convey a Combined Work under terms of your choice that, taken together, effectively do not restrict modification of the portions of the Library contained in the Combined Work and reverse engineering for debugging such modifications, if you also do each of the following: a) Give prominent notice with each copy of the Combined Work that the Library is used in it and that the Library and its use are covered by this License. b) Accompany the Combined Work with a copy of the GNU GPL and this license document. c) For a Combined Work that displays copyright notices during execution, include the copyright notice for the Library among these notices, as well as a reference directing the user to the copies of the GNU GPL and this license document. d) Do one of the following: 0) Convey the Minimal Corresponding Source under the terms of this License, and the Corresponding Application Code in a form suitable for, and under terms that permit, the user to recombine or relink the Application with a modified version of the Linked Version to produce a modified Combined Work, in the manner specified by section 6 of the GNU GPL for conveying Corresponding Source. 1) Use a suitable shared library mechanism for linking with the Library. A suitable mechanism is one that (a) uses at run time a copy of the Library already present on the user's computer system, and (b) will operate properly with a modified version of the Library that is interface-compatible with the Linked Version. e) Provide Installation Information, but only if you would otherwise be required to provide such information under section 6 of the GNU GPL, and only to the extent that such information is necessary to install and execute a modified version of the Combined Work produced by recombining or relinking the Application with a modified version of the Linked Version. (If you use option 4d0, the Installation Information must accompany the Minimal Corresponding Source and Corresponding Application Code. If you use option 4d1, you must provide the Installation Information in the manner specified by section 6 of the GNU GPL for conveying Corresponding Source.) 5. Combined Libraries. You may place library facilities that are a work based on the Library side by side in a single library together with other library facilities that are not Applications and are not covered by this License, and convey such a combined library under terms of your choice, if you do both of the following: a) Accompany the combined library with a copy of the same work based on the Library, uncombined with any other library facilities, conveyed under the terms of this License. b) Give prominent notice with the combined library that part of it is a work based on the Library, and explaining where to find the accompanying uncombined form of the same work. 6. Revised Versions of the GNU Lesser General Public License. The Free Software Foundation may publish revised and/or new versions of the GNU Lesser General Public License from time to time. Such new versions will be similar in spirit to the present version, but may differ in detail to address new problems or concerns. Each version is given a distinguishing version number. If the Library as you received it specifies that a certain numbered version of the GNU Lesser General Public License "or any later version" applies to it, you have the option of following the terms and conditions either of that published version or of any later version published by the Free Software Foundation. If the Library as you received it does not specify a version number of the GNU Lesser General Public License, you may choose any version of the GNU Lesser General Public License ever published by the Free Software Foundation. If the Library as you received it specifies that a proxy can decide whether future versions of the GNU Lesser General Public License shall apply, that proxy's public statement of acceptance of any version is permanent authorization for you to choose that version for the Library.

简介

2024年5月的实验课作业,旨在使用遗传算法解决 CVRP 问题。CVRP 问题,即包含容量限制的车辆路径优化问题,是物流和运输领域中的一个经典优化问题。其目标是确定一组车辆从一个配送中心出发,访问一系列客户节点并返回配送中心的 最优路径方案。该问题的目的是在满足所有客户需求和车辆运载能力限制的同时,最小化总行驶距离或总成本。 展开 收起
MATLAB
LGPL-3.0
取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/BOXonline_1396529/GA-CVRP-opt.git
git@gitee.com:BOXonline_1396529/GA-CVRP-opt.git
BOXonline_1396529
GA-CVRP-opt
GA CVRP Optimize - 基于 MATLAB 通过遗传算法实现车辆路径优化问题求解
master

搜索帮助

344bd9b3 5694891 D2dac590 5694891