Package: tsp-solver2
Suboptimal Travelling Salesman Problem (TSP) solverIn pure Python.This project provides a pure Python code for searching sub-optimal solutions to the TSP.
Additionally, demonstration scripts for visualization of results are provided.The library does not requires any libraries, but demo scripts require:NumpyPIL (Python imaging library)MatplotlibThe library works under both Python 2 and 3.Modules provided:tsp_solver.greedy: Basic greedy TSP solver in Pythontsp_solver.greedy_numpy: Version that uses Numpy matrices, which reduces memory use, but performance is several percents lowertsp_solver.demo: Code for the demo applicaitonScripts provideddemo_tsp: Generates random TSP, solves it and visualises the result. Optionally, result can be saved to the numpy-format file.tsp_numpy2svg: Generates neat SVG image from the numpy file, generated by thedemo_tsp.Both applications support a variety of command-line keys, run them with --help option to see additional info.InstallationInstall from PyPi:# pip install tsp_solver2or$pipinstall--usertsp_solver2(Note tahttsp_solverpackage contains an older version).Manual installation:# python setup.py installAlternatively, you may simply copy the tsp_solver/greedy.py to your project.UsageThe library provides a greedy solver for the symmetric TSP.
Basic usage is:fromtsp_solver.greedyimportsolve_tsp#Prepare the square symmetric distance matrix for 3 nodes:#  Distance from A to B is 1.0#                B to C is 3.0#                A to C is 2.0D=[[],[1.0],[2.0,3.0]]path=solve_tsp(D)#will print [1,0,2], path with total length of 3.0 unitsprint(path)The triangular matrixDin the above example represents the following graph with three nodes A, B, and C:Square matrix may be provided, but only left triangular part is used from it.Utility functionstsp_solver.util.path_cost(distance_matrix, path)Caclulate total length of the given path, using the provided distance matrix.Using fixed endpointsIt is also possible to manually specify desired start and/or end nodes of the path. Note that this would usually increase total length of the path.
Example, using the same distance matrix as above, but now requiring that path starts at A (index 0) and ends at C (index 2):D=[[],[1.0],[2.0,3.0]]path=solve_tsp(D,endpoints=(0,2))#will print path [0,1,2]print(path)New in version 0.4: it is not possible to specify only one of two end points:solve_tsp(D,endpoints=(None,2))solve_tsp(D,endpoints=(0,None))Round trip pathsTo find a round trip path, that returns to the starting node, specify the same value to both endpoints:path=solve_tsp(D,endpoints=(0,0))#will print path [0,1,2,0]print(path)Note that round trip paths are one step longer.Neither solution quality nor complexity depends on the endpoints specified, so it is safe to use (0,0) when don't care.AlgorithmThe library implements a simple "greedy" algorithm:Initially, each vertex belongs to its own path fragment. Each path fragment has length 1.Find 2 nearest disconnected path fragments and connect them.Repeat, until there are at least 2 path fragments.This algorightm has polynomial complexity.OptimizationGreedy algorithm sometimes produces highly non-optimal solutions. To solve this,optimizationis provided. It tries to rearrange points in the paths to improve the solution. One optimization pass has O(n^4) complexity. Note that even unlimited number of optimization paths does not guarantees to find the optimal solution.PerformanceThis library neither implements a state-of-the-art algorithm, nor it is tuned for a high performance.It however can find a decent suboptimal solution for the TSP with 4000 points in several minutes. The biggest practical limitation is memory: O(n^2) memory is used.DemoTo see a demonstration, run$makedemowithout installation. The demo requiresNumpyandMatplotlibpython libraries to be installed.TestingTo execute unit tests, run$maketestChange logVersion 0.4.1Added possibility to search for round trip paths, when endpoints coincide.Version 0.4Added possibility to specify only one of end points.