环境问题—安装TSP求解器concorde&LKH
Posted oliveQ
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了环境问题—安装TSP求解器concorde&LKH相关的知识,希望对你有一定的参考价值。
Ubuntu16安装TSP求解器
安装pyconcorde【成功】
- 下载GitHub项目pyconcorde:
-
下载到调库地址:
/home/dell/anaconda3/envs/(虚拟环境名)/lib/python3.9/site-packages
-
或者就在当前项目文件夹下,用os和sys搭配调取,如测试文件那样
-
下载命令:可以Windows下载,但是不要解压!!
git clone https://github.com/jvkersch/pyconcorde
不然安装过程会出现错误:collect2: error: ld returned 1 exit status error: command 'gcc' failed with exit status 1
-
打开文件:
cd pyconcorde
[可以]预先安装好:Cython[whl],setuptools[pip3] -
安装:
pip install -e . -i https://pypi.tuna.tsinghua.edu.cn/simple
-
【可忽略】测试代码concorde_test.py:参考
文件夹的设置如下:
—pyconcorde
—TSP
------concorde_test.py
------tsplib_dataimport os import sys os.chdir(os.path.dirname(os.path.abspath(__file__))) sys.path.insert(0, "..") # for pyconcorde from pyconcorde.concorde.tsp import TSPSolver import networkx as nx import matplotlib.pyplot as plt import os import tsplib95 from time import time def plot(problem, tour): G = problem.get_graph() if len(problem.node_coords) == 0: coord = problem.display_data else: coord = for node in G.nodes: coord[node] = problem.node_coords[node] # remove original edges G.remove_edges_from(list(G.edges)) # add edges edges = [] for i in range(len(tour) - 1): n1, n2 = tour[i], tour[i + 1] edges.append((n1, n2)) edges.append((tour[-1], tour[0])) G.add_edges_from(edges) nx.draw_networkx(G, coord, node_size=15, with_labels=False) plt.savefig("test_concorde.png", bbox_inches='tight') plt.show() def get_example_path(): path = os.path.dirname(os.path.abspath(__file__)) datapath = os.path.join(path, "tsplib_data") return datapath def run(filename, is_plot=True): datapath = get_example_path() filepath = os.path.join(datapath, filename) problem = tsplib95.load(filepath) start = time() solver = TSPSolver.from_tspfile(filepath) solution = solver.solve() end = time() print("Time spent to solve s".format(end - start)) print("Optimal value: ", solution.optimal_value) tour = solution.tour tour = [t + 1 for t in tour] plot(problem, tour) if __name__ == "__main__": run("berlin52.tsp")
安装concorde【qsopt无法链接】
-
安装命令
#!/bin/bash mkdir concorde cd concorde mkdir qsopt cd qsopt # ubuntu16 [(文件)直接下载就行] wget http://www.math.uwaterloo.ca/~bico/qsopt/beta/codes/PIC/qsopt.PIC.a wget http://www.math.uwaterloo.ca/~bico/qsopt/beta/codes/PIC/qsopt.h wget http://www.math.uwaterloo.ca/tsp/concorde/downloads/codes/src/co031219.tgz tar xf co031219.tgz # 把解码后的文件放到与qsopt相同的文件夹下 # concorde # ——qsopt # ——BIGGUY # ——configure # ——.... # 在concorde文件夹下(有configure文件)执行 bash ./configure --with-qsopt=$(pwd)/qsopt make sudo cp concorde /usr/local/bin TSP/concorde -s 99 -k 100
-
检查是否可以导入(pyconcorde->concorde)【没报错即可】
# 如果安装的是concorde from concorde.tsp import TSPSolver from concorde.tests.data_utils import get_dataset_path # 如果安装的是pyconcorde from pyconcorde.concorde.tsp import TSPSolver from pyconcorde.concorde.tests.data_utils import get_dataset_path
安装LKH3.0.7
用python包:lkh,调用LKH3.0.7C文件,参考
-
预先编译LKH相关文件,windows下载到Ubuntu解压
wget http://akira.ruc.dk/~keld/research/LKH-3/LKH-3.0.7.tgz tar xvfz LKH-3.0.7.tgz cd LKH-3.0.7 make sudo cp LKH /usr/local/bin
-
下载python包
pip3 install lkh
-
测试代码:
因为lkh库输出solution.tour,但我想要全部solution信息
于是修改了lkh.init.py问题
import requests import lkh problem_str = requests.get('http://vrp.atd-lab.inf.puc-rio.br/media/com_vrp/instances/A/A-n32-k5.vrp').text problem = lkh.LKHProblem.parse(problem_str) solver_path = '../LKH-3.0.7/LKH' solution = lkh.solve(solver_path, problem=problem, max_trials=100, runs=10)
其他
-
想看未知对象的所有属性?
obj.__dict__
- 比如solution
‘name’: ‘berlin52.7542.tour’,
‘comment’: ‘Found by LKH-3 [Keld Helsgaun] Thu Jun 30 03:52:12 2022’,
‘type’: ‘TOUR’,
‘dimension’: 52,
‘tours’: [[1, 22, 31, 18, 3, 17, 21, 42, 7, 2, 30, 23, 20, 50, 29, 16, 46, 44, 34, 35, 36, 39, 40, 37, 38, 48, 24, 5, 15, 6, 4, 25, 12, 28, 27, 26, 47, 13, 14, 52, 11, 51, 33, 43, 10, 9, 8, 41, 19, 45, 32, 49]],
‘_defaults’: ‘edge_weight_type’: None, ‘edge_weight_format’: None,
‘_wfunc’: <function StandardProblem._create_wfunc.. at 0x7fc7e9e85488>,
‘_special’: None
- 比如solution
-
出现
numpy.darray size changed,.....
因为有多个版本的numpy,或者版本不对,建议重装numpy
pip uninstall numpy
,直到numpy卸载干净
pip install numpy
,重装,之后最好重启pycharm -
concorde的结果:0.04s
-
LKH的结果:0.009s
以上是关于环境问题—安装TSP求解器concorde&LKH的主要内容,如果未能解决你的问题,请参考以下文章