Skip to main content
Home

Main navigation

  • Home
User account menu
  • Log in

Breadcrumb

  1. Home

A Utility for Converting TSPLIB files to JSON Format

By Skander, 7 November, 2025

I’ve recently been looking for a dataset to evaluate some TSP algorithms. Naturally, the TSPLIB benchmark came to mind. However, one quickly realizes that the TSP instances in this library are stored in a proprietary format. There’s an XML port of these files, but that format has long been superseded by JSON.

 

So, I decided to write a small utility to export TSPLIB files (starting with symmetric TSPs) to JSON format. Unlike many original TSPLIB files, these JSON files are self-contained—they include all the data a TSP solver needs to optimize and evaluate an instance. Specifically, each JSON file contains the pairwise distance matrix between nodes, as well as the best-known solution cost for that instance. While best-known costs may change over time, they tend to remain stable for most benchmarks.

Fortunately, I came across an excellent Python package called tsplib95, developed by Robert Grant and Michael Ritter. This library saved me the trouble of dealing with the many intricacies of the TSPLIB format. It provides a consistent interface to access nodes, edges, and distances between any pair of nodes—regardless of the edge weight type. tsplib95 can also convert TSP problems into the original text format or NetworkX graph objects, but it does not include a transformer for exporting to JSON.

My utility builds on top of tsplib95. It loads and parses TSPLIB files into Problem instances, constructs the distance matrix using tsplib95’s uniform distance computation, and adds the best-known cost from a reference dictionary. Since the current focus is on symmetric TSPs, only the upper half of the distance matrix is stored (excluding the main diagonal). The resulting data structure is then exported as a JSON file.

TSPLIB JSON converter output
 

I have converted most of the 111 TSPLIB instances and shared them in a public GitHub repository. The conversion of some large instances (such as pla85900 and pla33810, with 85,900 and 33,810 nodes respectively) was aborted due to the long processing time—pla85900 alone was estimated to take around 10 hours. Future versions may support parallel computation. Also, these very large JSON files would be several gigabytes in size, so TSP applications may need optimized access strategies—perhaps borrowing ideas from virtual memory systems—to avoid loading entire datasets into memory. Some converted files could not be uploaded to GitHub due to its 50 MB file size limit.

 

An example TSPLIB instance in JSON

I’ve also shared the source code for the converter, so you can generate missing instances yourself—or apply it to your own TSP data that follows the TSPLIB format.

Having self-contained JSON instance files removes the dependency on tsplib95 and makes it easier to implement TSP solvers in any programming language. I hope you’ll find the files and the utility useful.

  • Add new comment
Tags
Travelling Salesman Problem (TSP)

My Apps

  • One-dimensional Cellular Automata Simulator
  • Collatz (Syracuse) Sequence Calculator / Visualizer
  • Erdős–Rényi Random Graph Generator / Analyzer
  • KMeans Animator
  • Language Family Explorer

New Articles

The Hundred-Page Language Models Book - Book Review
A Utility for Converting TSPLIB files to JSON Format
Escape YouTube Filter Bubble - An LLM-based Video Recommender
Implementing a 1-D Binary-State Cellular Automaton with TypeScript, Svelte, and PixiJS
A Parametric Approach to Cellular Automata Framework Design

Skander Kort