TSP heuristics
Shorter tours than nearest neighbour on six point sets, scored by the demo runner
Escape Or-opt local minima with double-bridge perturbations
Browse files+25 −4 routes/or-opt-iterated-kepler/README.md View file
| @@ -5,12 +5,33 @@ tags: search | ||
| 5 | 5 | --- |
| 6 | 6 | # Iterated 2-opt and Or-opt |
| 7 | 7 | |
| 8 | -Continues `or-opt` from `935ea86dbafb0c21f6cbdecbf540ee0422bc489b`. The original solver scored 0.8524, both in Shoal job 15 and in a local run. | |
| 8 | +Continues `or-opt` from `935ea86dbafb0c21f6cbdecbf540ee0422bc489b`. The solver precomputes distances and uses eight deterministic nearest-neighbour starting cities. It alternates 2-opt with Or-opt relocations of one to three cities in either orientation, including runs that wrap around the cycle. It then applies 128 double-bridge perturbations to the best tour, runs the same local search after each, and keeps improvements. | |
| 9 | 9 | |
| 10 | -This checkpoint precomputes distances, searches Or-opt runs around the entire cycle, and keeps the shortest local optimum from eight deterministic nearest-neighbour starts. The six-set score was 0.8482 locally in 0.57 s. The server's check is queued while `ada-server` is offline. | |
| 10 | +## Local measurements | |
| 11 | 11 | |
| 12 | -From the repository root, run `python3 score.py routes/or-opt-iterated-kepler`. | |
| 12 | +The original solver reproduced [Shoal job 15](https://shoal.club/ada/tsp-heuristics/jobs/15) at 0.8524. The new solver measured **0.8402** with the same scorer, a 1.43% reduction in the mean ratio. Shoal verification is pending because the project's runner is offline. | |
| 13 | + | |
| 14 | +| Point set | Original | Iterated search | | |
| 15 | +| --- | ---: | ---: | | |
| 16 | +| n40-1 | 0.8876 | 0.8744 | | |
| 17 | +| n40-2 | 0.7962 | 0.7962 | | |
| 18 | +| n80-3 | 0.8945 | 0.8901 | | |
| 19 | +| n80-4 | 0.8898 | 0.8636 | | |
| 20 | +| n160-5 | 0.8033 | 0.7916 | | |
| 21 | +| n160-6 | 0.8428 | 0.8254 | | |
| 22 | +| **Mean** | **0.8524** | **0.8402** | | |
| 23 | + | |
| 24 | +Eight starts with no perturbations scored 0.8482. With 16, 32, and 64 perturbations the scores were 0.8442, 0.8410, and 0.8406, respectively. At 128 perturbations, also accepting worse tours and restarting from the best tour every 4, 8, or 16 steps scored 0.8415, 0.8420, and 0.8427. The final version keeps only improvements and takes approximately five seconds locally, within the scorer's 30-second total limit. | |
| 25 | + | |
| 26 | +Reproduce from the repository root using Python 3.12: | |
| 27 | + | |
| 28 | +```sh | |
| 29 | +python3.12 score.py routes/or-opt | |
| 30 | +python3.12 score.py routes/or-opt-iterated-kepler | |
| 31 | +``` | |
| 32 | + | |
| 33 | +Each invocation writes its measurements to `metrics.json` in the working directory. The route's own `metrics.json` contains the local run being reported. | |
| 13 | 34 | |
| 14 | 35 | ## Next |
| 15 | 36 | |
| 16 | -Test double-bridge perturbations that let local search leave its current basin. | |
| 37 | +Check the queued score job when `ada-server` returns. Compare the score and runtime with these local measurements before adopting the route. | |
+12 −1 routes/or-opt-iterated-kepler/metrics.json View file
| @@ -1 +1,12 @@ | ||
| 1 | -{"score": 0.8482, "cost": 0.57, "subtasks": {"n40-1": 0.8829, "n40-2": 0.7962, "n80-3": 0.8901, "n80-4": 0.88, "n160-5": 0.7959, "n160-6": 0.844}} | |
| 1 | +{ | |
| 2 | + "score": 0.8402, | |
| 3 | + "cost": 4.66, | |
| 4 | + "subtasks": { | |
| 5 | + "n40-1": 0.8744, | |
| 6 | + "n40-2": 0.7962, | |
| 7 | + "n80-3": 0.8901, | |
| 8 | + "n80-4": 0.8636, | |
| 9 | + "n160-5": 0.7916, | |
| 10 | + "n160-6": 0.8254 | |
| 11 | + } | |
| 12 | +} | |
+24 −3 routes/or-opt-iterated-kepler/solve.py View file
| @@ -1,4 +1,4 @@ | ||
| 1 | -"""Improve nearest-neighbour tours with alternating 2-opt and Or-opt.""" | |
| 1 | +"""Iterated local search with alternating 2-opt and Or-opt.""" | |
| 2 | 2 | |
| 3 | 3 | import json |
| 4 | 4 | import math |
| @@ -8,6 +8,7 @@ import sys | ||
| 8 | 8 | |
| 9 | 9 | EPSILON = 1e-12 |
| 10 | 10 | STARTS = 8 |
| 11 | +PERTURBATIONS = 128 | |
| 11 | 12 | |
| 12 | 13 | |
| 13 | 14 | def nearest_neighbour(distances, start): |
| @@ -49,7 +50,7 @@ def or_opt(tour, distances): | ||
| 49 | 50 | for first in range(size): |
| 50 | 51 | start = doubled[first] |
| 51 | 52 | end = doubled[first + width - 1] |
| 52 | - before = doubled[first - 1] | |
| 53 | + before = tour[first - 1] | |
| 53 | 54 | after = doubled[first + width] |
| 54 | 55 | start_row, end_row = distances[start], distances[end] |
| 55 | 56 | removed = start_row[before] + end_row[after] - distances[before][after] |
| @@ -78,6 +79,14 @@ def length(tour, distances): | ||
| 78 | 79 | return sum(distances[city][tour[index - 1]] for index, city in enumerate(tour)) |
| 79 | 80 | |
| 80 | 81 | |
| 82 | +def double_bridge(tour, rng): | |
| 83 | + """Cut four cycle edges and reconnect the segments in a different order.""" | |
| 84 | + # Leave at least two cities per segment so the kick changes four edges. | |
| 85 | + first, second, third = sorted(rng.sample(range(1, len(tour) - 4), 3)) | |
| 86 | + first, second, third = first + 1, second + 2, third + 3 | |
| 87 | + return tour[:first] + tour[third:] + tour[second:third] + tour[first:second] | |
| 88 | + | |
| 89 | + | |
| 81 | 90 | def solve(points): |
| 82 | 91 | size = len(points) |
| 83 | 92 | if size < 4: |
| @@ -87,7 +96,19 @@ def solve(points): | ||
| 87 | 96 | rng = random.Random(0) |
| 88 | 97 | starts = [0] + rng.sample(range(1, size), min(STARTS, size) - 1) |
| 89 | 98 | tours = (improve(nearest_neighbour(distances, start), distances) for start in starts) |
| 90 | - return min(tours, key=lambda tour: length(tour, distances)) | |
| 99 | + best = min(tours, key=lambda tour: length(tour, distances)) | |
| 100 | + if size < 8: | |
| 101 | + return best | |
| 102 | + | |
| 103 | + best_length = length(best, distances) | |
| 104 | + for _ in range(PERTURBATIONS): | |
| 105 | + # Rotate the cycle so any edge can be used as the first cut. | |
| 106 | + offset = rng.randrange(size) | |
| 107 | + candidate = improve(double_bridge(best[offset:] + best[:offset], rng), distances) | |
| 108 | + candidate_length = length(candidate, distances) | |
| 109 | + if candidate_length < best_length - EPSILON: | |
| 110 | + best, best_length = candidate, candidate_length | |
| 111 | + return best | |
| 91 | 112 | |
| 92 | 113 | |
| 93 | 114 | if __name__ == "__main__": |