DESCRIPTION = '''The Traveling Salesman Problem (TSP) is a classic combinatorial optimization problem where, given a set of cities with known pairwise distances, the objective is to find the shortest possible tour that visits each city exactly once and returns to the starting city. More formally, given a complete graph G = (V, E) with vertices V representing cities and edges E with weights representing distances, we seek to find a Hamiltonian cycle (a closed path visiting each vertex exactly once) of minimum total weight.''' def solve(**kwargs): """ Solve a TSP instance. Args: - nodes (list): List of (x, y) coordinates representing cities in the TSP problem Format: [(x1, y1), (x2, y2), ..., (xn, yn)] Returns: dict: Solution information with: - 'tour' (list): List of node indices representing the solution path Format: [0, 3, 1, ...] where numbers are indices into the nodes list """ # Your function must yield multiple solutions over time, not just return one solution # Use Python's yield keyword repeatedly to produce a stream of solutions # Each yielded solution should be better than the previous one # Evaluation is on the last yielded solution before timeout while True: yield { 'tour': [], } def load_data(filepath): import re nodes = [] dimension = 0 reading_nodes = False with open(filepath, 'r') as f: for line in f: line = line.strip() # Extract dimension if present if line.startswith("DIMENSION"): dimension = int(line.split(":")[1].strip()) # Check if we're at the node coordinates section elif line == "NODE_COORD_SECTION": reading_nodes = True # End of node coordinates section elif line == "EOF": reading_nodes = False # Read node coordinates elif reading_nodes and line: # TSP format typically has: index x_coord y_coord parts = re.split(r'\s+', line.strip()) if len(parts) >= 3: # Make sure we have at least index, x, y try: # Skip the index (parts[0]) x, y = float(parts[1]), float(parts[2]) nodes.append([x, y]) except ValueError: continue # Skip lines that can't be parsed properly # Verify that we got the expected number of nodes if dimension > 0 and len(nodes) != dimension: print(f"Warning: Expected {dimension} nodes but found {len(nodes)}") # Convert to tensor return [{'nodes': nodes}] def eval_func(nodes, tour): """ Evaluate a predicted TSP tour against a reference tour. Args: nodes (list): List of (x, y) coordinates representing cities in the TSP problem Format: [(x1, y1), (x2, y2), ..., (xn, yn)] tour (list): Predicted tour from the solver as list of node indices Format: [0, 3, 1, ...] Returns: float: Optimality gap percentage ((predicted_cost/optimal_cost - 1) * 100) or just the predicted cost if no label_tour is provided """ # Calculate the predicted tour cost import math num_nodes = len(nodes) if len(tour) != num_nodes: raise Exception(f"Invalid tour length: Expected {num_nodes}, got {len(tour)}") nodes_set = set(tour) if len(nodes_set) != num_nodes: raise Exception(f"Invalid tour: Contains {len(nodes_set)} unique nodes, expected {num_nodes}") expected_nodes = set(range(num_nodes)) if nodes_set != expected_nodes: raise Exception(f"Invalid tour: Contains out-of-range or missing nodes") def calculate_tour_cost(nodes, tour): cost = 0 for i in range(len(tour)): from_node = tour[i] to_node = tour[(i + 1) % len(tour)] # Wrap around to the first node # Calculate Euclidean distance from_x, from_y = nodes[from_node] to_x, to_y = nodes[to_node] segment_cost = math.sqrt((to_x - from_x) ** 2 + (to_y - from_y) ** 2) cost += segment_cost return cost pred_cost = calculate_tour_cost(nodes, tour) return pred_cost def norm_score(results): optimal_scores = {'valid_instances/E1k.0': [23360648.0], 'valid_instances/E1k.1': [22985695.0], 'valid_instances/E1k.2': [23023351.0], 'valid_instances/E1k.3': [23149856.0], 'valid_instances/E1k.4': [22698717.0], 'valid_instances/E1k.5': [23192391.0], 'valid_instances/E1k.6': [23349803.0], 'valid_instances/E1k.7': [22882343.0], 'valid_instances/E1k.8': [23027023.0], 'valid_instances/E1k.9': [23356256.0]} optimal_scores = optimal_scores | {'hard_test_instances/C100k.0': [104617752.0], 'hard_test_instances/C100k.1': [105390777.0], 'hard_test_instances/C10k.0': [33001034.0], 'hard_test_instances/C10k.1': [33186248.0], 'hard_test_instances/C10k.2': [33155424.0], 'hard_test_instances/C316k.0': [186870839.0], 'hard_test_instances/C31k.0': [59545390.0], 'hard_test_instances/C31k.1': [59293266.0], 'hard_test_instances/E100k.0': [225784127.0], 'hard_test_instances/E100k.1': [225654639.0], 'hard_test_instances/E10M.0': [2253040346.0], 'hard_test_instances/E10k.0': [71865826.0], 'hard_test_instances/E10k.1': [72031630.0], 'hard_test_instances/E10k.2': [71822483.0], 'hard_test_instances/E1M.0': [713187688.0], 'hard_test_instances/E316k.0': [401301206.0], 'hard_test_instances/E31k.0': [127282138.0], 'hard_test_instances/E31k.1': [127452384.0], 'hard_test_instances/E3M.0': [1267295473.0]} optimal_scores = optimal_scores | {'easy_test_instances/brd14051.tsp': [469385.0], 'easy_test_instances/d1291.tsp': [50801.0], 'easy_test_instances/d15112.tsp': [1573084.0], 'easy_test_instances/d1655.tsp': [62128.0], 'easy_test_instances/d18512.tsp': [645238.0], 'easy_test_instances/d2103.tsp': [80450.0], 'easy_test_instances/fl1400.tsp': [20127.0], 'easy_test_instances/fl1577.tsp': [22249.0], 'easy_test_instances/fl3795.tsp': [28772.0], 'easy_test_instances/fnl4461.tsp': [182566.0], 'easy_test_instances/nrw1379.tsp': [56638.0], 'easy_test_instances/pcb1173.tsp': [56892.0], 'easy_test_instances/pcb3038.tsp': [137694.0], 'easy_test_instances/pr1002.tsp': [259045.0], 'easy_test_instances/pr2392.tsp': [378032.0], 'easy_test_instances/rl11849.tsp': [923288.0], 'easy_test_instances/rl1304.tsp': [252948.0], 'easy_test_instances/rl1323.tsp': [270199.0], 'easy_test_instances/rl1889.tsp': [316536.0], 'easy_test_instances/rl5915.tsp': [565530.0], 'easy_test_instances/rl5934.tsp': [556045.0], 'easy_test_instances/u1060.tsp': [224094.0], 'easy_test_instances/u1432.tsp': [152970.0], 'easy_test_instances/u1817.tsp': [57201.0], 'easy_test_instances/u2152.tsp': [64253.0], 'easy_test_instances/u2319.tsp': [234256.0], 'easy_test_instances/usa13509.tsp': [19982859.0], 'easy_test_instances/vm1084.tsp': [239297.0], 'easy_test_instances/vm1748.tsp': [336556.0]} normed = {} for case, (scores, error_message) in results.items(): if case not in optimal_scores: continue # Skip if there's no optimal score defined. optimal_list = optimal_scores[case] normed_scores = [] # Compute normalized score for each index. for idx, score in enumerate(scores): if isinstance(score, (int, float)): normed_scores.append(1 - abs(score - optimal_list[idx]) / max(score, optimal_list[idx])) else: normed_scores.append(score) normed[case] = (normed_scores, error_message) return normed