Skip to content

Commit 174a806

Browse files
authored
Merge pull request #92 from jvkersch/fix-random-seed
fix: correct inverted random seed logic in _CCtsp_solve_dat
2 parents a4d373f + 43f255d commit 174a806

4 files changed

Lines changed: 20 additions & 5 deletions

File tree

concorde/_concorde.pyx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ def _CCtsp_solve_dat(
106106

107107
out_tour = np.zeros(ncount, dtype=np.int32)
108108

109-
if seed != 0:
109+
if seed == 0:
110110
seed = <int>CCutil_real_zeit()
111111
CCutil_sprand (seed, &rstate)
112112

concorde/tests/data_utils.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from os.path import dirname, join as pjoin
22

33
import numpy as np
4+
import numpy.testing as nptest
45

56

67
BERLIN_TOUR = np.array(
@@ -72,3 +73,17 @@ def get_dataset_path(tspname):
7273

7374
def get_solution_data(tspname):
7475
return SOLUTION_DATA[tspname]
76+
77+
78+
def assert_tour_equal(actual, expected):
79+
"""Assert two TSP tours are equal, allowing for reverse direction.
80+
81+
A TSP tour is a cycle, so the same optimal tour starting at node 0
82+
can be traversed in either direction.
83+
"""
84+
try:
85+
nptest.assert_array_equal(actual, expected)
86+
except AssertionError:
87+
# Try reversed direction: keep node 0 first, reverse the rest
88+
reversed_tour = np.concatenate([[expected[0]], expected[1:][::-1]])
89+
nptest.assert_array_equal(actual, reversed_tour)

concorde/tests/test_concorde_core.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import numpy.testing as nptest
66

77
from concorde._concorde import _CCutil_gettsplib, _CCtsp_solve_dat
8-
from concorde.tests.data_utils import get_dataset_path, get_solution_data
8+
from concorde.tests.data_utils import get_dataset_path, get_solution_data, assert_tour_equal
99

1010

1111
class TestCCutil_gettsplib(unittest.TestCase):
@@ -45,7 +45,7 @@ def test_solve_berlin_normal(self):
4545
)
4646

4747
# Then
48-
nptest.assert_array_equal(tour, expected_tour)
48+
assert_tour_equal(tour, expected_tour)
4949
self.assertAlmostEqual(val, expected_opt_value)
5050
self.assertTrue(success)
5151
self.assertTrue(foundtour)

concorde/tests/test_concorde_datagroup.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import numpy.testing as nptest
55

66
from concorde.tsp import TSPSolver
7-
from concorde.tests.data_utils import get_dataset_path, get_solution_data
7+
from concorde.tests.data_utils import get_dataset_path, get_solution_data, assert_tour_equal
88

99

1010
class TestTSPSolver(unittest.TestCase):
@@ -75,7 +75,7 @@ def test_solve(self):
7575
tour, val, success, foundtour, hit_timebound = datagroup.solve()
7676

7777
# Then
78-
nptest.assert_array_equal(tour, expected_tour)
78+
assert_tour_equal(tour, expected_tour)
7979
self.assertAlmostEqual(val, expected_opt_value)
8080
self.assertTrue(success)
8181
self.assertTrue(foundtour)

0 commit comments

Comments
 (0)