使用python解决TSP(旅行商问题)

时间:2023-01-13 23:49:46

这段时间,因为要交一篇关于旅行商问题的作业,所以在github上搜索了一下,觉得用python解决比较方便,所以给大家简单的介绍一下如何使用所给的代码:

用python实现的TSP源码:  GitHub链接:https://github.com/eldrtimo/python-salesman    百度云盘链接:链接:http://pan.baidu.com/s/1i57BhGx 密码:2w4b

这段代码使用python3实现的,可以在windows的dos环境下运行,而且还要借助于numpy(它是一种开源的python数值计算扩展)。所以必须要安装python3 和 相应的numpy;


1.安装python3

2.配置python的环境变量:把python安装路径添加到path环境变量中即可;

3.安装numpy;如果已安装python,会自动识别出python安装路径,直接点next即可。

这一切准备工作结束后,然后进入dos环境(win+r,输入cmd回车),进入源代码(python-salesman-master)所在目录,我这里是E盘,输入以下命令运行即可:


运行的命令:python main.py -nfi tspfiles


运行的结果部分截图如下:

使用python解决TSP(旅行商问题)


命令中的参数介绍:

python main.py --help

usage: main.py [-h] [-n] [-f] [-i] [-p] PATH [PATH ...]

Parse TSP files and calculate paths using simple algorithms.

positional arguments:
PATH Path to directory or .tsp file. If PATH is a directory,
run on all .tsp files in the directory.

optional arguments:
-h, --help show this help message and exit
-n, --nearest calculate distance traveled by nearest neighbor heuristic
-f, --furthest calculate distance traveled by furthest insertion
heuristic
-i, --in-order calculate the distance traveled by the in-order-tour
[1..n,1]
-p, --print-tours print explicit tours