Skip to main content

Super simple Python wrapper for LKH-3

Project description

PyLKH

This is a super simple Python wrapper for the constrained traveling salesman and vehicle routing problem solver called LKH-3.

If you want to use this wrapper, you need to install LKH-3 first. For example, on Ubuntu:

wget http://akira.ruc.dk/~keld/research/LKH-3/LKH-3.0.6.tgz
tar xvfz LKH-3.0.6.tgz
cd LKH-3.0.6
make
sudo cp LKH /usr/local/bin

LKH-3 expects problems in the TSPLIB format. It extends the format to support VRPs.

Using PyLKH you can solve problems represented as Python objects or files.

CAUTION: distances are represented by integer values in the TSPLIB format. This can produce unexpected behaviour for some problems, like those with all nodes within the unit square. You can use the EXACT_2D distance to avoid rounding issues.

Install

pip install lkh

Example

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.6/LKH'
lkh.solve(solver_path, problem=problem, max_trials=10000, runs=10)

Output (values correspond to nodes, which are 1-indexed, not node indicies, which are 0-indexed):

[[27, 8, 14, 18, 20, 32, 22],
 [25, 28],
 [15, 29, 12, 5, 24, 4, 3, 7],
 [30, 19, 9, 10, 23, 16, 11, 26, 6, 21],
 [13, 2, 17, 31]]

API

lkh.solve(solver='LKH', problem=None, problem_file=None, **kwargs)

Solve a problem instance.

Parameters

  • solver (optional): Path to LKH-3 executable. Defaults to LKH.

  • problem (optional): Problem object. LKHProblem is preferred but tsplib95.models.StandardProblem also works. problem or problem_file is required.

  • problem_file (optional): Path to TSPLIB-formatted problem. problem or problem_file is required.

  • kwargs (optional): Any LKH-3 parameter described here (pg. 5-7) or here (pg. 6-8). Lowercase works. For example: runs=10.

Returns

routes: List of lists of nodes (nodes, not node indicies).

class lkh.LKHProblem

A problem that can be solved by LKH-3. Fields are (partially) described here (pg. 4-6). Inherits from tsplib95.models.StandardProblem.

The available specification fields are:

  • CAPACITY
  • COMMENT
  • DEMAND_DIMENSION
  • DIMENSION
  • DISPLAY_DATA_TYPE
  • DISTANCE
  • EDGE_DATA_FORMAT
  • EDGE_WEIGHT_FORMAT
  • EDGE_WEIGHT_TYPE
  • NAME
  • NODE_COORD_TYPE
  • RISK_THRESHOLD
  • SALESMEN
  • SCALE
  • SERVICE_TIME
  • TYPE
  • VEHICLES

The available data fields are:

  • BACKHAUL_SECTION
  • CTSP_SET_SECTION
  • DEMAND_SECTION
  • DEPOT_SECTION
  • DISPLAY_DATA_SECTION
  • DRAFT_LIMIT_SECTION
  • EDGE_DATA_SECTION
  • EDGE_WEIGHT_SECTION
  • FIXED_EDGES_SECTION
  • NODE_COORD_SECTION
  • PICKUP_AND_DELIVERY_SECTION
  • REQUIRED_NODES_SECTION
  • SERVICE_TIME_SECTION
  • TIME_WINDOW_SECTION

You probably want to initialize a problem instance using one of the following class methods:

classmethod load(filepath, **options)

Load a problem instance from a text file.

Inherited from tsplib95.problems.Problem.load.

classmethod parse(text, **options)

Parse text into a problem instance.

Inherited from tsplib95.problems.Problem.parse.

classmethod read(fp, **options)

Read a problem instance from a file-like object.

Inherited from tsplib95.problems.Problem.read.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

lkh-2.0.0-py3-none-any.whl (24.2 kB view details)

Uploaded Python 3

File details

Details for the file lkh-2.0.0-py3-none-any.whl.

File metadata

  • Download URL: lkh-2.0.0-py3-none-any.whl
  • Upload date:
  • Size: 24.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.10.12

File hashes

Hashes for lkh-2.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 4f0086b18f616afe46c67cf1acfbfbae5c3dc57c54fe49fab482a034782efb9b
MD5 0e56b7a87aa43f83a3c142f9d3eb9dfb
BLAKE2b-256 c673bc14b1c26703919425035a6c024d7014fd0a8a2b581f61b83bf2136bcae8

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page