运筹系列81:LKH代码分析

news2025/2/21 19:33:55

1. 基本数据结构

基础的node定义在LKH.h中
在这里插入图片描述
用于2-level tree的segment定义如下:
在这里插入图片描述
LKH可以使用3种数据结构,默认是2-level tree:
在这里插入图片描述
2-level tree的flip操作(即2-opt算子),在Flip_SL.c中,特殊的地方在于flip过程中涉及到重新分配segment的操作,其逻辑如下,其中P开头的是node对应的segment编号。
在这里插入图片描述

2. 读取问题数据

2.1 关键词清单

读数据代码在ReadProblem.c中,必须包含两部分:specification part和data part。specification part支持的关键词包括:

* NAME : <string>e
 * Identifies the data file.
 *
 * TYPE : <string>。可以是非对称tsp问题,带时间窗的问题,经典VRP问题,非对称VRP问题,pickup-and-delivery TSP问题,开放VRP问题,。
 * Specifies the type of data. Possible types are
 * TSP          Data for a symmetric traveling salesman problem
 * ATSP         Data for an asymmetric traveling salesman problem
 * SOP          Data for a sequence ordering problem
 * HCP          Hamiltonian cycle problem data
 * HPP          Hamiltonian path problem data (not available in TSPLIB)
 * TSPTW        Data for a TSP instance with time windows
 * CCVRP        Data for a cumulative capacitated vehicle routing problem
 * CVRP         Data for a symmetric capacitated vehicle routing problem
 * ACVRP        Data for an asymmetric capacitated vehicle routing problem
 * ADCVRP       Data for an asymmetric distance constrained vehicle
 *              routing problem
 * CVRPTW       Data for a capacitated vehicle routing problem with
 *              time windows
 * VRPMPD       Data for a mixed pickup and delivery problem with backhauls
 * 1-PDTSP      Data for a one-commodity pickup-and-delivery traveling
 *              salesman problem
 * MLP          Data for a minimum latency problem
 * m-PDTSP      Data for a mulity-commodity pickup-and-delivery traveling
 *              salesman problem
 * m1-PDTSP     Data for a mulity-commodity one-to-one pickup-and-delivery
 *              traveling salesman problem
 * OVRP         Data for an open vehicle routing problem
 * PDTSP        Data for a pickup and delivery traveling salesman problem
 * PDTSPF       Data for a pickup and delivery traveling salesman problem
 *              with FIFO loading
 * PDTSPL       Data for a pickup and delivery traveling salesman problem
 *              with LIFO loading
 * PDPTW        Data for a pickup and delivery problem with time windows
 * RCTVRP       Data for a risk-constrained cash-in-transit vehicle
 *              routing problem
 * RCTVRPTW     Data for a risk-constrained cash-in-transit vehicle
 *              routing problem with time windows
 * TRP          Data for a traveling repairman problem
 * TSPDL        Dara for a traveling salesman problem with draft limits
 * TSPPD        Data for a pickup and delivery travling salesman problem
 * TSTSP        Data for a Steiner traveling salesman problem
 * VRPB         Data for a vehicle routing problem with backhauls
 * VRPBTW       Data for a vehicle routing problem with backhauls and
 *              time windows
 * CTSP         Data for a colored traveling salesman problem
 *
 * COMMENT : <string>
 * Additional comments (usually the name of the contributor or the creator of
 * the problem instance is given here).
 *
 * DIMENSION : < integer>
 * The number of nodes.
 *
 * CAPACITY : <integer>
 * Specifies the truck capacity in a CVRP.
 *
 * DISTANCE : <real>
 * The maximum length allowed for each route in a CVRP.
 *
 * EDGE_WEIGHT_TYPE : <string>
 * Specifies how the edge weights (or distances) are given. The values are:
 * ATT          Special distance function for problem att48 and att532
 * CEIL_2D      Weights are Euclidean distances in 2-D rounded up
 * CEIL_3D      Weights are Euclidean distances in 3-D rounded up
 * EUC_2D       Weights are Euclidean distances in 2-D
 * EUC_3D       Weights are Euclidean distances in 3-D
 * EXACT_2D     Weights are EUC_2D distances (SCALE = 1000 as default)
 * EXACT_3D     Weights are EUC_3D distances (SCALE = 1000 as default)
 * EXPLICIT     Weights are listed explicitly in the corresponding section
 * FLOOR_2D     Weights are Euclidean distances in 2-D rounded down
 * FLOOR_3D     Weights are Euclidean distances in 3-D rounded down
 * GEO          Weights are geographical distances in kilometers (TSPLIB)
 *              Coordinates are given in the form DDD.MM where DDD are the
 *              degrees and MM the minutes
 * GEOM         Weights are geographical distances in meters (used for the
 *              world TSP). Coordinates are given in decimal form
 * GEO_MEEUS    Weights are geographical distances in kilometers, computed
 *              according to Meeus' formula.  Coordinates are given in the
 *              form DDD.MM where DDD are the degrees and MM the minutes
 * GEOM_MEEUS   Weights are geographical distances, computed according to
 *              Meeus' formula. Coordinates are given in decimal form
 * MAN_2D       Weights are Manhattan distances in 2-D
 * MAN_3D       Weights are Manhattan distances in 3-D
 * MAX_2D       Weights are maximum distances in 2-D
 * MAX_3D       Weights are maximum distances in 3-D
 * TOR_2D       Wirghes are toroidal distances in 2-D
 * TOR_3D       Wirghes are toroidal distances in 3-D
 * XRAY1        Distance function for crystallography problems (Version 1)
 * XRAY2        Distance function for crystallography problems (Version 2)
 * SPECIAL      There is a special distance function implemented in
 *              the Distance_SPECIAL function.
 *
 * EDGE-WEIGHT_FORMAT : <string>
 * Describes the format of the edge weights if they are given explicitly.
 * The values are
 * FUNCTION         Weights are given by a function (see above)
 * FULL_MATRIX      Weights are given by a full matrix
 * UPPER_ROW        Upper triangular matrix
 *                      (row-wise without diagonal entries)
 * LOWER_ROW        Lower triangular matrix
 *                      (row-wise without diagonal entries)
 * UPPER_DIAG_ROW   Upper triangular matrix
 *                      (row-wise including diagonal entries)
 * LOWER_DIAG_ROW   Lower triangular matrix
 *                      (row-wise including diagonal entries)
 * UPPER_COL        Upper triangular matrix
 *                      (column-wise without diagonal entries)
 * LOWER_COL        Lower triangular matrix
 *                      (column-wise without diagonal entries)
 * UPPER_DIAG_COL   Upper triangular matrix
 *                      (column-wise including diagonal entries)
 * LOWER_DIAG_COL   Lower triangular matrix
 *                      (column-wise including diagonal entries)
 *
 * EDGE_DATA_FORMAT : <string>
 * Describes the format in which the edges of a graph are given, if the
 * graph is not complete. The values are
 * EDGE_LIST    The graph is given by an edge list
 * ADJ_LIST     The graph is given by an adjacency list
 *
 * NODE_COORD_TYPE : <string>
 * Specifies whether the coordinates are associated with each node
 * (which, for example may be used for either graphical display or
 * distance computations.
 * The values are
 * TWOD_COORDS      Nodes are specified by coordinates in 2-D
 * THREED_COORDS    Nodes are specified by coordinates in 3-D
 * NO_COORDS        The nodes do not have associated coordinates
 * The default value is NO_COORDS. In the current implementation, however,
 * the value has no significance.
 *
 * DISPLAY_DATA_TYPE : <string>
 * Specifies how a graphical display of the nodes can be obtained.
 * The values are
 * COORD_DISPLAY    Display is generated from the node coordinates
 * TWOD_DISPLAY     Explicit coordinates in 2-D are given
 * NO_DISPLAY       No graphical display is possible
 *
 * The default value is COORD_DISPLAY if node coordinates are specifies and
 * NO_DISPLAY otherwise. In the current implementation, however, the value
 * has no significance.
 *
 * DEMAND_DIMENSION : <integer>
 * The number of objects in an m1-PDTSP.
 *
 * GRID_SIZE : <real>
 * The grid size for toroidal instances.
 * Default: 1000000.0
 *
 * RISK_THRESHOLD : <integer>
 * The maximum risk alllowed for each route in an RCTVRP or RCTVRPTW instance.
 *
 * SALESMEN : <integer>
 * VEHICLES : <integer>
 * The number of vehicles/salesmen in a CVRP.
 *
 * SCALE : <integer>
 * Scale factor. Distances are multiplied by this factor.
 *
 * SERVICE_TIME : <real>
 * Same service time for all nodes.
 *
 * EOF
 * Terminates input data. The entry is optional.
 *

数据部分的格式如下,可以是
NODE_COORD_SECTION:node坐标
EDGE_DATA_SECTION:edge清单(如果是3个整数,最后一个表示weight),临近edge清单(第一个数据是当前点,后面是临近点,以-1结束)
FIXED_EDGES_SECTION:必须出现的edge清单

* NODE_COORD_SECTION :
 * Node coordinates are given in this section. Each line is of the form
 *
 *      <integer> <real> <real>
 *
 * if NODE_COORD_TYPE is TWOD_COORDS, or
 *
 *      <integer> <real> <real> <real>
 *
 * if NODE_COORD_TYPE is THREED_COORDS. The integers give the number of the
 * respective nodes. The real numbers are the associated coordinates.
 *
 * EDGE_DATA_SECTION :
 * Edges of the graph are specified in either of the two formats allowed in
 * the EDGE_DATA_FORMAT entry. If the type is EDGE_LIST, then the edges are
 * given as a sequence of lines of one of the forms
 *
 *      <integer> <integer>
 *      <integer> <integer> <integer>
 *
 * each entry giving the terminal nodes of some edge, and if three integers are
 * given, the last one specifies its weight. The list is terminated by a -1.
 * If the type is ADJ_LIST, the section consists of adjacency lists for nodes.
 * The adjacency list of a node x is specified as
 *
 *      <integer> <integer> ... <integer> -1
 *
 * where the first integer gives the number of node x and the following
 * integers (terminated by -1) the numbers of the nodes adjacent to x.
 * The list of adjacency lists are terminated by an additional -1.
 *
 * FIXED_EDGES_SECTION :
 * In this section, edges are listed that are required to appear in each
 * solution to the problem. The edges to be fixed are given in the form
 * (per line)
 *
 *      <integer> <integer>
 *
 * meaning that the edge (arc) from the first node to the second node has
 * to be contained in a solution. This section is terminated by a -1.
 *
 * DISPLAY_DATA_SECTION :
 * If DISPLAY_DATA_TYPE is TWOD_DISPLAY, the 2-dimensional coordinates from
 * which a display can be generated are given in the form (per line)
 *
 *      <integer> <real> <real>
 *
 * The integers specify the respective nodes and the real numbers give the
 * associated coordinates. The contents of this section, however, has no
 * significance in the current implementation.
 *
 * EDGE_WEIGHT_SECTION :
 * The edge weights are given in the format specifies by the EDGE_WEIGHT_FORMAT
 * entry. At present, all explicit data are integral and is given in one of the
 * (self-explanatory) matrix formats, with explicitly known lengths.
 *
 * TOUR_SECTION :
 * A tour is specified in this section. The tour is given by a list of
 * integers giving the sequence in which the nodes are visited in the tour.
 * The tour is terminated by a -1. Note: In contrast to the TSPLIB format,
 * only one tour can be given in this section. The tour is used to limit
 * the search (the last edge to be excluded in a non-gainful move must not
 * belong to the tour). In addition, the Alpha field of its edges is set to
 * -1.
 *
 * BACKHAUL_SECTION :
 * This section is used for specifying VRPB instances.
 * It contains a list of backhaul nodes. This list is terminated by a -1.
 *
 * CTSP_SET_SECTION :
 * This section is used for specifying CTSP instances.
 * Each entry has the following format:
 * c v1 v2 ... vk -1, where c is the color number (colors are numbered
 * from 1 to SALESMEN), and v1 v2 ... vk are vertices with color c
 * (vertices are numbered from 1 to Dimension).
 *
 * DEMAND_SECTION :
 * The demands of all nodes of a CVRP are given in the form (per line)
 *
 *    <integer> <integer>
 *
 * The first integer spcifies a node number, the second its demand. The depot
 * nodes must also occcur in this section. Their demands are 0.
 *
 * DEPOT_SECTION :
 * Contains a list of possible alternate depot nodes. This list is terminated
 * by a -1. The current implementation allows only one depot.
 *
 * DRAFT_LIMIT_SECTION :
 * The draft limits of all nodes of a CVRP are give in the form (per line)
 *
 *    <integer> <integer>
 *
 * The first integer spcifies a node number, the second its draft limit.
 * The depot nodes must also occcur in this section. Their demands are 0.
 *
 * PICKUP_AND_DELIVERY_SECTION :
 * This section is used for specifying specifying pickup-and-delivery
 * instances. Each line is of the form
 *
 *     <integer> <integer> <real> <real> <real> <integer> <integer>
 *
 * The first integer gives the number of the node.
 * The second integer gives its demand (ignored for PDTSPF, PDTSPL, VRPMPD
 * and VRPSPD instances).
 * The third and fourth number give the earliest and latest time for the node.
 * The fifth number specifies the service time for the node.
 * The last two integers are used to specify pickup and delivery. For a PDPTW,
 * PDTSP, PDTSPF and PDTSPL instance, the first of these integers gives the
 * index of the pickup sibling, whereas the second integer gives the index of
 * the delivery sibling. For a VRPMPD and VRPSPD instance, the two integers
 * simply give the size of the pickup and delivery for the node.
 *
 * REIQUIRED_NODES_SECTION :
 * Contains a list of required nodes for a Steiner traveling salesman problem.
 * This list is terminated * by a -1.
 *
 * SERVICE_TIME_SECTION :
 * The service times of all nodes of a CVRP are given in the form (per line)
 *
 *    <integer> <real>
 *
 * The integer specifies a node number, the real its service time.
 * The depot node must also occur in this section. Its service time is 0.
 *
 * TIME_WINDOW_SECTION :
 * Time windows are given in this section. Each line is of the form
 *
 *      <integer> <real> <real>
 *
 * The first integer specifies a node number. The two reals specify
 * earliest and latest arrival time for the node, respectively.

2.2 示例

直接使用点坐标(node_coord_section):
在这里插入图片描述
使用距离矩阵,我们以lower diag row为例,代码如下,
在这里插入图片描述
示例如下:
在这里插入图片描述
如果想要固定某些边,可以参考下面的代码,在tsp文件中添加fixed edge section
在这里插入图片描述

2.3 示例2

上面是通过读写文件的方式来传递问题数据的,下面说下直接通过库文件传递问题数据的方法。
首先在LKHmain.c中添加函数,例如double run_matrix(int* distM, int Dim),然后将readParameters和readProblem两个函数的内容修改后粘贴进函数中。读取数据的部分修改为从内存中读取:

在这里插入图片描述

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/628029.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

请问python如何处理url带有“?”参数的接口?

在Python中处理带有"?"参数的URL接口&#xff0c;可以使用urllib.parse库中的urlencode()函数来进行编码。以下是一些示例代码 如果你想学习自动化测试&#xff0c;我这边给你推荐一套视频&#xff0c;这个视频可以说是B站百万播放全网第一的自动化测试教程&#x…

详解python排序的5种高级用法

来源&#xff1a;投稿 作者&#xff1a;Fairy 编辑&#xff1a;学姐 排序是编程中常用的操作之一。Python提供了多种排序方法&#xff0c;可以适用于不同的排序需求。 那么&#xff0c;今天我们将介绍Python中常用的 5 种列表排序方法。 「1.使用sorted()函数和lambda表达式」…

常见的JS存储方式及其特点

在前端开发中&#xff0c;经常需要在浏览器端存储和管理数据。为了实现数据的持久化存储和方便的访问&#xff0c;JavaScript提供了多种数据存储方式。本文将介绍几种常见的前端JS数据存储方式及其特点。 1. Cookie Cookie是一种小型的文本文件&#xff0c;由浏览器保存在用户…

VMware Workstation下载与安装(适用于在官网注册好账号的朋友,许可证秘钥请自行网上搜索获取)

一、VMware Workstation下载 第一步&#xff1a;点击下方“Resources”链接&#xff0c;进入Vmware官网&#xff08;本教程适用于已经在官网注册好账号并已经进行登陆的朋友&#xff09;&#xff1a; Resources 第二步&#xff1a;点击“Product Downloads”&#xff0c;进入…

GIT分支管理(随笔)

目录 前言 一、概念原理 1、分支 2、原理 说明1&#xff1a; 说明2&#xff1a; 二、分支操作 1、查看分支 2、创建分支 3、切换分支 4、删除分支 5、合并分支 6、合并冲突 7、重命名分支 8、编制分支的介绍 三、标签 1、概念 2、操作 总结 前言 分支&#xff0…

C语言---关键字static的初步剖析

&#x1f680;write in front&#x1f680; &#x1f4dd;个人主页&#xff1a;认真写博客的夏目浅石. &#x1f381;欢迎各位→点赞&#x1f44d; 收藏⭐️ 留言&#x1f4dd; &#x1f4e3;系列专栏&#xff1a;凡人修C传 &#x1f4ac;总结&#xff1a;希望你看完之后&…

数据结构--队列2--双端队列--java双端队列

介绍 双端队列&#xff0c;和前面学的队列和栈的区别在于双端队列2端都可以进行增删&#xff0c;其他2个都是只能一端可以增/删。 实现 链表 因为2端都需要可以操作所以我们使用双向链表 我们也需要一共头节点 所以节点设置 static class Node<E>{E value;Node<E…

优化设备管理,提升企业效益——工程设备管理模板的实用价值分析

随着建筑行业的不断发展&#xff0c;建筑企业在施工过程中的各种设备也越来越多&#xff0c;设备的管理已经成为建筑企业发展过程中必须要面对的一个问题。作为低代码开发平台&#xff0c;百数根据建筑业的实际需求搭建了一款工程设备管理系统&#xff0c;从而能够让建筑行业企…

图漾相机—windows- Python SDK(官网下载编译)

文章目录 一、 安装依赖&#xff1a;二. 下载swig和SDK&#xff1a;swig下载连接&#xff1a;[https://www.swig.org/](https://www.swig.org/)下载python SDK下载 Windows Camport3 SDK 三、配置python和swig环境变量编译前&#xff0c;请先&#xff1a;安装 Python。 安装 Nu…

智慧垃圾焚烧发电厂Web3D可视化管理系统

前言 随着我国生产力的飞速发展和经济的迅速崛起&#xff0c;城市生活垃圾作为一种生物质能&#xff0c;将其燃烧用于发电&#xff0c;可以有效节约化石能源。 建设背景 随着城镇化进程加速、人民生活水平持续提升,城市生活垃圾产生量也在逐年增长。生活垃圾是“放错地方的资…

Kuboard

安装 Kuboard 之前&#xff0c;假设&#xff1a; 您已经准备好了一个 Linux 服务器用于安装 Kuboard-V3&#xff0c;并且该机器上的 docker 版本不低于 19.03用于安装 Kuboard v3.x 的机器已经安装了 docker&#xff0c;并且版本不低于 docker 19.03您已经有自己的 Kubernetes…

XXL-JOB任务分片

文章目录 任务类型任务配置路由策略阻塞处理策略&#xff1a; 单个任务和集群任务单机多任务分片集群分片 任务类型 单个任务&#xff1a;一个任务实例便可完成 单机单任务&#xff1a;单机模式下任何路由模式都只有一个实例执行 集群单任务&#xff1a;由路由策略(广播模式除…

接口自动化测试之Fiddler的运用

1.接口介绍&#xff08;基础部分&#xff09; 接口是一种用来定义程序的协议&#xff0c;它描述可属于任何类或结构的一组相关行为应用程序编程接口&#xff0c;它是一些预先定义的函数&#xff0c;目的是提供应用程序与开发人员基于某软件或硬件得以访问一组例程的能力接口测…

开源项目合集....

likeshop开源商城系统&#xff0c;公众号商城、H5商城、微信小程序商城、抖音小程序商城、字节小程序商城、头条小程序商城、安卓App商城、苹果App商城代码全开源&#xff0c;免费商用。 适用场景&#xff1a;B2C商城、新零售商城、社交电商商城、分销系统商城、小程序商城、商…

Go单元测试及框架使用

Go自带测试框架 单元测试 建议Go 语言推荐测试文件和源代码文件放在一块&#xff0c;测试文件以 _test.go 结尾。函数名必须以 Test 开头&#xff0c;后面一般跟待测试的函数名参数为 t *testing.T 简单测试用例定义如下&#xff1a; func TestXXXX(t *testing.T) {// ...}…

城市NOA转向BEV,头部Tier 1如何笑傲江湖?

主讲&#xff5c;蒋沁宏 编辑&#xff5c;Amy 编者注&#xff1a; 本文是HiEV出品的系列直播「硬核拆解BEV」第三期问答环节内容整理。商汤绝影量产行车智能驾驶研发负责人蒋沁宏&#xff0c;与连线嘉宾寒武纪行歌自动驾驶总监李想、宏景智驾高级工程经理柴可宁、主持嘉宾周琳…

SpringBoot 源码分析初始化应用上下文(1)-createApplicationContext

前言&#xff1a;springBoot的版本是 2.2.4.RELEASE 一、入口 /*** Run the Spring application, creating and refreshing a new* {link ApplicationContext}.* param args the application arguments (usually passed from a Java main method)* return a running {link A…

2023最新版Java 面试突击手册开源(涵盖 p5-p8 技术栈)

前言: 本文收集整理了各大厂常见面试题N道&#xff0c;你想要的这里都有内容涵盖&#xff1a;Java、MyBatis、ZooKeeper、Dubbo、Elasticsearch、Memcached、Redis、MySQL、Spring、Spring Boot、Spring Cloud、RabbitMQ、Kafka、Linux 等技术栈&#xff0c;希望大家都能找到适…

【免配置】Qt的mingw使用编译opencv库

【免配置】Qt的mingw_32/64使用编译opencv库 网上在qt中使用mingw编译器配置opencv的时候&#xff0c;通常需要使用cmake编译工具&#xff0c;进行预先编译&#xff0c;步骤比较繁琐&#xff0c;这里推荐一个捷径&#xff0c;直接使用前人编译好的opencv库即可&#xff0c;避免…

软件测试金融项目,在测试的时候一定要避开的一些雷区

软件测试金融项目需要格外谨慎和专注&#xff0c;因为这些项目通常涉及大量的交易、用户隐私和其他敏感信息。以下是一些软件测试金融项目时需要关注的方面&#xff1a; 1. 数据保护 在测试金融项目时&#xff0c;必须确保用户数据和投资信息得到保护。测试人员必须确保测试环…