百科.dev
全部条目在线工具AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
P

pyconcorde

> 编程语言
开源

Python 是 Concorde TSP 解算器的封装

398 stars0 点赞2 次浏览
访问官网GitHub

工具介绍

Python 是 Concorde TSP 解算器的封装

PyConcorde

What is it?

PyConcorde is a Python wrapper around the Concorde TSP solver.

PyConcorde allows you to compute solutions to the Traveling Salesman Problem with just a few lines of Python code. It uses the state-of-the-art Concorde solver and provides a convenient Python layer around it.

Note: until commit e065497 (pre version 0.1) PyConcorde was called PyTSP. It was renamed to emphasize the central role of the underlying Concorde solver.

How do I install it?

PyConcorde runs under Python 3.9 and higher and requires a UNIX-like OS (Native Windows not supported, but it can be run under WSL). It needs the Concorde TSP solver and QSOpt linear programming library. Further instructions on building/downloading those can be found below.

Using uv (recommended)

Clone the repository and sync dependencies:

bash
git clone https://github.com/jvkersch/pyconcorde
cd pyconcorde
uv sync

Using pip

Alternatively, you can install with pip:

bash
pip install -e .

Or, all in one line:

bash
pip install 'pyconcorde @ git+https://github.com/jvkersch/pyconcorde'

Notes

This will download and build Concorde (and its dependency QSOpt) and then build PyConcorde. While this may take a few minutes, downloading Concorde only happens the first time the install script is run (unless you remove the data directory).

If you already have Concorde and/or QSOpt installed (or an equivalent linear solver), you can use those instead. Simply set the environment variable CONCORDE_DIR (or QSOPT_DIR) to point to the folder where you installed Concorde (or QSOpt).

Detailed instructions to build Concorde can be found at this web page.

What can I do with it?

PyConcorde is a very light-weight library. The main entry point is the TSPSolver class. Here we use it to read in the Berlin52 dataset, a dataset of 52 locations in Berlin (part of the TSPlib test data).

python
    >>> from concorde.tsp import TSPSolver
    >>> from concorde.tests.data_utils import get_dataset_path
    >>> fname = get_dataset_path("berlin52")
    >>> solver = TSPSolver.from_tspfile(fname)
    Problem Name: berlin52
    Problem Type: TSP
    52 locations in Berlin (Groetschel)
    Number of Nodes: 52
    Rounded Euclidean Norm (CC_EUCLIDEAN)

As you can see above, PyConcorde (or rather, Concorde) is somewhat chatty and will print various message to the standard output. Now that we have a solver instance, let's compute a solution. On my machine this is almost instantaneous.

python
    >>> solution = solver.solve()
    (... output snipped for brevity ...)

Again, Concorde will display a variety of messages while it's running. The end result is a ComputedTour object called solution with information about the tour that we just computed:

python
    
    >>> solution.found_tour
    True
    >>> solution.optimal_value
    7542.0
    >>> solution.tour
    array([ 0, 48, 31, 44, 18, 40,  7,  8,  9, 42, 32, 50, 10, 51, 13, 12, 46,
           25, 26, 27, 11, 24,  3,  5, 14,  4, 23, 47, 37, 36, 39, 38, 35, 34,
           33, 43, 45, 15, 28, 49, 19, 22, 29,  1,  6, 41, 20, 16,  2, 17, 30,
           21], dtype=int32)
    

Other TSP solvers for Python

Several other TSP implementations exist. The following are written in pure Python:

  • tsp-solver
  • python-tsp

If you have another TSP algorithm that can be called from within Python, and you want to have it added here, please open an issue.

Technical Notes

Coordinate scaling: Concorde rounds all edge distances to the nearest integer, as required by the TSPLIB specification. If your coordinates are small (e.g. in [0, 1]), distances between points will round to 0, leading to incorrect results or crashes. Scale your coordinates up before passing them to the solver — for example, multiply by 1e6. Similarly, very large coordinates (above 1e7) may cause integer overflow. See examples/truncation_demo.py for a demonstration.

PyConcorde needs Concorde and QSOpt. Downloading and building these packages should happen automatically on Linux/Mac OS, but please file an issue if you experience any trouble during this step.

Note that Windows is currently unsupported. If you get the library to work on Windows, please open a ticket to describe any tweaks (or better yet, a Pull Request).

License

PyConcorde is licensed under the Modified BSD license. Note that Concorde and QSOpt are released under different licenses, and that PyConcorde does not include any code from these packages.

Issues· 17 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

Python

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月17日
分类编程语言
定价开源

> 相关工具

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言