Skip to content

Commit

Permalink
Fixes #11
Browse files Browse the repository at this point in the history
  • Loading branch information
rhgrant10 committed Mar 31, 2020
1 parent 4ea7391 commit 80de85d
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 6 deletions.
Empty file removed tests/data/testfixture.py
Empty file.
14 changes: 13 additions & 1 deletion tests/test_distances.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# -*- coding: utf-8 -*-
import pytest

from tsplib95 import models
from tsplib95 import distances


Expand Down Expand Up @@ -56,7 +57,7 @@ def test_maximum(start, end, dist, exc):
((3, 4), (3, 4), 1, None),
((0, 0), (3, 4), 557, None),
((3, 4), (0, 0), 557, None),
((-3, -4), (3, 4), 1218, None),
((-3, -4), (3, 4), 1113, None),
((0, 1), (0, 1, 2), None, ValueError),
((0, 1, 2), (0, 1), None, ValueError),
])
Expand All @@ -66,3 +67,14 @@ def test_geographical(start, end, dist, exc):
distances.geographical(start, end)
else:
assert distances.geographical(start, end) == dist


@pytest.mark.parametrize('pfile,answer', [
('data/pcb442.tsp', 221440),
('data/gr666.tsp', 423710),
('data/att532.tsp', 309636),
])
def test_verifcation_problems(read_problem_text, pfile, answer):
problem_text = read_problem_text(pfile)
problem = models.StandardProblem.parse(problem_text)
assert problem.trace_canonical_tour() == answer
6 changes: 3 additions & 3 deletions tsplib95/distances.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ def geographical(start, end, round=utils.nint, radius=6378.388):
q3 = math.cos(start.lat + end.lat)
distance = radius * math.acos(0.5 * ((1 + q1) * q2 - (1 - q1) * q3)) + 1

return round(distance)
return int(distance)


def pseudo_euclidean(start, end, round=utils.nint):
Expand Down Expand Up @@ -127,8 +127,8 @@ def xray(start, end, sx=1, sy=1, sz=1, round=utils.icost):
'MAN_2D': manhattan,
'MAN_3D': manhattan,
'CEIL_2D': functools.partial(euclidean, round=math.ceil),
'GEO': euclidean,
'ATT': euclidean,
'GEO': geographical,
'ATT': pseudo_euclidean,
'XRAY1': xray,
'XRAY2': functools.partial(xray, sx=1.25, sy=1.5, sz=1.15),
}
4 changes: 4 additions & 0 deletions tsplib95/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,10 @@ def trace_tours(self, solution):
solutions.append(weight)
return solutions

def trace_canonical_tour(self):
canonical_edges = utils.pairwise(self.get_nodes())
return sum(self.wfunc(i, j) for i, j in canonical_edges)

def get_nodes(self):
"""Return an iterator over the nodes.
Expand Down
9 changes: 7 additions & 2 deletions tsplib95/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ def parse_degrees(coord):
:return: real degrees
:rtype: float
"""
degrees = nint(coord)
degrees = int(coord)
minutes = coord - degrees
return degrees + minutes * 5 / 3

Expand Down Expand Up @@ -58,9 +58,14 @@ def integer_sum(n, m=None):


def pairwise(indexes):
# double list double in case indexes is an iterator
starts = list(indexes)
ends = list(indexes)
ends = list(starts)

# shift the ends by one, and make it circular
ends += [ends.pop(0)]

# pair up the neighbors
return zip(starts, ends)


Expand Down

0 comments on commit 80de85d

Please sign in to comment.