当前位置: 首页 > news >正文

TSP 问题求解的最好方法 LKH

目前可以查到的最好的方法求解TSP问题是 LKH,所以本篇文章介绍如何使用Matlab 调用LKH
参考文档:

用matlab调用迄今为止最强悍的求解旅行商(TSP)的算法-LKH算法_wx6333e948c3602的技术博客_51CTO博客

【LKH算法体验】用matlab调用迄今为止最强悍的求解旅行商(TSP)的算法-LKH算法_lkh3算法_果州做题家的博客-CSDN博客

使用步骤:
首先需要提前准备好matlab的环境,然后去github 下载matlab程序:
网址链接:https://github.com/unr-arl/LKH_TSP
里面除了有matlab调用程序 还有python 的程序

下载之后将内部的函数放到当前文件夹的路径下

然后建立两个文件夹 LKH 和 TSPLIB

下载LKH 应用程序链接如下:

LKH (Keld Helsgaun)

LKH问价夹内放该程序

 然后就是编写程序就可以了
 

%已知距离矩阵,求最优解
clear,clc;
fname_tsp='test';
LKHdir=strcat(pwd,'/LKH/');
TSPLIBdir=strcat(pwd,'/TSPLIB/');
lkh_cmd=[LKHdir,'LKH',' ',TSPLIBdir,fname_tsp,'.par'];
%距离矩阵
D=[0 0 2 1 2 1 1 1 1 1 2 4 3 0 0 
0 0 0 2 1 3 1 1 1 1 1 1 3 0 0 
2 0 0 0 2 1 1 0 1 1 2 1 1 2 0 
1 2 0 0 0 2 1 1 1 1 0 1 2 1 0 
2 1 2 0 0 1 1 3 1 1 1 2 2 4 0 
1 3 1 2 1 0 0 1 1 2 1 1 2 0 0 
1 1 1 1 1 0 0 1 1 1 1 3 1 1 0 
1 1 0 1 3 1 1 0 0 0 0 2 1 2 0 
1 1 1 1 1 1 1 0 0 0 0 1 1 3 0 
1 1 1 1 1 2 1 0 0 0 0 2 1 2 0 
2 1 2 0 1 1 1 0 0 0 0 1 1 1 0 
4 1 1 1 2 1 3 2 1 2 1 0 0 1 0 
3 3 1 2 2 2 1 1 1 1 1 0 0 0 0 
0 0 2 1 4 0 1 2 3 2 1 1 0 0 0 
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0];
CostMatrix=D;
user_comment='a comment by the user';
pars_struct.CostMatrixMulFactor = 1000;
pars_struct.user_comment = user_comment;
LKH_TSP(CostMatrix,pars_struct,fname_tsp,LKHdir,TSPLIBdir)

 这里需要注意一些细节:
下载下来的LKH_TSP 可能会报错,此时需要更改
 

function TSPsolution = LKH_TSP(CostMatrix,pars_struct,fname_tsp,LKHdir,TSPLIBdir)
%
%   Syntax:
%   TSPsolution = LKH_TSP(CostMatrix,pars_struct,fname_tsp,LKHdir,TSPLIBdir)
%
%   This functions solves TSP problems using the Lin-Kernighan-Helsgaun
%   solver. It assumes that a compiled executable of the LKH solver as
%   found at: http://www.akira.ruc.dk/~keld/research/LKH/ is available at
%   the LKHdir directory. Furthermore a TSPLIB directory is assumed.
%   For the definition of the TSPLIB and the compilation of the LKH code
%   check the aforementioned url. 
%
%   Inputs:
%   CostMatrix      : the Cost Matrix of the (asymmetric) TSP. [e.g. it can be an NxN matrix of distances]
%   pars_struct     : parameters structure with
%                   : -> CostMatrixMulFactor (value that makes Cost Matrix
%                        almost integer. [eg. pars_struct.CostMatrixMulFactor = 1000; ]
%                     -> user_comment (a user comment for the problem) [optional]
%   fname_tsp       : the filename to save the tsp problem
%   LKHdir          : the directory of the LKH executable
%   TSPLIBdir       : the directory of the TSPLIB files
%   
%   Outputs:
%   TSPsolution     : the TSP solution
%   
%   Authors:
%   Kostas Alexis (kalexis@unr.edu)
%CostMatrix_tsp = pars_struct.CostMatrixMulFactor*CostMatrix;
CostMatrix_tsp = floor(CostMatrix_tsp);
user_comment = pars_struct.user_comment; % fileID = writeTSPLIBfile_FE(fname_tsp,CostMatrix_tsp,user_comment);
% writeProblemFiles(strcat(LKHdir, "/", TSPLIBdir, "/", fname_tsp),CostMatrix_tsp,pars_struct);disp('### LKH problem set-up...');
%%  Solve the TSP Problem via the LKH Heuristic
disp('### Now solving the TSP problem using the LKH Heuristic...');
copy_toTSPLIBdir_cmd = ['cp ' LKHdir fname_tsp '.txt' ' ' TSPLIBdir];start_lkh_time = cputime;
lkh_cmd = [LKHdir 'LKH' ' ' TSPLIBdir fname_tsp '.par'];[status,cmdout] = system(lkh_cmd,'-echo');
end_lkh_time = cputime;copy_toTSPLIBdir_cmd = ['cp ' LKHdir fname_tsp '.txt' ' ' TSPLIBdir];
[status,cmdout] = system(copy_toTSPLIBdir_cmd);
disp('### ... done!');
solution_file = [fname_tsp '.txt']; 
tsp_sol_cell = importdata(solution_file);% rm_solution_file_cmd = ['rm ' LKHdir fname_tsp '.txt'];
% [status,cmdout] = system(rm_solution_file_cmd);
% 
tsp_sol = [];
for i = 1:length(tsp_sol_cell.textdata)if ~isempty(str2num(tsp_sol_cell.textdata{i}))tsp_sol(end+1) = str2num(tsp_sol_cell.textdata{i});end
endtsp_sol(end) = [];TSPsolution = tsp_sol;end

https://download.csdn.net/download/qq_34995963/87541471

http://www.lryc.cn/news/32325.html

相关文章:

  • RocketMQ5.1控制台的安装与启动
  • 【java基础】类型擦除、桥方法、泛型代码和虚拟机
  • 十家公司有九家问过的软件测试面试题,最后一题我猜你肯定不会
  • C++核心知识(三)—— 静态成员(变量、函数、const成员)、面向对象模型(this指针、常函数、常对象)、友元、数组类、单例模式
  • RocketMQ【3】Rocketmq集群部署(多master多slave)异步复制
  • 魏玛早春 木心
  • 关于Scipy的概念和使用方法及实战
  • 第二章Linux操作语法1
  • linux内核调度问题分析
  • C语言-基础了解-25-C强制类型转换
  • 【Python】如何安装 Allure 工具进行自动化测试
  • nginx七大核心应用场景详解 解决生产中的实际问题 二次开发扩展
  • Java 整合 Redis
  • Django实践-03模型-02基于admin管理表
  • 如何安装python
  • java String类 万字详解(通俗易懂)
  • Hive拉链表
  • day1 开发我的第一个MyBatis程序
  • 【CDP】更改solr 存储路径导致ranger-audit 大量报错问题解决
  • JavaScript基础一、简介
  • Qt音视频开发20-vlc内核动态保存录像文件(不需要重新编译源码)
  • 【深度学习】BERT变体—RoBERTa
  • java面试准备1
  • buffer它到底做了个啥,源码级分析linux内核的文件系统的缓冲区
  • 【蓝桥杯刷题】盗版Huybery系列之手抓饼赛马
  • 【微信小程序-原生开发】实用教程16 - 查看详情(含页面跳转的传参方法--简单传参 vs 复杂传参)
  • 论文精读:Ansor: Generating High-Performance Tensor Programs for Deep Learning
  • SpringBoot With IoC,DI, AOP,自动配置
  • ggplot2的组图拓展包(1):patchwork(上篇)
  • Python 异步: 异步迭代器(15)