2-Opt Search Algorithm
In optimization, 2-opt is a simple local search algorithm with a 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. For more detailed description, you can read this article: How to Solve the Traveling Salesman Problem — A Comparative Analysis
Install
The module requires the following libraries:
- numpy
- random2
Then, it can be installed using pip:
pip install py2opt
Usage
To use this module, you must have a distance matrix dist_mat showing the pair distance among all nodes. Then, the
first thing to do is create an instance of the RouteFinder class. When you call the solve method, you will get the
best_route and best_distance of this problem. Check out the example below.
from py2opt.routefinder import RouteFinder
cities_names = ['A', 'B', 'C', 'D']
dist_mat = [[0, 29, 15, 35], [29, 0, 57, 42], [15, 57, 0, 61], [35, 42, 61, 0]]
route_finder = RouteFinder(dist_mat, cities_names, iterations=5)
best_distance, best_route = route_finder.solve()
print(best_distance)
114
print(best_route)
['A', 'C', 'B', 'D']
The solver finds out the optimum order (re: minimum total distance traveled) in which the nodes must be visited along with the total distance traveled. Note that the 2-opt algorithm doesn't guarantee the global optimum similar to other heuristic search algorithms. So, the results can vary in each iteration.
And that's pretty much it!
Metadata
Release files for py2opt 1.4.2
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| py2opt-1.4.2.tar.gz | 6.1 kB | Without provenance | Details for py2opt-1.4.2.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| py2opt-1.4.2-py3-none-any.whl | Python 3 | none | any | Details for py2opt-1.4.2-py3-none-any.whl |
Total release size: 12.9 kB
Release files / py2opt-1.4.2.tar.gz
| Download URL | py2opt-1.4.2.tar.gz |
|---|---|
| Size | 6.1 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
71bfa93c3b21806bc488388f7485d948a3b40f7b56c9da62b757dd5acf22b8c1
|
|
BLAKE2b-256 checksum How to use checksums |
2dcd09ffb13fe186be4ecf6c4393586caf69f73f1eeac4224550631d0af65df3
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.2 CPython/3.9.6
|
Release files / py2opt-1.4.2-py3-none-any.whl
| Download URL | py2opt-1.4.2-py3-none-any.whl |
|---|---|
| Size | 6.8 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
7cc7369a8f32a65a47fb28f4fc7c343705578e83202e6c43acb62f6b2758b231
|
|
BLAKE2b-256 checksum How to use checksums |
b62f6463deb8f4b60a1fcaf2a8e99df109676334b550289c13ab51c3ec650296
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.2 CPython/3.9.6
|
Security for py2opt 1.4.2
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.