Skip to content

TSPLIB

All formats · Reader spec · Cases and output

tsplib.iris reads explicit edge weights in TSPLIB matrix layouts and coordinates for EUC_2D, CEIL_2D, ATT, and GEO. The dimension must precede sections that use it. COMMENT lines are skipped; the reader accepts only the header keys and sections listed in the spec. This is a TSP reader, not the complete family of TSPLIB problem formats.

The format source is Gerhard Reinelt’s TSPLIB 95 documentation, particularly the specification and data parts. The reader spec is copied from Gecode’s tsp.cpp.

  • explicit-five stores a symmetric 5×5 FULL_MATRIX as the weight entry.
  • euc-2d-five stores five node IDs and the float arrays x and y. Expected data contains coordinates, including 1.5; distance construction belongs to the Gecode example’s host code.
  • missing-weights declares EXPLICIT without an EDGE_WEIGHT_SECTION. The reference reports E-CHECK-FAILED with an empty record path.

These are hand-made samples under the repository’s MIT license. See the library README for case layout, validation, and Gecode notices.

Reader spec

From Gecode commit 5e7bbd5c26b0702c6fd652356b2289da0710ac3c. Source notices.

format tsplib
comment "COMMENT"
sections until "EOF" {
"NAME : {name:rest}" optional
"TYPE : {type:word}" optional
"DIMENSION : {n:2..46340}"
"EDGE_WEIGHT_TYPE : {edge_weight_type:word}" optional
"EDGE_WEIGHT_FORMAT : {edge_weight_format:word}" optional
"NODE_COORD_TYPE : {node_coord_type:word}" optional
"DISPLAY_DATA_TYPE : {display_data_type:word}" optional
"NODE_COORD_SECTION" optional {
repeat n {
node: int
x: float
y: float
}
}
"EDGE_WEIGHT_SECTION" optional {
if !defined(edge_weight_format) {
let edge_weight_format = "FULL_MATRIX"
}
weight[n][n]: 0..gecode.int_max / n layout edge_weight_format
}
Excerpt · View full reader spec

Cases and output

Run the commands from the repository root. Successful reads show dzn, JSON, and a summary. Rejected inputs show the CLI's diagnostic and caret, with the stable error code beside it.

euc-2d-five

Five cities given by two-dimensional coordinates

NAME : handmade-euc-2d-five
TYPE : TSP
DIMENSION : 5
EDGE_WEIGHT_TYPE : EUC_2D
NODE_COORD_TYPE : TWOD_COORDS
NODE_COORD_SECTION
1 0 0
2 3 0
3 3 4
4 0 4
5 1.5 2
EOF
dzn
iris read formats/tsplib/tsplib.iris formats/tsplib/cases/euc-2d-five/input.txt --dzn
name = "handmade-euc-2d-five";
'type' = "TSP";
n = 5;
edge_weight_type = "EUC_2D";
node_coord_type = "TWOD_COORDS";
node = [1, 2, 3, 4, 5];
x = [0.0, 3.0, 3.0, 0.0, 1.5];
y = [0.0, 0.0, 4.0, 4.0, 2.0];
JSON
iris read formats/tsplib/tsplib.iris formats/tsplib/cases/euc-2d-five/input.txt --json
{"iris":"0.1","entries":[{"name":"name","type":"string","depth":0,"value":"handmade-euc-2d-five"},{"name":"type","type":"string","depth":0,"value":"TSP"},{"name":"n","type":"int","depth":0,"value":5},{"name":"edge_weight_type","type":"string","depth":0,"value":"EUC_2D"},{"name":"node_coord_type","type":"string","depth":0,"value":"TWOD_COORDS"},{"name":"node","type":"int","depth":1,"value":[1,2,3,4,5]},{"name":"x","type":"float","depth":1,"value":[0,3,3,0,1.5]},{"name":"y","type":"float","depth":1,"value":[0,0,4,4,2]}]}
Summary
iris read formats/tsplib/tsplib.iris formats/tsplib/cases/euc-2d-five/input.txt --summary
formats/tsplib/cases/euc-2d-five/input.txt [tsplib]
  name: string "handmade-euc-2d-five"
  type: string "TSP"
  n: int 5
  edge_weight_type: string "EUC_2D"
  node_coord_type: string "TWOD_COORDS"
  node: int array 5
  x: float array 5
  y: float array 5

Download full case · Open in playground

explicit-five

Five cities with an explicit full distance matrix

NAME : handmade-explicit-five
TYPE : TSP
COMMENT : Hand-made symmetric distances
DIMENSION : 5
EDGE_WEIGHT_TYPE : EXPLICIT
EDGE_WEIGHT_FORMAT : FULL_MATRIX
EDGE_WEIGHT_SECTION
0 2 4 6 3
2 0 5 4 7
4 5 0 3 6
6 4 3 0 2
3 7 6 2 0
EOF
dzn
iris read formats/tsplib/tsplib.iris formats/tsplib/cases/explicit-five/input.txt --dzn
name = "handmade-explicit-five";
'type' = "TSP";
n = 5;
edge_weight_type = "EXPLICIT";
edge_weight_format = "FULL_MATRIX";
weight = [| 0, 2, 4, 6, 3
  | 2, 0, 5, 4, 7
  | 4, 5, 0, 3, 6
  | 6, 4, 3, 0, 2
  | 3, 7, 6, 2, 0 |];
JSON
iris read formats/tsplib/tsplib.iris formats/tsplib/cases/explicit-five/input.txt --json
{"iris":"0.1","entries":[{"name":"name","type":"string","depth":0,"value":"handmade-explicit-five"},{"name":"type","type":"string","depth":0,"value":"TSP"},{"name":"n","type":"int","depth":0,"value":5},{"name":"edge_weight_type","type":"string","depth":0,"value":"EXPLICIT"},{"name":"edge_weight_format","type":"string","depth":0,"value":"FULL_MATRIX"},{"name":"weight","type":"int","depth":2,"value":[[0,2,4,6,3],[2,0,5,4,7],[4,5,0,3,6],[6,4,3,0,2],[3,7,6,2,0]]}]}
Summary
iris read formats/tsplib/tsplib.iris formats/tsplib/cases/explicit-five/input.txt --summary
formats/tsplib/cases/explicit-five/input.txt [tsplib]
  name: string "handmade-explicit-five"
  type: string "TSP"
  n: int 5
  edge_weight_type: string "EXPLICIT"
  edge_weight_format: string "FULL_MATRIX"
  weight: int array 5 x 5

Download full case · Open in playground

missing-weights

Explicit distances require an edge weight section

NAME : handmade-missing-weights
TYPE : TSP
DIMENSION : 5
EDGE_WEIGHT_TYPE : EXPLICIT
EOF

E-CHECK-FAILED

check failed: no EDGE_WEIGHT_SECTION

Read rejectedRun from the repository root.
iris read formats/tsplib/tsplib.iris formats/tsplib/cases/missing-weights/input.txt --dzn
formats/tsplib/cases/missing-weights/input.txt:5:4: check failed: no EDGE_WEIGHT_SECTION
     5 | EOF
       |    ^
spec: formats/tsplib/tsplib.iris:38

Input rejected

Download full case · Open in playground