5 * read_tsplib - read a symmetric tsp instance
6 * read_atsplib - asymmetric
8 Copyright (c) by Joao Pedro PEDROSO and Mikio KUBO, 2012
16 """Compute the L2-norm (Euclidean) distance between two points.
18 The distance is rounded to the closest integer, for compatibility
19 with the TSPLIB convention.
21 The two points are located on coordinates (x1,y1) and (x2,y2),
25 return int(math.sqrt(xdiff * xdiff + ydiff * ydiff) + .5)
29 """Compute the L1-norm (Manhattan) distance between two points.
31 The distance is rounded to the closest integer, for compatibility
32 with the TSPLIB convention.
34 The two points are located on coordinates (x1,y1) and (x2,y2),
36 return int(abs(x2 - x1) + abs(y2 - y1) + .5)
40 """Compute the Linfty distance between two points (see TSPLIB documentation)"""
41 return int(max(abs(x2 - x1), abs(y2 - y1)))
45 """Compute the ATT distance between two points (see TSPLIB documentation)"""
48 rij = math.sqrt((xd * xd + yd * yd) / 10.)
57 """returns smallest integer not less than the distance of two points"""
60 return int(math.ceil(math.sqrt(xdiff * xdiff + ydiff * ydiff)))
64 print(
"Implementation is wrong")
69 lat1 = PI * (deg + 5. * min_ / 3) / 180.
72 long1 = PI * (deg + 5. * min_ / 3) / 180.
75 lat2 = PI * (deg + 5. * min_ / 3) / 180.
78 long2 = PI * (deg + 5. * min_ / 3) / 180.
81 q1 = math.cos(long1 - long2);
82 q2 = math.cos(lat1 - lat2);
83 q3 = math.cos(lat1 + lat2);
84 return int(RRR * math.acos(.5 * ((1. + q1) * q2 - (1. - q1) * q3)) + 1.)
92 for data
in line.split():
99 return range(1, n + 1), c,
None,
None
107 for data
in line.split():
114 return range(1, n + 1), c,
None,
None
122 for data
in line.split():
129 return range(1, n + 1), c,
None,
None
137 for data
in line.split():
145 return range(1, n + 1), c,
None,
None
149 "basic function for reading a symmetric problem in the TSPLIB format"
150 "data is stored in an upper triangular matrix"
151 "NOTE: some distance types are not handled yet"
152 if filename[-3:] ==
".gz":
153 f = gzip.open(filename,
"rt")
158 while line.find(
"DIMENSION") == -1:
160 n = int(line.split()[-1])
162 while line.find(
"EDGE_WEIGHT_TYPE") == -1:
165 if line.find(
"EUC_2D") != -1:
167 elif line.find(
"MAN_2D") != -1:
169 elif line.find(
"MAX_2D") != -1:
171 elif line.find(
"ATT") != -1:
173 elif line.find(
"CEIL_2D") != -1:
178 elif line.find(
"EXPLICIT") != -1:
179 while line.find(
"EDGE_WEIGHT_FORMAT") == -1:
181 if line.find(
"LOWER_DIAG_ROW") != -1:
182 while line.find(
"EDGE_WEIGHT_SECTION") == -1:
185 if line.find(
"UPPER_ROW") != -1:
186 while line.find(
"EDGE_WEIGHT_SECTION") == -1:
189 if line.find(
"UPPER_DIAG_ROW") != -1:
190 while line.find(
"EDGE_WEIGHT_SECTION") == -1:
193 if line.find(
"FULL_MATRIX") != -1:
194 while line.find(
"EDGE_WEIGHT_SECTION") == -1:
197 print(
"error reading line " + line)
200 print(
"cannot deal with '%s' distances" % line)
203 while line.find(
"NODE_COORD_SECTION") == -1:
209 if line.find(
"EOF") != -1
or not line:
break
210 (i, xi, yi) = line.split()
218 c[i, j] = dist(x[i], y[i], x[j], y[j])
224 "basic function for reading a ATSP problem on the TSPLIB format"
225 "NOTE: only works for explicit matrices"
227 if filename[-3:] ==
".gz":
228 f = gzip.open(filename,
'r')
231 f = open(filename,
'r')
235 if line.find(
"DIMENSION") >= 0:
236 n = int(line.split()[1])
239 raise IOError(
"'DIMENSION' keyword not found in file '%s'" % filename)
242 if line.find(
"EDGE_WEIGHT_TYPE") >= 0:
243 if line.split()[1] ==
"EXPLICIT":
246 raise IOError(
"'EDGE_WEIGHT_TYPE' is not 'EXPLICIT' in file '%s'" % filename)
248 for k, line
in enumerate(data):
249 if line.find(
"EDGE_WEIGHT_SECTION") >= 0:
252 raise IOError(
"'EDGE_WEIGHT_SECTION' not found in file '%s'" % filename)
257 for line
in data[k + 1:]:
258 if line.find(
"EOF") >= 0:
260 for val
in line.split():
261 dist.append(int(val))
266 c[i + 1, j + 1] = dist[k]
272 if __name__ ==
"__main__":
276 if len(sys.argv) < 2:
277 print(
'Usage: %s instance' % sys.argv[0])
280 from read_tsplib
import read_tsplib
283 print(len(V),
"vertices,", len(c),
"arcs")
284 print(
"distance matrix:")