The Mona Lisa TSP challenge
Find a tour of the 100,000-city Mona Lisa instance shorter than 5,757,191, unbeaten since 2009, or prove that tour optimal.

| 1 | """The Mona Lisa TSP: Nagata's record tour, drawn as the continuous line it is. |
| 2 | |
| 3 | Data: tour.txt.gz is the 100,000 cities of mona-lisa100K.tsp (https://www.math.uwaterloo.ca/tsp/data/ml/mona-lisa100K.tsp) |
| 4 | in the order of Nagata's tour (https://www.math.uwaterloo.ca/tsp/data/ml/tour/monalisa_5757191.tour), both from the |
| 5 | Waterloo TSP pages the README cites. First line: the first city's coordinates; every later line: the step to the next |
| 6 | city. The whole tour is kept; the figure checks it (100,000 distinct cities, rounded EUC_2D length 5,757,191) and then |
| 7 | draws every STEP-th city in tour order, so the line is the tour seen at card resolution. |
| 8 | """ |
| 9 | import gzip |
| 10 | import math |
| 11 | from pathlib import Path |
| 12 | |
| 13 | import kit |
| 14 | |
| 15 | HERE = Path(__file__).resolve().parent |
| 16 | STEP = 8 # every 8th city: 12,500 vertices, enough for the face at card size |
| 17 | Q = 2 # path coordinates in half pixels, drawn at scale(1/Q), so the steps are small integers |
| 18 | |
| 19 | x = y = None |
| 20 | pts = [] |
| 21 | for line in gzip.open(HERE / 'tour.txt.gz', 'rt'): |
| 22 | a, b = map(int, line.split()) |
| 23 | x, y = (a, b) if x is None else (x + a, y + b) |
| 24 | pts.append((x, y)) |
| 25 | assert len(pts) == len(set(pts)) == 100_000 |
| 26 | length = sum(int(math.hypot(p[0] - q[0], p[1] - q[1]) + 0.5) for p, q in zip(pts, pts[1:] + pts[:1])) |
| 27 | assert length == 5_757_191, length |
| 28 | |
| 29 | xs, ys = [p[0] for p in pts], [p[1] for p in pts] |
| 30 | x0, x1, y0, y1 = min(xs), max(xs), min(ys), max(ys) |
| 31 | h = 252 # canvas px for the picture's height |
| 32 | s = h / (y1 - y0) |
| 33 | ox = kit.W / 2 - (x1 - x0) * s / 2 |
| 34 | # TSPLIB y grows upwards |
| 35 | P = lambda p: (ox + (p[0] - x0) * s, 8 + (y1 - p[1]) * s) |
| 36 | line = pts[::STEP] + pts[:1] |
| 37 | q = [(round(a * Q), round(b * Q)) for a, b in map(P, line)] |
| 38 | d = f'M{q[0][0]} {q[0][1]}l' + ' '.join(f'{b[0] - a[0]} {b[1] - a[1]}' for a, b in zip(q, q[1:])).replace(' -', '-') |
| 39 | # ponytail: stroke 1 px, under the kit's 2, because the picture is line density; at 2 the portrait goes solid |
| 40 | body = kit.path(d, 'ink2-s', 1 * Q, f' transform="scale({1 / Q:g})"') |
| 41 | label = f'best tour, length {length:,} · every {STEP}th city drawn' |
| 42 | kit.write(__file__, 'computer-science', body, label, |
| 43 | f"Nagata's tour of the 100,000 Mona Lisa cities, length {length:,}, drawn through every {STEP}th city") |
| 44 | print(label, len(line), 'vertices') |