带容量约束的车辆路径问题,是使用一组具有核定载重约束的车队为一组顾客点提供服务,要求服务的总路径最短。
本文的视频参考B站视频:Lingo求解车辆路径问题模型代码演示
1 基础知识储备
1.1 LINGO 具有9种逻辑运算符
这些运算符的优先级由高到低为
1.2 lingo的窗口状态解析
1.3 @wrap函数解析
1.3.1 官方解释
@wrap(index,limit)
@WRAP( INDEX, LIMIT)
This allows you to "wrap" an index around the end of a set and continue indexing at the other end of the set. That is, when the last (first) member of a set is reached in a set looping function, use of @WRAP will allow you to wrap the set index to the first (last) member of the set. This is a particularly useful function in cyclical, multiperiod planning models.
Formally speaking, @WRAP returns J such that J = INDEX - K * LIMIT, where K is an integer such that J is in the interval [1,LIMIT]. Informally speaking, @WRAP will subtract or add LIMIT to INDEX until it falls in the range 1 to LIMIT.
For an example on the use of the @WRAP function in a staff scheduling model, refer to the Primitive Set Example section in Using Sets.
@wrap函数的确是返回j=index-k*limit,其中k是一个整数,取适当值保证j落在区间[1,limit]内。可是它并不等于做简单的取模再加1的作用。如果硬要在取模方面来说明@wrap函数的话只能这么来解释了:@wrap(index,limit)函数相当于index模limit,如果取模结果不等于0,就返回该结果,否则返回limit。
1.3.2 示例代码及解释
resault = @wrap(10,2); resault1 = @wrap(10,1); resault2 = @wrap(10,3); resault3 = @wrap(10,4); resault4 = @wrap(10,5); resault5 = @wrap(10,6); resault6 = @wrap(10,7); resault7 = @wrap(10,8); resault8 = @wrap(10,9); resault9 = @wrap(10,0); !本行代码会报错误代码86;
代码演示结果
最后一行代码的报错及解释:不允许分母为0
An undefined arithmetic operation (e.g., 1/0 or @LOG( -1)) occurred while LINGO was solving the model. If you have specified a row name for the constraint, LINGO will print the name of the constraint. If you haven't specified row names in your model, you may want to add them to assist in tracking down this error. Check the referenced constraint for any undefined operations and try to either, 1) rewrite it avoiding operations that can potentially become undefined, or, 2) use the @BND function to restrict variables to ranges where all operations are defined.
2 CVRP问题描述与模型
2.1 问题描述
假定存在一个车场Depot和3个顾客需求点,4个节点之间的距离和每个顾客点的需求量如下图所示:
我们的任务是安排车辆从depot出发,然后服务完成这三个顾客需求点所需的车辆行驶路径最短
2.2 定义集合和数据段
我们在模型中使用到如下变量:
q_{i}:表示顾客节点的需求量,对应需求向量Q,维度为1*4;其中4为模型涉及的节点个数,并将车场点设置为0;
u_{i}:车辆行驶至节点i时的累积需求量,对应需求量向量U
d_{ij}:表示节点 i 到节点 j 的空间距离,对应距离矩阵DIST
x_{ij}:表示弧段i,j是否为车辆方位,若访问则取值为1,否则取值为0
2.2.1 定义集合
!定义集合; SETS: CITY/1..4/: Q, U; !定义需求量向量Q_{i}和累积载重量向量U(i); CXC(CITY, CITY): DIST, X;!定义距离矩阵d_{ij}和二进制变量x_{ij}; ENDSETS
2.2.2 定义数据段
!定义数据段; DATA: Q = 0 2 3 5;!需求量向量; DIST = !距离矩阵; 0 3 4 2 3 0 4 5 4 4 0 7 2 5 7 0; C = 5;!车辆的最大载重量为5; ENDDATA
2.2.3 定义目标函数
对应Latex源码:
min z = \sum_{i=1}^{4} \sum_{i=1}^{4} x_{ij} * d_{ij}
对应Lingo源码:
min = @sum(CXC : X * DIST);