Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/dmishin/tsp-solver
Browse files Browse the repository at this point in the history
  • Loading branch information
dmishin committed May 29, 2018
2 parents 5679fc1 + 1fb3722 commit 81ff0ff
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 16 deletions.
20 changes: 18 additions & 2 deletions README.md
Expand Up @@ -73,11 +73,27 @@ The triangular matrix `D` in the above example represents the following graph wi
Square matrix may be provided, but only left triangular part is used from it.

### Utility functions

*tsp_solver.util.path_cost(distance_matrix, path)*

Caclulate total length of the given path, using the provided distance matrix.

### Using fixed endpoints
It is also possible to manually specify desired start and 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):

```python

D = [[],
[1.0],
[2.0, 3.0]]

path = solve_tsp( D, endpoints = (0,2) )

#will print path [0,1,2]
print(path)
```

Endpoints must be different.

Algorithm
---------

Expand Down
2 changes: 1 addition & 1 deletion setup.py
Expand Up @@ -3,7 +3,7 @@
from distutils.core import setup

setup(name='tsp_solver2',
version='0.2',
version='0.3',
description='Greedy, suboptimal solver for the Travelling Salesman Problem',
author='Dmitry Shintyakov',
author_email='shintyakov@gmail.com',
Expand Down
19 changes: 11 additions & 8 deletions tsp_solver/demo/numpy2svg.py
@@ -1,10 +1,13 @@
from __future__ import with_statement, print_function
from __future__ import print_function
import os
import math
import numpy as np
from xml.etree import ElementTree as et
from itertools import izip
from optparse import OptionParser
try:
from itertools import izip as zip
except ImportError:
pass

def dbl2str( f ):
return str(int(f))
Expand Down Expand Up @@ -48,7 +51,7 @@ def GenerateSVGContour(output, x,y, palette, options=None, margin=10, out_size =
real_h = int((y1-y0)*scale + 2*margin)

def xy_seq():
return izip(x,y)
return zip(x,y)

# create an SVG XML element
doc = et.Element('svg',
Expand All @@ -71,11 +74,11 @@ def xy_seq():
r=circle_radius, fill='rgb(%d,%d,%d)'%palette.at(float(i)/len(x)))

# ElementTree 1.2 doesn't write the SVG file header errata, so do that manually
with file(output, 'w') as f:
with open(output, 'wb') as f:
if svg_header:
f.write('<?xml version=\"1.0\" standalone=\"no\"?>\n')
f.write('<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\"\n')
f.write('\"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">\n')
f.write(b'<?xml version=\"1.0\" standalone=\"no\"?>\n')
f.write(b'<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\"\n')
f.write(b'\"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">\n')
f.write(et.tostring(doc))


Expand Down Expand Up @@ -152,7 +155,7 @@ def main():
for in_file in args:
out_file = options.output or os.path.splitext(in_file)[0]+".svg"
try:
with file(in_file,"r") as fl:
with open(in_file,"rb") as fl:
xy = np.load( fl )
x=xy[0,:]
y=xy[1,:]
Expand Down
4 changes: 2 additions & 2 deletions tsp_solver/demo/tsp.py
Expand Up @@ -32,7 +32,7 @@ def spot_points( N ):
return xy

def box_points( N ):
xy = np.random.rand( (2,N) )
xy = np.random.rand(2,N)
return xy

def image_points( N, src_image ):
Expand Down Expand Up @@ -101,7 +101,7 @@ def main():

if options.output:
try:
with file( options.output, "w") as fl:
with open( options.output, "wb") as fl:
np.save( fl, xy[:,path])
print ("Saved file %s"%(options.output))
except IOError as err:
Expand Down
6 changes: 3 additions & 3 deletions tsp_solver/greedy.py
Expand Up @@ -87,10 +87,10 @@ def pairs_by_dist(N, distances):
indices = []
for i in xrange(N):
for j in xrange(i):
indices.append((i,j))
indices.append(i*N+j)

indices.sort(key = lambda ij: distances[ij[0]][ij[1]])
return indices
indices.sort(key = lambda ij: distances[ij//N][ij%N])
return ((ij//N,ij%N) for ij in indices)

def solve_tsp( distances, optim_steps=3, pairs_by_dist=pairs_by_dist, endpoints=None ):
"""Given a distance matrix, finds a solution for the TSP problem.
Expand Down

0 comments on commit 81ff0ff

Please sign in to comment.