| TSPLIB | R Documentation |
Reads and writes TSPLIB format files. TSPLIB files can be used by most TSP solvers. Many problems in TSPLIB format can be found in the local copy of the TSPLIB95 problem library.
read_TSPLIB(file, precision = 0)
write_TSPLIB(x, file, precision = 6, inf = NULL, neg_inf = NULL)
## S3 method for class 'TSP'
write_TSPLIB(x, file, precision = 6, inf = NULL, neg_inf = NULL)
## S3 method for class 'ATSP'
write_TSPLIB(x, file, precision = 6, inf = NULL, neg_inf = NULL)
## S3 method for class 'ETSP'
write_TSPLIB(x, file, precision = 6, inf = NULL, neg_inf = NULL)
file |
file name or a connection. |
precision |
controls the number of decimal places used to represent
distances (see details). If |
x |
an object with a TSP problem.
|
inf |
replacement value for |
neg_inf |
replacement value for |
In the TSPLIB format distances are represented by integer values. Therefore,
if x contains double values (which is normal in R) the values
given in x are multiplied by 10^{precision} and rounded to the
nearest integer. Note that therefore all results produced by programs using
the TSPLIB file as input need to be divided by 10^{precision} (i.e.,
the decimal point has to be shifted precision places to the left).
For TSP and ATSP objects, a warning is given if distances that are not
zero become zero after the conversion to integers (i.e., precision is too
low).
For ETSP objects, the coordinates are written with precision digits
(in scientific notation) and are not scaled. A warning is given if two or
more cities have identical coordinates in the written file because their
distance is zero.
Currently only the following EDGE_WEIGHT_TYPEs are implemented:
EXPLICIT, EUC_2D, EUC_3D, ATT and GEO.
returns an object of class TSP or
ATSP.
Michael Hahsler
Reinelt, Gerhard. 1991. “TSPLIB—a Traveling Salesman Problem Library.” ORSA Journal on Computing 3 (4): 376–84. \Sexpr[results=rd]{tools:::Rd_expr_doi("10.1287/ijoc.3.4.376")}
Other data:
USCA
## Drilling problem from TSP
drill <- read_TSPLIB(system.file("examples/d493.tsp", package = "TSP"))
drill
tour <- solve_TSP(drill, method = "nn", opt = "two")
tour
plot(drill, tour, cex=.6, col = "red", pch= 3, main = "TSPLIB: d493")
## Write and read data in TSPLIB format
x <- data.frame(x=runif(5), y=runif(5))
## create TSP, ATSP and ETSP (2D)
tsp <- TSP(dist(x))
atsp <- ATSP(dist(x))
etsp <- ETSP(x[,1:2])
write_TSPLIB(tsp, file="example.tsp")
#file.show("example.tsp")
r <- read_TSPLIB("example.tsp")
r
write_TSPLIB(atsp, file="example.tsp")
#file.show("example.tsp")
r <- read_TSPLIB("example.tsp")
r
write_TSPLIB(etsp, file="example.tsp")
#file.show("example.tsp")
r <- read_TSPLIB("example.tsp")
r
## clean up
unlink("example.tsp")
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.