运筹优化学习02:Lingo求解带容量约束的车辆路径问题(CVRP)
栏目:耀世登录 发布时间:2024-07-08
目录1基础知识储备1.1LINGO具有9种逻辑运算符1.2lingo的窗口状态解析1.3@wrap函数解析1.3.1官方解释1.3.2示例代码及解释2CVRP问题描述与模型2.1问题描述2.2定义集合和数据段2.2.1?定义集合2.2.2定义数据段2.2.3定义

目录

1 基础知识储备

1.1 LINGO 具有9种逻辑运算符

1.2 lingo的窗口状态解析

1.3 @wrap函数解析

1.3.1 官方解释

1.3.2 示例代码及解释

2 CVRP问题描述与模型

2.1 问题描述

2.2 定义集合和数据段

2.2.1?定义集合

2.2.2 定义数据段

2.2.3 定义目标函数

2.2.4 定义模型约束条件

3 结果演示


带容量约束的车辆路径问题,是使用一组具有核定载重约束的车队为一组顾客点提供服务,要求服务的总路径最短。

本文的视频参考B站视频:Lingo求解车辆路径问题模型代码演示

#not#否定该操作数的逻辑值,#not#是一个一元表达式
#eq#若两个运算数相等,则为true;否则为flase
#ne#若两个运算符不相等,则为true;否则为flase
#gt#若左边的运算符严格大于右边的运算符,则为true;否则为flase
#ge#若左边的运算符大于或等于右边的运算符,则为true;否则为flase
#lt#若左边的运算符严格小于右边的运算符,则为true;否则为flase
#le#若左边的运算符小于或等于右边的运算符,则为true;否则为flase
#and#仅当两个参数都为true 时,结果为true;否则为flase
#or#仅当两个参数都为false 时,结果为false;否则为true


这些运算符的优先级由高到低为

#not# > #eq# > #ne# > #gt# > #ge# > #lt# > #le# > and > or

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 示例代码及解释

 

代码演示结果

最后一行代码的报错及解释:不允许分母为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.

假定存在一个车场Depot和3个顾客需求点,4个节点之间的距离和每个顾客点的需求量如下图所示:

我们的任务是安排车辆从depot出发,然后服务完成这三个顾客需求点所需的车辆行驶路径最短

我们在模型中使用到如下变量:

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?定义集合

 
 
 

2.2.3 定义目标函数

min z = \sum_{i=1}^{4} \sum_{i=1}^{4} x_{ij} * d_{ij}

对应Latex源码:

 

对应Lingo源码:

 

2.2.4 定义模型约束条件

(1)限定变量x_{ij}为二进制变量

 

(2)为x_{ij}赋值:

程序逻辑:

  • ?第一步,将矩阵X的对角线元素赋值为0;
  • 第二步:为非对角线元素赋值
    • 若下一个节点为depot节点则设置为x_{ij} = 1,否则为0
    • 若为非depot节点,判断累加需求量是否大于车辆约束C,若是设置x_{ij} = 1

对应Lingo源码:

 

(3)车辆数目约束

vehicleCnt = \sum_{i=1}^{N} q_(i) / C;

 
 

? Local optimal solution found.
? Objective value: ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?15.00000
? Objective bound: ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?15.00000
? Infeasibilities: ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?0.000000
? Extended solver steps: ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 2
? Total solver iterations: ? ? ? ? ? ? ? ? ? ? ? ? ? 113


? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?Variable ? ? ? ? ? Value
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? C ? ? ? ?5.000000
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?VCAP ? ? ? ?37766.16
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?VEHCLF ? ? ? 0.2647873E-03
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?VEHCLR ? ? ? ?1.000000
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? Q( 1) ? ? ? ?0.000000
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? Q( 2) ? ? ? ?2.000000
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? Q( 3) ? ? ? ?3.000000
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? Q( 4) ? ? ? ?5.000000
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? U( 1) ? ? ? ?0.000000
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? U( 2) ? ? ? ?2.000000
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? U( 3) ? ? ? ?5.000000
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? U( 4) ?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? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?16 ? ? ? ?0.000000
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?17 ? ? ? ?0.000000
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?18 ? ? ? ?0.000000
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?19 ? ? ? ?37764.16
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?20 ? ? ? ?37761.16
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?21 ? ? ? ?0.000000
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?22 ? ? ? ?0.000000
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?23 ? ? ? ?0.000000
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?24 ? ? ? ?0.000000
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?25 ? ? ? ?1.000000

?

最优解:? 1 --> 4 --> 1;? ?1-->2-->3-->1

对CVRP的求解过程进行分析,并给出源代码的编写过程,对于代码本身可能还有其他的编码方式,也希望高人无私共享,共同学习

有兴趣共同学习的欢迎关注个人公众账号"学而立行“”

本文模型及代码将在【运筹优化与地理信息】公众号付费发布,如不愿意付费,可移步B站同名帐号,观看手敲代码及演示视频

?

平台注册入口