Skip to main content

2-Opt Search Algorithm

In optimization, 2-opt is a simple local search algorithm with special swapping mechanism that suits well to solve the traveling salesman problem. This algorithm is sensitive to the initial point of search, i.e., its final results get changed by different initial points. 2-opt runs very fast such that a tsp with 120 cities can be solved in less than 5 sec on the intel core i7. To get a more reliable result, you should run the 2-opt with different randomized initial points for enough number of times. One more thing, the travelling salesman problem has many applications in real world such as logistic planning or DNA sequencing. So, having a fast and simple method to solve the TSP is valuable.

Library

The library requires the following libraries:

  • Numpy
  • Random
  • Time

Install

It can be installed using pip: ''' pip install py2opt ''' Alternatively it can be intalled via conda:

''' conda install -c conda-forge py2opt '''

Usage

To use this library, you must have a distance matrix showing distance among all First thing to do is create an instance of the RouteFinder class, which reads in a corpus of text and does some indexing on it

Note that this package doesn't do calculate distance matrix in this version.

The data:

nodes = ['A', 'B', 'C', 'D']
dist_mat = [[0, 2, 5, 3], [2, 0, 7, 2], [5, 7, 0, 1], [3, 9, 1, 0 ]]

The format of the output should be (note that this is not an optimal route):

Vancouver
Calgary
Edmonton
Seattle
Portland
Los Angeles
San Francisco
Denver
5523 km

And that's pretty much it!

Metadata

Release files for py2opt 1.0.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for py2opt 1.0.1
File Size Uploaded Provenance Actions
py2opt-1.0.1.tar.gz 4.8 kB Without provenance Details for py2opt-1.0.1.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for py2opt 1.0.1
File Interpreter ABI Platform Actions
py2opt-1.0.1-py3-none-any.whl Python 3 none any Details for py2opt-1.0.1-py3-none-any.whl

Total release size: 10.1 kB

Release files / py2opt-1.0.1.tar.gz

Download URL py2opt-1.0.1.tar.gz
Size 4.8 kB
Tags Source
SHA-256 checksum
How to use checksums
c7a4f107c6fb5a853f895443640257aa12b5a2de3220c2c81d15b0a307bd723a
BLAKE2b-256 checksum
How to use checksums
808b884e28203e5b7a37c4c3f0f97ec32e20761cf5622448788b5e56641b5941
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/49.2.0.post20200714 requests-toolbelt/0.9.1 tqdm/4.41.1 CPython/3.8.3

Release files / py2opt-1.0.1-py3-none-any.whl

Download URL py2opt-1.0.1-py3-none-any.whl
Size 5.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
6386b07acb4419b1e33198497f0ff05cfe3dacba95f55c72a77124822a75040e
BLAKE2b-256 checksum
How to use checksums
545b0ca6ff42261e3463aef26668d89e95323d7f0e3799ef38b18243da5d15a6
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/49.2.0.post20200714 requests-toolbelt/0.9.1 tqdm/4.41.1 CPython/3.8.3

Security for py2opt 1.0.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page